flip_social_friends.c 5.3 KB

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