flip_social_friends.c 5.3 KB

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