feed.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #include <feed/feed.h>
  2. #include <flip_storage/flip_social_storage.h>
  3. #include <alloc/alloc.h>
  4. bool feed_fetch(FlipperHTTP *fhttp, int series_index)
  5. {
  6. if (!app_instance)
  7. {
  8. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  9. return false;
  10. }
  11. if (!fhttp)
  12. {
  13. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  14. return false;
  15. }
  16. // Get the feed from the server
  17. if (app_instance->login_username_logged_out == NULL)
  18. {
  19. FURI_LOG_E(TAG, "Username is NULL");
  20. return false;
  21. }
  22. snprintf(
  23. fhttp->file_path,
  24. sizeof(fhttp->file_path),
  25. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/feed.json");
  26. fhttp->save_received_data = true;
  27. alloc_headers();
  28. char command[96];
  29. if (strstr(flip_social_feed_type[flip_social_feed_type_index], "Global"))
  30. {
  31. snprintf(command, 96, "https://www.jblanked.com/flipper/api/feed/%d/%s/%d/max/series/", MAX_FEED_ITEMS, app_instance->login_username_logged_out, series_index);
  32. }
  33. else
  34. {
  35. snprintf(command, 96, "https://www.jblanked.com/flipper/api/feed/%d/%s/%d/max/friends/series/", MAX_FEED_ITEMS, app_instance->login_username_logged_out, series_index);
  36. }
  37. if (!flipper_http_request(fhttp, GET, command, auth_headers, NULL))
  38. {
  39. FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
  40. fhttp->state = ISSUE;
  41. return false;
  42. }
  43. fhttp->state = RECEIVING;
  44. return true;
  45. }
  46. FlipSocialFeedMini *feed_parse_json(FlipperHTTP *fhttp)
  47. {
  48. if (!fhttp)
  49. {
  50. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  51. return NULL;
  52. }
  53. // load the received data from the saved file
  54. FuriString *feed_data = flipper_http_load_from_file(fhttp->file_path);
  55. if (feed_data == NULL)
  56. {
  57. FURI_LOG_E(TAG, "Failed to load received data from file.");
  58. return NULL;
  59. }
  60. FlipSocialFeedMini *feed_info = (FlipSocialFeedMini *)malloc(sizeof(FlipSocialFeedMini));
  61. if (!feed_info)
  62. {
  63. FURI_LOG_E(TAG, "Failed to allocate memory for feed_info");
  64. return NULL;
  65. }
  66. int feed_count = 0;
  67. // Iterate through the feed array
  68. for (int i = 0; i < MAX_FEED_ITEMS; i++)
  69. {
  70. // Parse each item in the array
  71. FuriString *item = get_json_array_value_furi("feed", i, feed_data);
  72. if (item == NULL)
  73. {
  74. break;
  75. }
  76. // Extract individual fields from the JSON object
  77. FuriString *id = get_json_value_furi("id", item);
  78. if (id == NULL)
  79. {
  80. FURI_LOG_E(TAG, "Failed to parse item fields.");
  81. furi_string_free(item);
  82. if (id)
  83. furi_string_free(id);
  84. continue;
  85. }
  86. if (!flip_social_save_post(furi_string_get_cstr(id), furi_string_get_cstr(item)))
  87. {
  88. FURI_LOG_E(TAG, "Failed to save post.");
  89. furi_string_free(item);
  90. furi_string_free(id);
  91. continue;
  92. }
  93. feed_count++;
  94. feed_info->ids[i] = atoi(furi_string_get_cstr(id));
  95. // Furi_string_free allocated memory
  96. furi_string_free(item);
  97. furi_string_free(id);
  98. }
  99. // Store the number of feed items
  100. feed_info->count = feed_count;
  101. feed_info->index = 0;
  102. furi_string_free(feed_data);
  103. return feed_info;
  104. }
  105. bool feed_load_post(int post_id)
  106. {
  107. char file_path[128];
  108. snprintf(file_path, sizeof(file_path), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/feed/feed_post_%d.json", post_id);
  109. // load the received data from the saved file
  110. FuriString *feed_data = flipper_http_load_from_file(file_path);
  111. if (feed_data == NULL)
  112. {
  113. FURI_LOG_E(TAG, "Failed to load received data from file.");
  114. return false;
  115. }
  116. // Parse the feed post
  117. if (flip_feed_item)
  118. {
  119. free(flip_feed_item);
  120. }
  121. else
  122. {
  123. // first time
  124. save_char("series_index", "1");
  125. }
  126. flip_feed_item = (FlipSocialFeedItem *)malloc(sizeof(FlipSocialFeedItem));
  127. if (flip_feed_item == NULL)
  128. {
  129. FURI_LOG_E(TAG, "Failed to allocate memory for feed post.");
  130. furi_string_free(feed_data);
  131. return false;
  132. }
  133. // Extract individual fields from the JSON object
  134. FuriString *username = get_json_value_furi("username", feed_data);
  135. FuriString *message = get_json_value_furi("message", feed_data);
  136. FuriString *flipped = get_json_value_furi("flipped", feed_data);
  137. FuriString *flips = get_json_value_furi("flip_count", feed_data);
  138. FuriString *date_created = get_json_value_furi("date_created", feed_data);
  139. if (username == NULL || message == NULL || flipped == NULL || flips == NULL || date_created == NULL)
  140. {
  141. FURI_LOG_E(TAG, "Failed to parse item fields.");
  142. if (username)
  143. furi_string_free(username);
  144. if (message)
  145. furi_string_free(message);
  146. if (flipped)
  147. furi_string_free(flipped);
  148. if (flips)
  149. furi_string_free(flips);
  150. if (date_created)
  151. furi_string_free(date_created);
  152. furi_string_free(feed_data);
  153. return false;
  154. }
  155. // Safely copy strings with bounds checking
  156. snprintf(flip_feed_item->username, MAX_USER_LENGTH, "%s", furi_string_get_cstr(username));
  157. snprintf(flip_feed_item->message, MAX_MESSAGE_LENGTH, "%s", furi_string_get_cstr(message));
  158. snprintf(flip_feed_item->date_created, MAX_LINE_LENGTH, "%s", furi_string_get_cstr(date_created));
  159. // Store boolean and integer values
  160. flip_feed_item->is_flipped = strstr(furi_string_get_cstr(flipped), "true") ? true : false;
  161. flip_feed_item->id = post_id;
  162. flip_feed_item->flips = atoi(furi_string_get_cstr(flips));
  163. // Free allocated memory
  164. furi_string_free(username);
  165. furi_string_free(message);
  166. furi_string_free(flipped);
  167. furi_string_free(flips);
  168. furi_string_free(date_created);
  169. furi_string_free(feed_data);
  170. return true;
  171. }
  172. bool feed_load_initial_feed(FlipperHTTP *fhttp, int series_index)
  173. {
  174. if (!app_instance)
  175. {
  176. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  177. return false;
  178. }
  179. if (!fhttp)
  180. {
  181. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  182. return false;
  183. }
  184. if (fhttp->state == INACTIVE)
  185. {
  186. FURI_LOG_E(TAG, "HTTP state is INACTIVE");
  187. return false;
  188. }
  189. Loading *loading;
  190. int32_t loading_view_id = 987654321; // Random ID
  191. loading = loading_alloc();
  192. if (!loading)
  193. {
  194. FURI_LOG_E(TAG, "Failed to set loading screen");
  195. return false;
  196. }
  197. view_dispatcher_add_view(app_instance->view_dispatcher, loading_view_id, loading_get_view(loading));
  198. view_dispatcher_switch_to_view(app_instance->view_dispatcher, loading_view_id);
  199. if (feed_fetch(fhttp, series_index)) // start the async request
  200. {
  201. furi_timer_start(fhttp->get_timeout_timer, TIMEOUT_DURATION_TICKS);
  202. fhttp->state = RECEIVING;
  203. }
  204. else
  205. {
  206. FURI_LOG_E(HTTP_TAG, "Failed to send request");
  207. fhttp->state = ISSUE;
  208. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  209. view_dispatcher_remove_view(app_instance->view_dispatcher, loading_view_id);
  210. loading_free(loading);
  211. return false;
  212. }
  213. while (fhttp->state == RECEIVING && furi_timer_is_running(fhttp->get_timeout_timer) > 0)
  214. {
  215. // Wait for the request to be received
  216. furi_delay_ms(100);
  217. }
  218. furi_timer_stop(fhttp->get_timeout_timer);
  219. // load feed info
  220. flip_feed_info = feed_parse_json(fhttp);
  221. if (!flip_feed_info || flip_feed_info->count < 1)
  222. {
  223. FURI_LOG_E(TAG, "Failed to parse JSON feed");
  224. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  225. view_dispatcher_remove_view(app_instance->view_dispatcher, loading_view_id);
  226. loading_free(loading);
  227. return false;
  228. }
  229. // load the current feed post
  230. if (!feed_load_post(flip_feed_info->ids[flip_feed_info->index]))
  231. {
  232. FURI_LOG_E(TAG, "Failed to load feed post");
  233. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  234. view_dispatcher_remove_view(app_instance->view_dispatcher, loading_view_id);
  235. loading_free(loading);
  236. return false;
  237. }
  238. if (!alloc_feed_view())
  239. {
  240. FURI_LOG_E(TAG, "Failed to allocate feed dialog");
  241. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInSubmenu);
  242. view_dispatcher_remove_view(app_instance->view_dispatcher, loading_view_id);
  243. loading_free(loading);
  244. return false;
  245. }
  246. view_dispatcher_switch_to_view(app_instance->view_dispatcher, FlipSocialViewLoggedInFeed);
  247. view_dispatcher_remove_view(app_instance->view_dispatcher, loading_view_id);
  248. loading_free(loading);
  249. return true;
  250. }