flip_social_friends.c 5.3 KB

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