flip_social_explore.c 4.9 KB

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