flip_social_explore.c 4.9 KB

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