flip_social_explore.c 4.5 KB

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