flip_social_feed.c 5.9 KB

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