friends.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 directory
  18. Storage *storage = furi_record_open(RECORD_STORAGE);
  19. storage_common_mkdir(storage, STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/friends");
  20. furi_record_close(RECORD_STORAGE);
  21. char url[100];
  22. snprintf(
  23. fhttp->file_path,
  24. sizeof(fhttp->file_path),
  25. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/friends/friends.json");
  26. fhttp->save_received_data = true;
  27. alloc_headers();
  28. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/user/friends/%s/%d/", app_instance->login_username_logged_in, MAX_FRIENDS);
  29. if (!flipper_http_request(fhttp, GET, url, auth_headers, NULL))
  30. {
  31. FURI_LOG_E(TAG, "Failed to send HTTP request for friends");
  32. fhttp->state = ISSUE;
  33. return false;
  34. }
  35. return true;
  36. }
  37. bool friends_update()
  38. {
  39. if (!app_instance)
  40. {
  41. FURI_LOG_E(TAG, "App instance is NULL");
  42. return false;
  43. }
  44. if (!app_instance->submenu)
  45. {
  46. FURI_LOG_E(TAG, "Friends submenu is NULL");
  47. return false;
  48. }
  49. if (!flip_social_friends)
  50. {
  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);
  56. submenu_set_header(app_instance->submenu, "Friends");
  57. for (int i = 0; i < flip_social_friends->count; i++)
  58. {
  59. submenu_add_item(app_instance->submenu, flip_social_friends->usernames[i], FlipSocialSubmenuLoggedInIndexFriendsStart + i, callback_submenu_choices, app_instance);
  60. }
  61. return true;
  62. }
  63. bool friends_parse_json(FlipperHTTP *fhttp)
  64. {
  65. if (!fhttp)
  66. {
  67. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  68. return false;
  69. }
  70. if (!app_instance)
  71. {
  72. FURI_LOG_E(TAG, "App instance is NULL");
  73. return false;
  74. }
  75. if (!app_instance->submenu)
  76. {
  77. FURI_LOG_E(TAG, "Friends submenu is NULL");
  78. return false;
  79. }
  80. // load the received data from the saved file
  81. FuriString *friend_data = flipper_http_load_from_file(fhttp->file_path);
  82. if (friend_data == NULL)
  83. {
  84. FURI_LOG_E(TAG, "Failed to load received data from file.");
  85. return false;
  86. }
  87. // Allocate memory for each username only if not already allocated
  88. flip_social_friends = alloc_friends_model();
  89. if (flip_social_friends == NULL)
  90. {
  91. FURI_LOG_E(TAG, "Failed to allocate memory for friends usernames.");
  92. furi_string_free(friend_data);
  93. return false;
  94. }
  95. // Initialize friends count
  96. flip_social_friends->count = 0;
  97. // Reset the friends submenu
  98. submenu_reset(app_instance->submenu);
  99. submenu_set_header(app_instance->submenu, "Friends");
  100. // Extract the users array from the JSON
  101. for (int i = 0; i < MAX_FRIENDS; i++)
  102. {
  103. FuriString *friend = get_json_array_value_furi("friends", i, friend_data);
  104. if (friend == NULL)
  105. {
  106. FURI_LOG_E(TAG, "Failed to parse friend %d.", i);
  107. furi_string_free(friend_data);
  108. break;
  109. }
  110. snprintf(flip_social_friends->usernames[i], MAX_USER_LENGTH, "%s", furi_string_get_cstr(friend));
  111. submenu_add_item(app_instance->submenu, flip_social_friends->usernames[i], FlipSocialSubmenuLoggedInIndexFriendsStart + i, callback_submenu_choices, app_instance);
  112. flip_social_friends->count++;
  113. furi_string_free(friend);
  114. }
  115. furi_string_free(friend_data);
  116. return true;
  117. }