flip_social_friends.h 4.8 KB

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