flip_social_friends.c 5.5 KB

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