flip_social_friends.c 5.4 KB

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