flip_social_explore.c 4.2 KB

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