flip_social_explore.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. #include "flip_social_explore.h"
  2. FlipSocialModel *flip_social_explore_alloc(void)
  3. {
  4. if (!app_instance)
  5. {
  6. return NULL;
  7. }
  8. if (!app_instance->submenu_explore)
  9. {
  10. if (!easy_flipper_set_submenu(&app_instance->submenu_explore, FlipSocialViewLoggedInExploreSubmenu, "Explore", flip_social_callback_to_submenu_logged_in, &app_instance->view_dispatcher))
  11. {
  12. return NULL;
  13. }
  14. }
  15. // Allocate memory for each username only if not already allocated
  16. FlipSocialModel *explore = malloc(sizeof(FlipSocialModel));
  17. if (explore == NULL)
  18. {
  19. FURI_LOG_E(TAG, "Failed to allocate memory for explore model.");
  20. return NULL;
  21. }
  22. for (size_t i = 0; i < MAX_EXPLORE_USERS; i++)
  23. {
  24. if (explore->usernames[i] == NULL)
  25. {
  26. explore->usernames[i] = malloc(MAX_USER_LENGTH);
  27. if (explore->usernames[i] == NULL)
  28. {
  29. FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
  30. return NULL; // Return false on memory allocation failure
  31. }
  32. }
  33. }
  34. return explore;
  35. }
  36. void flip_social_free_explore(void)
  37. {
  38. if (!app_instance)
  39. {
  40. return;
  41. }
  42. if (app_instance->submenu_explore)
  43. {
  44. submenu_free(app_instance->submenu_explore);
  45. app_instance->submenu_explore = NULL;
  46. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoggedInExploreSubmenu);
  47. }
  48. if (!flip_social_explore)
  49. {
  50. return;
  51. }
  52. free(flip_social_explore);
  53. flip_social_explore = NULL;
  54. }
  55. // for now we're just listing the current users
  56. // as the feed is upgraded, then we can port more to the explore view
  57. bool flip_social_get_explore(void)
  58. {
  59. if (fhttp.state == INACTIVE)
  60. {
  61. FURI_LOG_E(TAG, "HTTP state is INACTIVE");
  62. return false;
  63. }
  64. char *keyword = !app_instance->explore_logged_in || strlen(app_instance->explore_logged_in) == 0 ? "a" : app_instance->explore_logged_in;
  65. snprintf(
  66. fhttp.file_path,
  67. sizeof(fhttp.file_path),
  68. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/%s.json",
  69. keyword);
  70. fhttp.save_received_data = true;
  71. auth_headers_alloc();
  72. char url[256];
  73. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/user/explore/%s/%d/", keyword, MAX_EXPLORE_USERS);
  74. if (!flipper_http_get_request_with_headers(url, auth_headers))
  75. {
  76. FURI_LOG_E(TAG, "Failed to send HTTP request for explore");
  77. fhttp.state = ISSUE;
  78. return false;
  79. }
  80. fhttp.state = RECEIVING;
  81. return true;
  82. }
  83. bool flip_social_get_explore_2(void)
  84. {
  85. if (fhttp.state == INACTIVE)
  86. {
  87. FURI_LOG_E(TAG, "HTTP state is INACTIVE");
  88. return false;
  89. }
  90. char *keyword = !app_instance->message_users_logged_in || strlen(app_instance->message_users_logged_in) == 0 ? "a" : app_instance->message_users_logged_in;
  91. snprintf(
  92. fhttp.file_path,
  93. sizeof(fhttp.file_path),
  94. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/%s.json",
  95. keyword);
  96. fhttp.save_received_data = true;
  97. auth_headers_alloc();
  98. char url[256];
  99. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/user/explore/%s/%d/", keyword, MAX_EXPLORE_USERS);
  100. if (!flipper_http_get_request_with_headers(url, auth_headers))
  101. {
  102. FURI_LOG_E(TAG, "Failed to send HTTP request for explore");
  103. fhttp.state = ISSUE;
  104. return false;
  105. }
  106. fhttp.state = RECEIVING;
  107. return true;
  108. }
  109. bool flip_social_parse_json_explore()
  110. {
  111. // load the received data from the saved file
  112. FuriString *user_data = flipper_http_load_from_file(fhttp.file_path);
  113. if (user_data == NULL)
  114. {
  115. FURI_LOG_E(TAG, "Failed to load received data from file.");
  116. return false;
  117. }
  118. char *data_cstr = (char *)furi_string_get_cstr(user_data);
  119. if (data_cstr == NULL)
  120. {
  121. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  122. furi_string_free(user_data);
  123. return false;
  124. }
  125. // Allocate memory for each username only if not already allocated
  126. flip_social_explore = flip_social_explore_alloc();
  127. if (flip_social_explore == NULL)
  128. {
  129. FURI_LOG_E(TAG, "Failed to allocate memory for explore usernames.");
  130. furi_string_free(user_data);
  131. free(data_cstr);
  132. return false;
  133. }
  134. // Initialize explore count
  135. flip_social_explore->count = 0;
  136. // set submenu
  137. submenu_reset(app_instance->submenu_explore);
  138. submenu_set_header(app_instance->submenu_explore, "Explore");
  139. // Parse the JSON array of usernames
  140. for (size_t i = 0; i < MAX_EXPLORE_USERS; i++)
  141. {
  142. char *username = get_json_array_value("users", i, data_cstr, 64); // currently its 53 tokens (with max explore users at 50)
  143. if (username == NULL)
  144. {
  145. break;
  146. }
  147. snprintf(flip_social_explore->usernames[i], MAX_USER_LENGTH, "%s", username);
  148. submenu_add_item(app_instance->submenu_explore, flip_social_explore->usernames[i], FlipSocialSubmenuExploreIndexStartIndex + i, flip_social_callback_submenu_choices, app_instance);
  149. flip_social_explore->count++;
  150. free(username);
  151. }
  152. free(data_cstr);
  153. furi_string_free(user_data);
  154. return flip_social_explore->count > 0;
  155. }