flip_social_feed.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. if (!app_instance)
  110. {
  111. FURI_LOG_E(TAG, "FlipSocialApp is NULL");
  112. return false;
  113. }
  114. // Get the feed from the server
  115. if (app_instance->login_username_logged_out == NULL)
  116. {
  117. FURI_LOG_E(TAG, "Username is NULL");
  118. return false;
  119. }
  120. snprintf(
  121. fhttp.file_path,
  122. sizeof(fhttp.file_path),
  123. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/feed.txt");
  124. fhttp.save_received_data = true;
  125. auth_headers_alloc();
  126. char command[96];
  127. snprintf(command, 96, "https://www.flipsocial.net/api/feed/40/%s/extended/", app_instance->login_username_logged_out);
  128. if (!flipper_http_get_request_with_headers(command, auth_headers))
  129. {
  130. FURI_LOG_E(TAG, "Failed to send HTTP request for feed");
  131. fhttp.state = ISSUE;
  132. return false;
  133. }
  134. fhttp.state = RECEIVING;
  135. return true;
  136. }
  137. bool flip_social_parse_json_feed()
  138. {
  139. // load the received data from the saved file
  140. FuriString *feed_data = flipper_http_load_from_file(fhttp.file_path);
  141. if (feed_data == NULL)
  142. {
  143. FURI_LOG_E(TAG, "Failed to load received data from file.");
  144. return false;
  145. }
  146. char *data_cstr = (char *)furi_string_get_cstr(feed_data);
  147. if (data_cstr == NULL)
  148. {
  149. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  150. furi_string_free(feed_data);
  151. return false;
  152. }
  153. // Allocate memory for each feed item if not already allocated
  154. flip_social_feed = flip_social_feed_alloc();
  155. if (flip_social_feed == NULL)
  156. {
  157. return false;
  158. }
  159. // Remove newlines
  160. char *pos = data_cstr;
  161. while ((pos = strchr(pos, '\n')) != NULL)
  162. {
  163. *pos = ' ';
  164. }
  165. // Initialize feed count
  166. flip_social_feed->count = 0;
  167. // Iterate through the feed array
  168. for (int i = 0; i < MAX_FEED_ITEMS; i++)
  169. {
  170. // Parse each item in the array
  171. char *item = get_json_array_value("feed", i, data_cstr, MAX_TOKENS);
  172. if (item == NULL)
  173. {
  174. break;
  175. }
  176. // Extract individual fields from the JSON object
  177. char *username = get_json_value("username", item, MAX_TOKENS);
  178. char *message = get_json_value("message", item, MAX_TOKENS);
  179. char *flipped = get_json_value("flipped", item, MAX_TOKENS);
  180. char *flips = get_json_value("flip_count", item, MAX_TOKENS);
  181. char *id = get_json_value("id", item, MAX_TOKENS);
  182. if (username == NULL || message == NULL || flipped == NULL || id == NULL)
  183. {
  184. FURI_LOG_E(TAG, "Failed to parse item fields.");
  185. free(item);
  186. free(username);
  187. free(message);
  188. free(flipped);
  189. free(flips);
  190. free(id);
  191. continue;
  192. }
  193. // Safely copy strings with bounds checking
  194. snprintf(flip_social_feed->usernames[i], MAX_USER_LENGTH, "%s", username);
  195. snprintf(flip_social_feed->messages[i], MAX_MESSAGE_LENGTH, "%s", message);
  196. // Store boolean and integer values
  197. flip_social_feed->is_flipped[i] = strstr(flipped, "true") != NULL;
  198. flip_social_feed->ids[i] = atoi(id);
  199. flip_social_feed->flips[i] = atoi(flips);
  200. flip_social_feed->count++;
  201. // Free allocated memory
  202. free(item);
  203. free(username);
  204. free(message);
  205. free(flipped);
  206. free(flips);
  207. free(id);
  208. }
  209. furi_string_free(feed_data);
  210. free(data_cstr);
  211. return flip_social_feed->count > 0;
  212. }