flip_social_explore.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #ifndef FLIP_SOCIAL_EXPLORE_H
  2. #define FLIP_SOCIAL_EXPLORE_H
  3. // for now we're just listing the current users
  4. // as the feed is upgraded, then we can port more to the explore view
  5. bool flip_social_get_explore()
  6. {
  7. // will return true unless the devboard is not connected
  8. bool success = flipper_http_get_request_with_headers("https://www.flipsocial.net/api/user/users/", "{\"Content-Type\":\"application/json\"}");
  9. if (!success)
  10. {
  11. FURI_LOG_E(TAG, "Failed to send HTTP request for explore");
  12. return false;
  13. }
  14. fhttp.state = RECEIVING;
  15. return true;
  16. }
  17. bool flip_social_parse_json_explore()
  18. {
  19. if (fhttp.received_data == NULL)
  20. {
  21. FURI_LOG_E(TAG, "No data received.");
  22. return false;
  23. }
  24. // Remove newlines
  25. char *pos = fhttp.received_data;
  26. while ((pos = strchr(pos, '\n')) != NULL)
  27. {
  28. *pos = ' ';
  29. }
  30. // Initialize explore count
  31. flip_social_explore.count = 0;
  32. // Extract the users array from the JSON
  33. char *json_users = get_json_value("users", fhttp.received_data, MAX_TOKENS);
  34. if (json_users == NULL)
  35. {
  36. FURI_LOG_E(TAG, "Failed to parse users array.");
  37. return false;
  38. }
  39. // Manual tokenization for comma-separated values
  40. char *start = json_users + 1; // Skip the opening bracket
  41. char *end;
  42. while ((end = strchr(start, ',')) != NULL)
  43. {
  44. *end = '\0'; // Null-terminate the current token
  45. // Remove the quotes
  46. if (*start == '"')
  47. {
  48. start++;
  49. }
  50. if (*(end - 1) == '"')
  51. {
  52. *(end - 1) = '\0';
  53. }
  54. // Allocate memory for the username
  55. size_t length = strlen(start) + 1;
  56. flip_social_explore.usernames[flip_social_explore.count] = malloc(length);
  57. if (flip_social_explore.usernames[flip_social_explore.count] == NULL)
  58. {
  59. FURI_LOG_E(TAG, "Memory allocation failed.");
  60. return false;
  61. }
  62. // Copy the username to the allocated memory
  63. strncpy(flip_social_explore.usernames[flip_social_explore.count], start, length);
  64. flip_social_explore.count++;
  65. start = end + 1; // Move to the next token
  66. }
  67. // Handle the last token
  68. if (*start != '\0')
  69. {
  70. // Remove the quotes
  71. if (*start == '"')
  72. {
  73. start++;
  74. }
  75. // Skip the closing bracket
  76. if (*(start + strlen(start) - 1) == ']')
  77. {
  78. *(start + strlen(start) - 1) = '\0';
  79. }
  80. // Remove the quotes
  81. if (*(start + strlen(start) - 1) == '"')
  82. {
  83. *(start + strlen(start) - 1) = '\0';
  84. }
  85. // Allocate memory for the username
  86. size_t length = strlen(start) + 1;
  87. flip_social_explore.usernames[flip_social_explore.count] = malloc(length);
  88. if (flip_social_explore.usernames[flip_social_explore.count] == NULL)
  89. {
  90. FURI_LOG_E(TAG, "Memory allocation failed.");
  91. return false;
  92. }
  93. // Copy the username to the allocated memory
  94. strncpy(flip_social_explore.usernames[flip_social_explore.count], start, length);
  95. flip_social_explore.count++;
  96. }
  97. // Add submenu items for the users
  98. submenu_reset(app_instance->submenu_explore);
  99. submenu_set_header(app_instance->submenu_explore, "Explore");
  100. for (uint32_t i = 0; i < flip_social_explore.count; i++)
  101. {
  102. submenu_add_item(app_instance->submenu_explore, flip_social_explore.usernames[i], FlipSocialSubmenuExploreIndexStartIndex + i, flip_social_callback_submenu_choices, app_instance);
  103. }
  104. // Free the json_users
  105. free(json_users);
  106. return true;
  107. }
  108. #endif // FLIP_SOCIAL_EXPLORE_H