flip_social_feed.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. #include "flip_social_feed.h"
  2. bool flip_social_get_feed(bool alloc_http)
  3. {
  4. if (!app_instance)
  5. {
  6. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  7. return false;
  8. }
  9. if (alloc_http && !flipper_http_init(flipper_http_rx_callback, app_instance))
  10. {
  11. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  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. flipper_http_deinit();
  45. return NULL;
  46. }
  47. flipper_http_deinit();
  48. FlipSocialFeedMini *feed_info = (FlipSocialFeedMini *)malloc(sizeof(FlipSocialFeedMini));
  49. if (!feed_info)
  50. {
  51. FURI_LOG_E(TAG, "Failed to allocate memory for feed_info");
  52. return NULL;
  53. }
  54. int feed_count = 0;
  55. // Iterate through the feed array
  56. for (int i = 0; i < MAX_FEED_ITEMS; i++)
  57. {
  58. // Parse each item in the array
  59. FuriString *item = get_json_array_value_furi("feed", i, feed_data);
  60. if (item == NULL)
  61. {
  62. break;
  63. }
  64. // Extract individual fields from the JSON object
  65. FuriString *username = get_json_value_furi("username", item);
  66. FuriString *message = get_json_value_furi("message", item);
  67. FuriString *flipped = get_json_value_furi("flipped", item);
  68. FuriString *flips = get_json_value_furi("flip_count", item);
  69. FuriString *id = get_json_value_furi("id", item);
  70. if (username == NULL || message == NULL || flipped == NULL || id == NULL)
  71. {
  72. FURI_LOG_E(TAG, "Failed to parse item fields.");
  73. furi_string_free(item);
  74. furi_string_free(username);
  75. furi_string_free(message);
  76. furi_string_free(flipped);
  77. furi_string_free(flips);
  78. furi_string_free(id);
  79. continue;
  80. }
  81. if (!flip_social_save_post((char *)furi_string_get_cstr(id), (char *)furi_string_get_cstr(item)))
  82. {
  83. FURI_LOG_E(TAG, "Failed to save post.");
  84. furi_string_free(item);
  85. furi_string_free(username);
  86. furi_string_free(message);
  87. furi_string_free(flipped);
  88. furi_string_free(flips);
  89. furi_string_free(id);
  90. continue;
  91. }
  92. feed_count++;
  93. feed_info->ids[i] = atoi(furi_string_get_cstr(id));
  94. // Furi_string_free allocated memory
  95. furi_string_free(item);
  96. furi_string_free(username);
  97. furi_string_free(message);
  98. furi_string_free(flipped);
  99. furi_string_free(flips);
  100. furi_string_free(id);
  101. }
  102. // Store the number of feed items
  103. feed_info->count = feed_count;
  104. feed_info->index = 0;
  105. furi_string_free(feed_data);
  106. return feed_info;
  107. }
  108. bool flip_social_load_feed_post(int post_id)
  109. {
  110. char file_path[128];
  111. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/feed/feed_post_%d.json", post_id);
  112. // load the received data from the saved file
  113. FuriString *feed_data = flipper_http_load_from_file(file_path);
  114. if (feed_data == NULL)
  115. {
  116. FURI_LOG_E(TAG, "Failed to load received data from file.");
  117. return false;
  118. }
  119. // Parse the feed post
  120. if (!flip_feed_item)
  121. {
  122. flip_feed_item = (FlipSocialFeedItem *)malloc(sizeof(FlipSocialFeedItem));
  123. if (flip_feed_item == NULL)
  124. {
  125. FURI_LOG_E(TAG, "Failed to allocate memory for feed post.");
  126. furi_string_free(feed_data);
  127. return false;
  128. }
  129. }
  130. // Extract individual fields from the JSON object
  131. FuriString *username = get_json_value_furi("username", feed_data);
  132. FuriString *message = get_json_value_furi("message", feed_data);
  133. FuriString *flipped = get_json_value_furi("flipped", feed_data);
  134. FuriString *flips = get_json_value_furi("flip_count", feed_data);
  135. FuriString *id = get_json_value_furi("id", feed_data);
  136. if (username == NULL || message == NULL || flipped == NULL || id == NULL)
  137. {
  138. FURI_LOG_E(TAG, "Failed to parse item fields.");
  139. furi_string_free(username);
  140. furi_string_free(message);
  141. furi_string_free(flipped);
  142. furi_string_free(flips);
  143. furi_string_free(id);
  144. furi_string_free(feed_data);
  145. return false;
  146. }
  147. // Safely copy strings with bounds checking
  148. snprintf(flip_feed_item->username, MAX_USER_LENGTH, "%s", furi_string_get_cstr(username));
  149. snprintf(flip_feed_item->message, MAX_MESSAGE_LENGTH, "%s", furi_string_get_cstr(message));
  150. // Store boolean and integer values
  151. flip_feed_item->is_flipped = strstr(furi_string_get_cstr(flipped), "true") != NULL;
  152. flip_feed_item->id = atoi(furi_string_get_cstr(id));
  153. flip_feed_item->flips = atoi(furi_string_get_cstr(flips));
  154. // Free allocated memory
  155. furi_string_free(username);
  156. furi_string_free(message);
  157. furi_string_free(flipped);
  158. furi_string_free(flips);
  159. furi_string_free(id);
  160. furi_string_free(feed_data);
  161. return true;
  162. }
  163. bool flip_social_load_initial_feed(bool alloc_http)
  164. {
  165. if (fhttp.state == INACTIVE)
  166. {
  167. FURI_LOG_E(TAG, "HTTP state is INACTIVE");
  168. return false;
  169. }
  170. Loading *loading;
  171. int32_t loading_view_id = 987654321; // Random ID
  172. loading = loading_alloc();
  173. if (!loading)
  174. {
  175. FURI_LOG_E(TAG, "Failed to set loading screen");
  176. return false;
  177. }
  178. view_dispatcher_add_view(app_instance->view_dispatcher, loading_view_id, loading_get_view(loading));
  179. view_dispatcher_switch_to_view(app_instance->view_dispatcher, loading_view_id);
  180. if (flip_social_get_feed(alloc_http)) // start the async request
  181. {
  182. furi_timer_start(fhttp.get_timeout_timer, TIMEOUT_DURATION_TICKS);
  183. fhttp.state = RECEIVING;
  184. }
  185. else
  186. {
  187. FURI_LOG_E(HTTP_TAG, "Failed to send request");
  188. fhttp.state = ISSUE;
  189. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  190. view_dispatcher_remove_view(app_instance->view_dispatcher, loading_view_id);
  191. loading_free(loading);
  192. return false;
  193. }
  194. while (fhttp.state == RECEIVING && furi_timer_is_running(fhttp.get_timeout_timer) > 0)
  195. {
  196. // Wait for the request to be received
  197. furi_delay_ms(100);
  198. }
  199. furi_timer_stop(fhttp.get_timeout_timer);
  200. // load feed info
  201. flip_feed_info = flip_social_parse_json_feed();
  202. if (!flip_feed_info || flip_feed_info->count < 1)
  203. {
  204. FURI_LOG_E(TAG, "Failed to parse JSON feed");
  205. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  206. view_dispatcher_remove_view(app_instance->view_dispatcher, loading_view_id);
  207. loading_free(loading);
  208. return false;
  209. }
  210. // load the current feed post
  211. if (!flip_social_load_feed_post(flip_feed_info->ids[flip_feed_info->index]))
  212. {
  213. FURI_LOG_E(TAG, "Failed to load feed post");
  214. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  215. view_dispatcher_remove_view(app_instance->view_dispatcher, loading_view_id);
  216. loading_free(loading);
  217. return false;
  218. }
  219. if (!feed_dialog_alloc())
  220. {
  221. FURI_LOG_E(TAG, "Failed to allocate feed dialog");
  222. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  223. view_dispatcher_remove_view(app_instance->view_dispatcher, loading_view_id);
  224. loading_free(loading);
  225. return false;
  226. }
  227. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewFeedDialog);
  228. view_dispatcher_remove_view(app_instance->view_dispatcher, loading_view_id);
  229. loading_free(loading);
  230. return true;
  231. }