flip_social_feed.h 6.0 KB

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