flip_social_feed.c 6.5 KB

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