flip_social_explore.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. FURI_LOG_E(TAG, "Explore model is NULL");
  30. return;
  31. }
  32. for (int i = 0; i < flip_social_explore->count; i++)
  33. {
  34. if (flip_social_explore->usernames[i])
  35. {
  36. free(flip_social_explore->usernames[i]);
  37. }
  38. }
  39. }
  40. // for now we're just listing the current users
  41. // as the feed is upgraded, then we can port more to the explore view
  42. bool flip_social_get_explore()
  43. {
  44. snprintf(
  45. fhttp.file_path,
  46. sizeof(fhttp.file_path),
  47. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/users.txt");
  48. fhttp.save_received_data = true;
  49. auth_headers_alloc();
  50. if (!flipper_http_get_request_with_headers("https://www.flipsocial.net/api/user/users/", auth_headers))
  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_parse_json_explore()
  60. {
  61. // load the received data from the saved file
  62. FuriString *user_data = flipper_http_load_from_file(fhttp.file_path);
  63. if (user_data == NULL)
  64. {
  65. FURI_LOG_E(TAG, "Failed to load received data from file.");
  66. return false;
  67. }
  68. char *data_cstr = (char *)furi_string_get_cstr(user_data);
  69. if (data_cstr == NULL)
  70. {
  71. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  72. furi_string_free(user_data);
  73. return false;
  74. }
  75. // Allocate memory for each username only if not already allocated
  76. flip_social_explore = flip_social_explore_alloc();
  77. if (flip_social_explore == NULL)
  78. {
  79. FURI_LOG_E(TAG, "Failed to allocate memory for explore usernames.");
  80. furi_string_free(user_data);
  81. free(data_cstr);
  82. return false;
  83. }
  84. // Remove newlines
  85. char *pos = data_cstr;
  86. while ((pos = strchr(pos, '\n')) != NULL)
  87. {
  88. *pos = ' ';
  89. }
  90. // Initialize explore count
  91. flip_social_explore->count = 0;
  92. // Extract the users array from the JSON
  93. char *json_users = get_json_value("users", data_cstr, MAX_TOKENS);
  94. if (json_users == NULL)
  95. {
  96. FURI_LOG_E(TAG, "Failed to parse users array.");
  97. furi_string_free(user_data);
  98. free(data_cstr);
  99. return false;
  100. }
  101. // Manual tokenization for comma-separated values
  102. char *start = json_users + 1; // Skip the opening bracket
  103. char *end;
  104. while ((end = strchr(start, ',')) != NULL && flip_social_explore->count < MAX_EXPLORE_USERS)
  105. {
  106. *end = '\0'; // Null-terminate the current token
  107. // Remove quotes
  108. if (*start == '"')
  109. start++;
  110. if (*(end - 1) == '"')
  111. *(end - 1) = '\0';
  112. // Copy username to pre-allocated memory
  113. snprintf(flip_social_explore->usernames[flip_social_explore->count], MAX_USER_LENGTH, "%s", start);
  114. flip_social_explore->count++;
  115. start = end + 1;
  116. }
  117. // Handle the last token
  118. if (*start != '\0' && flip_social_explore->count < MAX_EXPLORE_USERS)
  119. {
  120. if (*start == '"')
  121. start++;
  122. if (*(start + strlen(start) - 1) == ']')
  123. *(start + strlen(start) - 1) = '\0';
  124. if (*(start + strlen(start) - 1) == '"')
  125. *(start + strlen(start) - 1) = '\0';
  126. snprintf(flip_social_explore->usernames[flip_social_explore->count], MAX_USER_LENGTH, "%s", start);
  127. flip_social_explore->count++;
  128. }
  129. // Add submenu items for the users
  130. submenu_reset(app_instance->submenu_explore);
  131. submenu_set_header(app_instance->submenu_explore, "Explore");
  132. for (int i = 0; i < flip_social_explore->count; i++)
  133. {
  134. submenu_add_item(app_instance->submenu_explore, flip_social_explore->usernames[i], FlipSocialSubmenuExploreIndexStartIndex + i, flip_social_callback_submenu_choices, app_instance);
  135. }
  136. // Free the json_users
  137. free(json_users);
  138. free(start);
  139. free(end);
  140. furi_string_free(user_data);
  141. free(data_cstr);
  142. return flip_social_explore->count > 0;
  143. }