flip_social_friends.h 3.9 KB

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