feed.c 8.8 KB

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