flip_social_explore.c 4.7 KB

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