flip_social_feed.h 5.5 KB

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