flip_social_explore.c 4.9 KB

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