flip_social_explore.c 4.5 KB

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