flip_social_friends.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #include "flip_social_friends.h"
  2. FlipSocialModel *flip_social_friends_alloc()
  3. {
  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. {
  8. if (friends->usernames[i] == NULL)
  9. {
  10. friends->usernames[i] = malloc(MAX_USER_LENGTH);
  11. if (friends->usernames[i] == NULL)
  12. {
  13. FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
  14. return NULL; // Return false on memory allocation failure
  15. }
  16. }
  17. }
  18. return friends;
  19. }
  20. // for now we're just listing the current users
  21. // as the feed is upgraded, then we can port more to the friends view
  22. bool flip_social_get_friends()
  23. {
  24. if (!app_instance)
  25. {
  26. FURI_LOG_E(TAG, "App instance is NULL");
  27. return false;
  28. }
  29. // will return true unless the devboard is not connected
  30. char url[100];
  31. snprintf(
  32. fhttp.file_path,
  33. sizeof(fhttp.file_path),
  34. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/friends.json");
  35. fhttp.save_received_data = true;
  36. auth_headers_alloc();
  37. snprintf(url, sizeof(url), "https://www.flipsocial.net/api/user/friends/%s/", app_instance->login_username_logged_in);
  38. if (!flipper_http_get_request_with_headers(url, auth_headers))
  39. {
  40. FURI_LOG_E(TAG, "Failed to send HTTP request for friends");
  41. fhttp.state = ISSUE;
  42. return false;
  43. }
  44. fhttp.state = RECEIVING;
  45. return true;
  46. }
  47. bool flip_social_update_friends()
  48. {
  49. if (!app_instance->submenu_friends)
  50. {
  51. FURI_LOG_E(TAG, "Friends submenu is NULL");
  52. return false;
  53. }
  54. if (!flip_social_friends)
  55. {
  56. FURI_LOG_E(TAG, "Friends model is NULL");
  57. return false;
  58. }
  59. // Add submenu items for the users
  60. submenu_reset(app_instance->submenu_friends);
  61. submenu_set_header(app_instance->submenu_friends, "Friends");
  62. for (int i = 0; i < flip_social_friends->count; i++)
  63. {
  64. submenu_add_item(app_instance->submenu_friends, flip_social_friends->usernames[i], FlipSocialSubmenuLoggedInIndexFriendsStart + i, flip_social_callback_submenu_choices, app_instance);
  65. }
  66. return true;
  67. }
  68. bool flip_social_parse_json_friends()
  69. {
  70. // load the received data from the saved file
  71. FuriString *friend_data = flipper_http_load_from_file(fhttp.file_path);
  72. if (friend_data == NULL)
  73. {
  74. FURI_LOG_E(TAG, "Failed to load received data from file.");
  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. furi_string_free(friend_data);
  83. return false;
  84. }
  85. // Initialize friends count
  86. flip_social_friends->count = 0;
  87. // Reset the friends submenu
  88. submenu_reset(app_instance->submenu_friends);
  89. submenu_set_header(app_instance->submenu_friends, "Friends");
  90. // Extract the users array from the JSON
  91. for (int i = 0; i < MAX_FRIENDS; i++)
  92. {
  93. char *friend = get_json_array_value("friends", i, (char *)furi_string_get_cstr(friend_data), 256);
  94. if (friend == NULL)
  95. {
  96. FURI_LOG_E(TAG, "Failed to parse friend %d.", i);
  97. furi_string_free(friend_data);
  98. break;
  99. }
  100. snprintf(flip_social_friends->usernames[i], MAX_USER_LENGTH, "%s", friend);
  101. submenu_add_item(app_instance->submenu_friends, flip_social_friends->usernames[i], FlipSocialSubmenuLoggedInIndexFriendsStart + i, flip_social_callback_submenu_choices, app_instance);
  102. flip_social_friends->count++;
  103. free(friend);
  104. }
  105. furi_string_free(friend_data);
  106. return true;
  107. }