flip_social_feed.c 5.8 KB

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