flip_social_feed.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. #include "flip_social_feed.h"
  2. // Set failure FlipSocialFeed object
  3. bool flip_social_temp_feed()
  4. {
  5. if (flip_social_feed == NULL)
  6. {
  7. flip_social_feed = malloc(sizeof(FlipSocialFeed));
  8. if (flip_social_feed == NULL)
  9. {
  10. FURI_LOG_E(TAG, "Failed to allocate memory for feed");
  11. return false;
  12. }
  13. }
  14. for (int i = 0; i < 3; i++)
  15. {
  16. if (flip_social_feed->usernames[i] == NULL)
  17. {
  18. flip_social_feed->usernames[i] = malloc(MAX_USER_LENGTH);
  19. if (flip_social_feed->usernames[i] == NULL)
  20. {
  21. FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
  22. return false;
  23. }
  24. }
  25. if (flip_social_feed->messages[i] == NULL)
  26. {
  27. flip_social_feed->messages[i] = malloc(MAX_MESSAGE_LENGTH);
  28. if (flip_social_feed->messages[i] == NULL)
  29. {
  30. FURI_LOG_E(TAG, "Failed to allocate memory for message %zu", i);
  31. return false;
  32. }
  33. }
  34. }
  35. flip_social_feed->usernames[0] = "JBlanked";
  36. flip_social_feed->usernames[1] = "FlipperKing";
  37. flip_social_feed->usernames[2] = "FlipperQueen";
  38. //
  39. flip_social_feed->messages[0] = "Welcome. This is a temp message. Either the feed didn't load or there was a server error.";
  40. flip_social_feed->messages[1] = "I am the Chosen Flipper.";
  41. flip_social_feed->messages[2] = "No one can flip like me.";
  42. //
  43. flip_social_feed->is_flipped[0] = true;
  44. flip_social_feed->is_flipped[1] = false;
  45. flip_social_feed->is_flipped[2] = true;
  46. //
  47. flip_social_feed->ids[0] = 0;
  48. flip_social_feed->ids[1] = 1;
  49. flip_social_feed->ids[2] = 2;
  50. //
  51. flip_social_feed->flips[0] = 51;
  52. flip_social_feed->flips[1] = 8;
  53. flip_social_feed->flips[2] = 23;
  54. //
  55. flip_social_feed->count = 3;
  56. flip_social_feed->index = 0;
  57. return true;
  58. }
  59. // Allocate memory for each feed item if not already allocated
  60. FlipSocialFeed *flip_social_feed_alloc()
  61. {
  62. // Initialize the feed
  63. FlipSocialFeed *feed = (FlipSocialFeed *)malloc(sizeof(FlipSocialFeed));
  64. if (!feed)
  65. {
  66. FURI_LOG_E(TAG, "Failed to allocate memory for feed");
  67. return feed;
  68. }
  69. for (size_t i = 0; i < MAX_FEED_ITEMS; i++)
  70. {
  71. if (feed->usernames[i] == NULL)
  72. {
  73. feed->usernames[i] = malloc(MAX_USER_LENGTH);
  74. if (feed->usernames[i] == NULL)
  75. {
  76. FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
  77. return NULL;
  78. }
  79. }
  80. if (feed->messages[i] == NULL)
  81. {
  82. feed->messages[i] = malloc(MAX_MESSAGE_LENGTH);
  83. if (feed->messages[i] == NULL)
  84. {
  85. FURI_LOG_E(TAG, "Failed to allocate memory for message %zu", i);
  86. return NULL;
  87. }
  88. }
  89. }
  90. return feed;
  91. }
  92. void flip_social_free_feed()
  93. {
  94. if (!flip_social_feed)
  95. {
  96. FURI_LOG_E(TAG, "Feed model is NULL");
  97. return;
  98. }
  99. for (uint32_t i = 0; i < flip_social_feed->count; i++)
  100. {
  101. if (flip_social_feed->usernames[i])
  102. {
  103. free(flip_social_feed->usernames[i]);
  104. }
  105. }
  106. }
  107. bool flip_social_get_feed()
  108. {
  109. // Get the feed from the server
  110. if (app_instance->login_username_logged_out == NULL)
  111. {
  112. FURI_LOG_E(TAG, "Username is NULL");
  113. return false;
  114. }
  115. char command[128];
  116. snprintf(
  117. fhttp.file_path,
  118. sizeof(fhttp.file_path),
  119. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_rss/feed.txt");
  120. fhttp.save_received_data = true;
  121. char *headers = jsmn("Content-Type", "application/json");
  122. snprintf(command, 128, "https://www.flipsocial.net/api/feed/40/%s/extended/", app_instance->login_username_logged_out);
  123. bool success = flipper_http_get_request_with_headers(command, headers);
  124. free(headers);
  125. if (!success)
  126. {
  127. FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
  128. fhttp.state = ISSUE;
  129. return false;
  130. }
  131. fhttp.state = RECEIVING;
  132. return true;
  133. }
  134. bool flip_social_parse_json_feed()
  135. {
  136. // load the received data from the saved file
  137. FuriString *feed_data = flipper_http_load_from_file(fhttp.file_path);
  138. if (feed_data == NULL)
  139. {
  140. FURI_LOG_E(TAG, "Failed to load received data from file.");
  141. return false;
  142. }
  143. char *data_cstr = (char *)furi_string_get_cstr(feed_data);
  144. if (data_cstr == NULL)
  145. {
  146. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  147. furi_string_free(feed_data);
  148. return false;
  149. }
  150. // Allocate memory for each feed item if not already allocated
  151. flip_social_feed = flip_social_feed_alloc();
  152. if (flip_social_feed == NULL)
  153. {
  154. return false;
  155. }
  156. // Remove newlines
  157. char *pos = data_cstr;
  158. while ((pos = strchr(pos, '\n')) != NULL)
  159. {
  160. *pos = ' ';
  161. }
  162. // Initialize feed count
  163. flip_social_feed->count = 0;
  164. // Iterate through the feed array
  165. for (int i = 0; i < MAX_FEED_ITEMS; i++)
  166. {
  167. // Parse each item in the array
  168. char *item = get_json_array_value("feed", i, data_cstr, MAX_TOKENS);
  169. if (item == NULL)
  170. {
  171. break;
  172. }
  173. // Extract individual fields from the JSON object
  174. char *username = get_json_value("username", item, MAX_TOKENS);
  175. char *message = get_json_value("message", item, MAX_TOKENS);
  176. char *flipped = get_json_value("flipped", item, MAX_TOKENS);
  177. char *flips = get_json_value("flip_count", item, MAX_TOKENS);
  178. char *id = get_json_value("id", item, MAX_TOKENS);
  179. if (username == NULL || message == NULL || flipped == NULL || id == NULL)
  180. {
  181. FURI_LOG_E(TAG, "Failed to parse item fields.");
  182. free(item);
  183. free(username);
  184. free(message);
  185. free(flipped);
  186. free(flips);
  187. free(id);
  188. continue;
  189. }
  190. // Safely copy strings with bounds checking
  191. snprintf(flip_social_feed->usernames[i], MAX_USER_LENGTH, "%s", username);
  192. snprintf(flip_social_feed->messages[i], MAX_MESSAGE_LENGTH, "%s", message);
  193. // Store boolean and integer values
  194. flip_social_feed->is_flipped[i] = strstr(flipped, "true") != NULL;
  195. flip_social_feed->ids[i] = atoi(id);
  196. flip_social_feed->flips[i] = atoi(flips);
  197. flip_social_feed->count++;
  198. // Free allocated memory
  199. free(item);
  200. free(username);
  201. free(message);
  202. free(flipped);
  203. free(flips);
  204. free(id);
  205. }
  206. furi_string_free(feed_data);
  207. free(data_cstr);
  208. return flip_social_feed->count > 0;
  209. }