flip_social_friends.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #include "flip_social_friends.h"
  2. FlipSocialModel *flip_social_friends_alloc()
  3. {
  4. // Allocate memory for each username only if not already allocated
  5. FlipSocialModel *friends = malloc(sizeof(FlipSocialModel));
  6. for (size_t i = 0; i < MAX_FRIENDS; i++)
  7. {
  8. if (friends->usernames[i] == NULL)
  9. {
  10. friends->usernames[i] = malloc(MAX_USER_LENGTH);
  11. if (friends->usernames[i] == NULL)
  12. {
  13. FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
  14. return NULL; // Return false on memory allocation failure
  15. }
  16. }
  17. }
  18. return friends;
  19. }
  20. void flip_social_free_friends()
  21. {
  22. if (!flip_social_friends)
  23. {
  24. return;
  25. }
  26. for (int i = 0; i < flip_social_friends->count; i++)
  27. {
  28. if (flip_social_friends->usernames[i])
  29. {
  30. free(flip_social_friends->usernames[i]);
  31. }
  32. }
  33. free(flip_social_friends);
  34. flip_social_friends = NULL;
  35. }
  36. // for now we're just listing the current users
  37. // as the feed is upgraded, then we can port more to the friends view
  38. bool flip_social_get_friends()
  39. {
  40. if (!app_instance)
  41. {
  42. FURI_LOG_E(TAG, "App instance is NULL");
  43. return false;
  44. }
  45. // will return true unless the devboard is not connected
  46. char url[100];
  47. snprintf(
  48. fhttp.file_path,
  49. sizeof(fhttp.file_path),
  50. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/friends.json");
  51. fhttp.save_received_data = true;
  52. auth_headers_alloc();
  53. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/user/friends/%s/", app_instance->login_username_logged_in);
  54. if (!flipper_http_get_request_with_headers(url, auth_headers))
  55. {
  56. FURI_LOG_E(TAG, "Failed to send HTTP request for friends");
  57. fhttp.state = ISSUE;
  58. return false;
  59. }
  60. fhttp.state = RECEIVING;
  61. return true;
  62. }
  63. bool flip_social_update_friends()
  64. {
  65. if (!app_instance->submenu_friends)
  66. {
  67. FURI_LOG_E(TAG, "Friends submenu is NULL");
  68. return false;
  69. }
  70. if (!flip_social_friends)
  71. {
  72. FURI_LOG_E(TAG, "Friends model is NULL");
  73. return false;
  74. }
  75. // Add submenu items for the users
  76. submenu_reset(app_instance->submenu_friends);
  77. submenu_set_header(app_instance->submenu_friends, "Friends");
  78. for (int i = 0; i < flip_social_friends->count; i++)
  79. {
  80. submenu_add_item(app_instance->submenu_friends, flip_social_friends->usernames[i], FlipSocialSubmenuLoggedInIndexFriendsStart + i, flip_social_callback_submenu_choices, app_instance);
  81. }
  82. return true;
  83. }
  84. bool flip_social_parse_json_friends()
  85. {
  86. // load the received data from the saved file
  87. FuriString *friend_data = flipper_http_load_from_file(fhttp.file_path);
  88. if (friend_data == NULL)
  89. {
  90. FURI_LOG_E(TAG, "Failed to load received data from file.");
  91. return false;
  92. }
  93. char *data_cstr = (char *)furi_string_get_cstr(friend_data);
  94. if (data_cstr == NULL)
  95. {
  96. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  97. furi_string_free(friend_data);
  98. return false;
  99. }
  100. // Allocate memory for each username only if not already allocated
  101. flip_social_friends = flip_social_friends_alloc();
  102. if (flip_social_friends == NULL)
  103. {
  104. FURI_LOG_E(TAG, "Failed to allocate memory for friends usernames.");
  105. furi_string_free(friend_data);
  106. free(data_cstr);
  107. return false;
  108. }
  109. // Remove newlines
  110. char *pos = data_cstr;
  111. while ((pos = strchr(pos, '\n')) != NULL)
  112. {
  113. *pos = ' ';
  114. }
  115. // Initialize friends count
  116. flip_social_friends->count = 0;
  117. // Extract the users array from the JSON
  118. char *json_users = get_json_value("friends", data_cstr, MAX_TOKENS);
  119. if (json_users == NULL)
  120. {
  121. FURI_LOG_E(TAG, "Failed to parse friends array.");
  122. furi_string_free(friend_data);
  123. free(data_cstr);
  124. flip_social_free_friends();
  125. return false;
  126. }
  127. // Manual tokenization for comma-separated values
  128. char *start = json_users + 1; // Skip the opening bracket
  129. char *end;
  130. while ((end = strchr(start, ',')) != NULL && flip_social_friends->count < MAX_FRIENDS)
  131. {
  132. *end = '\0'; // Null-terminate the current token
  133. // Remove quotes
  134. if (*start == '"')
  135. start++;
  136. if (*(end - 1) == '"')
  137. *(end - 1) = '\0';
  138. // Copy username to pre-allocated memory
  139. snprintf(flip_social_friends->usernames[flip_social_friends->count], MAX_USER_LENGTH, "%s", start);
  140. flip_social_friends->count++;
  141. start = end + 1;
  142. }
  143. // Handle the last token
  144. if (*start != '\0' && flip_social_friends->count < MAX_FRIENDS)
  145. {
  146. if (*start == '"')
  147. start++;
  148. if (*(start + strlen(start) - 1) == ']')
  149. *(start + strlen(start) - 1) = '\0';
  150. if (*(start + strlen(start) - 1) == '"')
  151. *(start + strlen(start) - 1) = '\0';
  152. snprintf(flip_social_friends->usernames[flip_social_friends->count], MAX_USER_LENGTH, "%s", start);
  153. flip_social_friends->count++;
  154. }
  155. // Add submenu items for the friends
  156. if (!flip_social_update_friends())
  157. {
  158. FURI_LOG_E(TAG, "Failed to update friends submenu");
  159. furi_string_free(friend_data);
  160. free(data_cstr);
  161. free(json_users);
  162. free(start);
  163. free(end);
  164. return false;
  165. }
  166. furi_string_free(friend_data);
  167. free(data_cstr);
  168. free(json_users);
  169. free(start);
  170. free(end);
  171. return true;
  172. }