explore.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. // create the explore directory
  18. if (!flip_social_subfolder_mkdir("explore"))
  19. {
  20. FURI_LOG_E(TAG, "Failed to create explore directory");
  21. return false;
  22. }
  23. char *keyword = !app_instance->explore_logged_in || strlen(app_instance->explore_logged_in) == 0 ? "a" : app_instance->explore_logged_in;
  24. snprintf(
  25. fhttp->file_path,
  26. sizeof(fhttp->file_path),
  27. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/explore/%s.json",
  28. keyword);
  29. fhttp->save_received_data = true;
  30. alloc_headers();
  31. char url[256];
  32. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/user/explore/%s/%d/", keyword, MAX_EXPLORE_USERS);
  33. if (!flipper_http_request(fhttp, GET, url, auth_headers, NULL))
  34. {
  35. FURI_LOG_E(TAG, "Failed to send HTTP request for explore");
  36. fhttp->state = ISSUE;
  37. return false;
  38. }
  39. fhttp->state = RECEIVING;
  40. return true;
  41. }
  42. bool explore_fetch_2(FlipperHTTP *fhttp)
  43. {
  44. if (!app_instance)
  45. {
  46. FURI_LOG_E(TAG, "App instance is NULL");
  47. return false;
  48. }
  49. if (!fhttp)
  50. {
  51. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  52. return false;
  53. }
  54. // create the explore directory
  55. if (!flip_social_subfolder_mkdir("explore"))
  56. {
  57. FURI_LOG_E(TAG, "Failed to create explore directory");
  58. return false;
  59. }
  60. char *keyword = !app_instance->message_users_logged_in || strlen(app_instance->message_users_logged_in) == 0 ? "a" : app_instance->message_users_logged_in;
  61. snprintf(
  62. fhttp->file_path,
  63. sizeof(fhttp->file_path),
  64. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/explore/%s.json",
  65. keyword);
  66. fhttp->save_received_data = true;
  67. alloc_headers();
  68. char url[256];
  69. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/user/explore/%s/%d/", keyword, MAX_EXPLORE_USERS);
  70. return flipper_http_request(fhttp, GET, url, auth_headers, NULL);
  71. }
  72. bool explore_parse_json(FlipperHTTP *fhttp)
  73. {
  74. if (!app_instance)
  75. {
  76. FURI_LOG_E(TAG, "App instance is NULL");
  77. return false;
  78. }
  79. if (!fhttp)
  80. {
  81. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  82. return false;
  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. return false;
  90. }
  91. // Allocate memory for each username only if not already allocated
  92. flip_social_explore = alloc_explore();
  93. if (flip_social_explore == NULL)
  94. {
  95. FURI_LOG_E(TAG, "Failed to allocate memory for explore usernames.");
  96. furi_string_free(user_data);
  97. return false;
  98. }
  99. // Initialize explore count
  100. flip_social_explore->count = 0;
  101. // set submenu
  102. submenu_reset(app_instance->submenu);
  103. submenu_set_header(app_instance->submenu, "Explore");
  104. // Parse the JSON array of usernames
  105. for (size_t i = 0; i < MAX_EXPLORE_USERS; i++)
  106. {
  107. FuriString *username = get_json_array_value_furi("users", i, user_data); // currently its 53 tokens (with max explore users at 50)
  108. if (username == NULL)
  109. {
  110. break;
  111. }
  112. snprintf(flip_social_explore->usernames[i], MAX_USER_LENGTH, "%s", furi_string_get_cstr(username));
  113. submenu_add_item(app_instance->submenu, flip_social_explore->usernames[i], FlipSocialSubmenuExploreIndexStartIndex + i, callback_submenu_choices, app_instance);
  114. flip_social_explore->count++;
  115. furi_string_free(username);
  116. }
  117. furi_string_free(user_data);
  118. return flip_social_explore->count > 0;
  119. }