flip_social_friends.c 3.5 KB

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