flip_social_feed.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "flip_social_feed.h"
  2. // Set failure FlipSocialFeed object
  3. bool flip_social_temp_feed() {
  4. if(flip_social_feed == NULL) {
  5. flip_social_feed = malloc(sizeof(FlipSocialFeed));
  6. if(flip_social_feed == NULL) {
  7. FURI_LOG_E(TAG, "Failed to allocate memory for feed");
  8. return false;
  9. }
  10. }
  11. for(int i = 0; i < 3; i++) {
  12. if(flip_social_feed->usernames[i] == NULL) {
  13. flip_social_feed->usernames[i] = malloc(MAX_USER_LENGTH);
  14. if(flip_social_feed->usernames[i] == NULL) {
  15. FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
  16. return false;
  17. }
  18. }
  19. if(flip_social_feed->messages[i] == NULL) {
  20. flip_social_feed->messages[i] = malloc(MAX_MESSAGE_LENGTH);
  21. if(flip_social_feed->messages[i] == NULL) {
  22. FURI_LOG_E(TAG, "Failed to allocate memory for message %zu", i);
  23. return false;
  24. }
  25. }
  26. }
  27. flip_social_feed->usernames[0] = "JBlanked";
  28. flip_social_feed->usernames[1] = "FlipperKing";
  29. flip_social_feed->usernames[2] = "FlipperQueen";
  30. //
  31. flip_social_feed->messages[0] =
  32. "Welcome. This is a temp message. Either the feed didn't load or there was a server error.";
  33. flip_social_feed->messages[1] = "I am the Chosen Flipper.";
  34. flip_social_feed->messages[2] = "No one can flip like me.";
  35. //
  36. flip_social_feed->is_flipped[0] = true;
  37. flip_social_feed->is_flipped[1] = false;
  38. flip_social_feed->is_flipped[2] = true;
  39. //
  40. flip_social_feed->ids[0] = 0;
  41. flip_social_feed->ids[1] = 1;
  42. flip_social_feed->ids[2] = 2;
  43. //
  44. flip_social_feed->flips[0] = 51;
  45. flip_social_feed->flips[1] = 8;
  46. flip_social_feed->flips[2] = 23;
  47. //
  48. flip_social_feed->count = 3;
  49. flip_social_feed->index = 0;
  50. return true;
  51. }
  52. // Allocate memory for each feed item if not already allocated
  53. FlipSocialFeed* flip_social_feed_alloc() {
  54. // Initialize the feed
  55. FlipSocialFeed* feed = (FlipSocialFeed*)malloc(sizeof(FlipSocialFeed));
  56. if(!feed) {
  57. FURI_LOG_E(TAG, "Failed to allocate memory for feed");
  58. return feed;
  59. }
  60. for(size_t i = 0; i < MAX_FEED_ITEMS; i++) {
  61. if(feed->usernames[i] == NULL) {
  62. feed->usernames[i] = malloc(MAX_USER_LENGTH);
  63. if(feed->usernames[i] == NULL) {
  64. FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
  65. return NULL;
  66. }
  67. }
  68. if(feed->messages[i] == NULL) {
  69. feed->messages[i] = malloc(MAX_MESSAGE_LENGTH);
  70. if(feed->messages[i] == NULL) {
  71. FURI_LOG_E(TAG, "Failed to allocate memory for message %zu", i);
  72. return NULL;
  73. }
  74. }
  75. }
  76. return feed;
  77. }
  78. void flip_social_free_feed() {
  79. if(!flip_social_feed) {
  80. FURI_LOG_E(TAG, "Feed model is NULL");
  81. return;
  82. }
  83. for(uint32_t i = 0; i < flip_social_feed->count; i++) {
  84. if(flip_social_feed->usernames[i]) {
  85. free(flip_social_feed->usernames[i]);
  86. }
  87. }
  88. }
  89. bool flip_social_get_feed() {
  90. // Get the feed from the server
  91. if(app_instance->login_username_logged_out == NULL) {
  92. FURI_LOG_E(TAG, "Username is NULL");
  93. return false;
  94. }
  95. char command[128];
  96. snprintf(
  97. fhttp.file_path,
  98. sizeof(fhttp.file_path),
  99. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/feed.txt");
  100. fhttp.save_received_data = true;
  101. char* headers = jsmn("Content-Type", "application/json");
  102. snprintf(
  103. command,
  104. 128,
  105. "https://www.flipsocial.net/api/feed/40/%s/extended/",
  106. app_instance->login_username_logged_out);
  107. bool success = flipper_http_get_request_with_headers(command, headers);
  108. free(headers);
  109. if(!success) {
  110. FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
  111. fhttp.state = ISSUE;
  112. return false;
  113. }
  114. fhttp.state = RECEIVING;
  115. return true;
  116. }
  117. bool flip_social_parse_json_feed() {
  118. // load the received data from the saved file
  119. FuriString* feed_data = flipper_http_load_from_file(fhttp.file_path);
  120. if(feed_data == NULL) {
  121. FURI_LOG_E(TAG, "Failed to load received data from file.");
  122. return false;
  123. }
  124. char* data_cstr = (char*)furi_string_get_cstr(feed_data);
  125. if(data_cstr == NULL) {
  126. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  127. furi_string_free(feed_data);
  128. return false;
  129. }
  130. // Allocate memory for each feed item if not already allocated
  131. flip_social_feed = flip_social_feed_alloc();
  132. if(flip_social_feed == NULL) {
  133. return false;
  134. }
  135. // Remove newlines
  136. char* pos = data_cstr;
  137. while((pos = strchr(pos, '\n')) != NULL) {
  138. *pos = ' ';
  139. }
  140. // Initialize feed count
  141. flip_social_feed->count = 0;
  142. // Iterate through the feed array
  143. for(int i = 0; i < MAX_FEED_ITEMS; i++) {
  144. // Parse each item in the array
  145. char* item = get_json_array_value("feed", i, data_cstr, MAX_TOKENS);
  146. if(item == NULL) {
  147. break;
  148. }
  149. // Extract individual fields from the JSON object
  150. char* username = get_json_value("username", item, MAX_TOKENS);
  151. char* message = get_json_value("message", item, MAX_TOKENS);
  152. char* flipped = get_json_value("flipped", item, MAX_TOKENS);
  153. char* flips = get_json_value("flip_count", item, MAX_TOKENS);
  154. char* id = get_json_value("id", item, MAX_TOKENS);
  155. if(username == NULL || message == NULL || flipped == NULL || id == NULL) {
  156. FURI_LOG_E(TAG, "Failed to parse item fields.");
  157. free(item);
  158. free(username);
  159. free(message);
  160. free(flipped);
  161. free(flips);
  162. free(id);
  163. continue;
  164. }
  165. // Safely copy strings with bounds checking
  166. snprintf(flip_social_feed->usernames[i], MAX_USER_LENGTH, "%s", username);
  167. snprintf(flip_social_feed->messages[i], MAX_MESSAGE_LENGTH, "%s", message);
  168. // Store boolean and integer values
  169. flip_social_feed->is_flipped[i] = strstr(flipped, "true") != NULL;
  170. flip_social_feed->ids[i] = atoi(id);
  171. flip_social_feed->flips[i] = atoi(flips);
  172. flip_social_feed->count++;
  173. // Free allocated memory
  174. free(item);
  175. free(username);
  176. free(message);
  177. free(flipped);
  178. free(flips);
  179. free(id);
  180. }
  181. furi_string_free(feed_data);
  182. free(data_cstr);
  183. return flip_social_feed->count > 0;
  184. }