flip_social_friends.h 4.8 KB

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