friends.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #include <friends/friends.h>
  2. #include <alloc/alloc.h>
  3. // for now we're just listing the current users
  4. // as the feed is upgraded, then we can port more to the friends view
  5. bool friends_fetch(FlipperHTTP *fhttp)
  6. {
  7. if (!app_instance)
  8. {
  9. FURI_LOG_E(TAG, "App instance is NULL");
  10. return false;
  11. }
  12. if (!fhttp)
  13. {
  14. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  15. return false;
  16. }
  17. // create the friends directory
  18. if (!flip_social_subfolder_mkdir("friends"))
  19. {
  20. FURI_LOG_E(TAG, "Failed to create friends directory");
  21. return false;
  22. }
  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/friends.json");
  28. fhttp->save_received_data = true;
  29. alloc_headers();
  30. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/user/friends/%s/%d/", app_instance->login_username_logged_in, MAX_FRIENDS);
  31. if (!flipper_http_request(fhttp, GET, url, auth_headers, NULL))
  32. {
  33. FURI_LOG_E(TAG, "Failed to send HTTP request for friends");
  34. fhttp->state = ISSUE;
  35. return false;
  36. }
  37. return true;
  38. }
  39. bool friends_update()
  40. {
  41. if (!app_instance)
  42. {
  43. FURI_LOG_E(TAG, "App instance is NULL");
  44. return false;
  45. }
  46. if (!app_instance->submenu)
  47. {
  48. FURI_LOG_E(TAG, "Friends submenu is NULL");
  49. return false;
  50. }
  51. if (!flip_social_friends)
  52. {
  53. FURI_LOG_E(TAG, "Friends model is NULL");
  54. return false;
  55. }
  56. // Add submenu items for the users
  57. submenu_reset(app_instance->submenu);
  58. submenu_set_header(app_instance->submenu, "Friends");
  59. for (int i = 0; i < flip_social_friends->count; i++)
  60. {
  61. submenu_add_item(app_instance->submenu, flip_social_friends->usernames[i], FlipSocialSubmenuLoggedInIndexFriendsStart + i, callback_submenu_choices, app_instance);
  62. }
  63. return true;
  64. }
  65. bool friends_parse_json(FlipperHTTP *fhttp)
  66. {
  67. if (!fhttp)
  68. {
  69. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  70. return false;
  71. }
  72. if (!app_instance)
  73. {
  74. FURI_LOG_E(TAG, "App instance is NULL");
  75. return false;
  76. }
  77. if (!app_instance->submenu)
  78. {
  79. FURI_LOG_E(TAG, "Friends submenu is NULL");
  80. return false;
  81. }
  82. // load the received data from the saved file
  83. FuriString *friend_data = flipper_http_load_from_file(fhttp->file_path);
  84. if (friend_data == NULL)
  85. {
  86. FURI_LOG_E(TAG, "Failed to load received data from file.");
  87. return false;
  88. }
  89. // Allocate memory for each username only if not already allocated
  90. flip_social_friends = alloc_friends_model();
  91. if (flip_social_friends == NULL)
  92. {
  93. FURI_LOG_E(TAG, "Failed to allocate memory for friends usernames.");
  94. furi_string_free(friend_data);
  95. return false;
  96. }
  97. // Initialize friends count
  98. flip_social_friends->count = 0;
  99. // Reset the friends submenu
  100. submenu_reset(app_instance->submenu);
  101. submenu_set_header(app_instance->submenu, "Friends");
  102. // Extract the users array from the JSON
  103. for (int i = 0; i < MAX_FRIENDS; i++)
  104. {
  105. FuriString *friend = get_json_array_value_furi("friends", i, friend_data);
  106. if (friend == NULL)
  107. {
  108. FURI_LOG_E(TAG, "Failed to parse friend %d.", i);
  109. furi_string_free(friend_data);
  110. break;
  111. }
  112. snprintf(flip_social_friends->usernames[i], MAX_USER_LENGTH, "%s", furi_string_get_cstr(friend));
  113. submenu_add_item(app_instance->submenu, flip_social_friends->usernames[i], FlipSocialSubmenuLoggedInIndexFriendsStart + i, callback_submenu_choices, app_instance);
  114. flip_social_friends->count++;
  115. furi_string_free(friend);
  116. }
  117. furi_string_free(friend_data);
  118. return true;
  119. }