explore.c 4.2 KB

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