feed.c 8.9 KB

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