flip_social_feed.h 6.1 KB

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