flip_social_friends.h 4.8 KB

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