flip_social_explore.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. snprintf(
  46. fhttp.file_path,
  47. sizeof(fhttp.file_path),
  48. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/users.json");
  49. fhttp.save_received_data = true;
  50. auth_headers_alloc();
  51. if (!flipper_http_get_request_with_headers("https://www.flipsocial.net/api/user/users/", auth_headers))
  52. {
  53. FURI_LOG_E(TAG, "Failed to send HTTP request for explore");
  54. fhttp.state = ISSUE;
  55. return false;
  56. }
  57. fhttp.state = RECEIVING;
  58. return true;
  59. }
  60. bool flip_social_parse_json_explore()
  61. {
  62. // load the received data from the saved file
  63. FuriString *user_data = flipper_http_load_from_file(fhttp.file_path);
  64. if (user_data == NULL)
  65. {
  66. FURI_LOG_E(TAG, "Failed to load received data from file.");
  67. return false;
  68. }
  69. char *data_cstr = (char *)furi_string_get_cstr(user_data);
  70. if (data_cstr == NULL)
  71. {
  72. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  73. furi_string_free(user_data);
  74. return false;
  75. }
  76. // Allocate memory for each username only if not already allocated
  77. flip_social_explore = flip_social_explore_alloc();
  78. if (flip_social_explore == NULL)
  79. {
  80. FURI_LOG_E(TAG, "Failed to allocate memory for explore usernames.");
  81. furi_string_free(user_data);
  82. free(data_cstr);
  83. return false;
  84. }
  85. // Remove newlines
  86. char *pos = data_cstr;
  87. while ((pos = strchr(pos, '\n')) != NULL)
  88. {
  89. *pos = ' ';
  90. }
  91. // Initialize explore count
  92. flip_social_explore->count = 0;
  93. // Extract the users array from the JSON
  94. char *json_users = get_json_value("users", data_cstr, MAX_TOKENS); // currently it's about 480 tokens
  95. if (json_users == NULL)
  96. {
  97. FURI_LOG_E(TAG, "Failed to parse users array.");
  98. furi_string_free(user_data);
  99. free(data_cstr);
  100. return false;
  101. }
  102. // Manual tokenization for comma-separated values
  103. char *start = json_users + 1; // Skip the opening bracket
  104. char *end;
  105. while ((end = strchr(start, ',')) != NULL && flip_social_explore->count < MAX_EXPLORE_USERS)
  106. {
  107. *end = '\0'; // Null-terminate the current token
  108. // Remove quotes
  109. if (*start == '"')
  110. start++;
  111. if (*(end - 1) == '"')
  112. *(end - 1) = '\0';
  113. // Copy username to pre-allocated memory
  114. snprintf(flip_social_explore->usernames[flip_social_explore->count], MAX_USER_LENGTH, "%s", start);
  115. flip_social_explore->count++;
  116. start = end + 1;
  117. }
  118. // Handle the last token
  119. if (*start != '\0' && flip_social_explore->count < MAX_EXPLORE_USERS)
  120. {
  121. if (*start == '"')
  122. start++;
  123. if (*(start + strlen(start) - 1) == ']')
  124. *(start + strlen(start) - 1) = '\0';
  125. if (*(start + strlen(start) - 1) == '"')
  126. *(start + strlen(start) - 1) = '\0';
  127. snprintf(flip_social_explore->usernames[flip_social_explore->count], MAX_USER_LENGTH, "%s", start);
  128. flip_social_explore->count++;
  129. }
  130. // Add submenu items for the users
  131. submenu_reset(app_instance->submenu_explore);
  132. submenu_set_header(app_instance->submenu_explore, "Explore");
  133. for (int i = 0; i < flip_social_explore->count; i++)
  134. {
  135. submenu_add_item(app_instance->submenu_explore, flip_social_explore->usernames[i], FlipSocialSubmenuExploreIndexStartIndex + i, flip_social_callback_submenu_choices, app_instance);
  136. }
  137. // Free the json_users
  138. free(json_users);
  139. free(start);
  140. free(end);
  141. furi_string_free(user_data);
  142. free(data_cstr);
  143. return flip_social_explore->count > 0;
  144. }