flip_social_explore.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "flip_social_explore.h"
  2. FlipSocialModel* flip_social_explore_alloc() {
  3. // Allocate memory for each username only if not already allocated
  4. FlipSocialModel* explore = malloc(sizeof(FlipSocialModel));
  5. if(explore == NULL) {
  6. FURI_LOG_E(TAG, "Failed to allocate memory for explore model.");
  7. return NULL;
  8. }
  9. for(size_t i = 0; i < MAX_EXPLORE_USERS; i++) {
  10. if(explore->usernames[i] == NULL) {
  11. explore->usernames[i] = malloc(MAX_USER_LENGTH);
  12. if(explore->usernames[i] == NULL) {
  13. FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
  14. return NULL; // Return false on memory allocation failure
  15. }
  16. }
  17. }
  18. return explore;
  19. }
  20. void flip_social_free_explore() {
  21. if(!flip_social_explore) {
  22. FURI_LOG_E(TAG, "Explore model is NULL");
  23. return;
  24. }
  25. for(int i = 0; i < flip_social_explore->count; i++) {
  26. if(flip_social_explore->usernames[i]) {
  27. free(flip_social_explore->usernames[i]);
  28. }
  29. }
  30. }
  31. // for now we're just listing the current users
  32. // as the feed is upgraded, then we can port more to the explore view
  33. bool flip_social_get_explore() {
  34. snprintf(
  35. fhttp.file_path,
  36. sizeof(fhttp.file_path),
  37. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/users.txt");
  38. fhttp.save_received_data = true;
  39. auth_headers_alloc();
  40. if(!flipper_http_get_request_with_headers(
  41. "https://www.flipsocial.net/api/user/users/", auth_headers)) {
  42. FURI_LOG_E(TAG, "Failed to send HTTP request for explore");
  43. fhttp.state = ISSUE;
  44. return false;
  45. }
  46. fhttp.state = RECEIVING;
  47. return true;
  48. }
  49. bool flip_social_parse_json_explore() {
  50. // load the received data from the saved file
  51. FuriString* user_data = flipper_http_load_from_file(fhttp.file_path);
  52. if(user_data == NULL) {
  53. FURI_LOG_E(TAG, "Failed to load received data from file.");
  54. return false;
  55. }
  56. char* data_cstr = (char*)furi_string_get_cstr(user_data);
  57. if(data_cstr == NULL) {
  58. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  59. furi_string_free(user_data);
  60. return false;
  61. }
  62. // Allocate memory for each username only if not already allocated
  63. flip_social_explore = flip_social_explore_alloc();
  64. if(flip_social_explore == NULL) {
  65. FURI_LOG_E(TAG, "Failed to allocate memory for explore usernames.");
  66. furi_string_free(user_data);
  67. free(data_cstr);
  68. return false;
  69. }
  70. // Remove newlines
  71. char* pos = data_cstr;
  72. while((pos = strchr(pos, '\n')) != NULL) {
  73. *pos = ' ';
  74. }
  75. // Initialize explore count
  76. flip_social_explore->count = 0;
  77. // Extract the users array from the JSON
  78. char* json_users = get_json_value("users", data_cstr, MAX_TOKENS);
  79. if(json_users == NULL) {
  80. FURI_LOG_E(TAG, "Failed to parse users array.");
  81. furi_string_free(user_data);
  82. free(data_cstr);
  83. return false;
  84. }
  85. // Manual tokenization for comma-separated values
  86. char* start = json_users + 1; // Skip the opening bracket
  87. char* end;
  88. while((end = strchr(start, ',')) != NULL && flip_social_explore->count < MAX_EXPLORE_USERS) {
  89. *end = '\0'; // Null-terminate the current token
  90. // Remove quotes
  91. if(*start == '"') start++;
  92. if(*(end - 1) == '"') *(end - 1) = '\0';
  93. // Copy username to pre-allocated memory
  94. snprintf(
  95. flip_social_explore->usernames[flip_social_explore->count],
  96. MAX_USER_LENGTH,
  97. "%s",
  98. start);
  99. flip_social_explore->count++;
  100. start = end + 1;
  101. }
  102. // Handle the last token
  103. if(*start != '\0' && flip_social_explore->count < MAX_EXPLORE_USERS) {
  104. if(*start == '"') start++;
  105. if(*(start + strlen(start) - 1) == ']') *(start + strlen(start) - 1) = '\0';
  106. if(*(start + strlen(start) - 1) == '"') *(start + strlen(start) - 1) = '\0';
  107. snprintf(
  108. flip_social_explore->usernames[flip_social_explore->count],
  109. MAX_USER_LENGTH,
  110. "%s",
  111. start);
  112. flip_social_explore->count++;
  113. }
  114. // Add submenu items for the users
  115. submenu_reset(app_instance->submenu_explore);
  116. submenu_set_header(app_instance->submenu_explore, "Explore");
  117. for(int i = 0; i < flip_social_explore->count; i++) {
  118. submenu_add_item(
  119. app_instance->submenu_explore,
  120. flip_social_explore->usernames[i],
  121. FlipSocialSubmenuExploreIndexStartIndex + i,
  122. flip_social_callback_submenu_choices,
  123. app_instance);
  124. }
  125. // Free the json_users
  126. free(json_users);
  127. free(start);
  128. free(end);
  129. furi_string_free(user_data);
  130. free(data_cstr);
  131. return flip_social_explore->count > 0;
  132. }