flip_social_explore.c 4.9 KB

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