friends.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // will return true unless the devboard is not connected
  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.json");
  26. fhttp->save_received_data = true;
  27. alloc_headers();
  28. snprintf(url, sizeof(url), "https://www.jblanked.com/flipper/api/user/friends/%s/", app_instance->login_username_logged_in);
  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. fhttp->state = RECEIVING;
  36. return true;
  37. }
  38. bool friends_update()
  39. {
  40. if (!app_instance)
  41. {
  42. FURI_LOG_E(TAG, "App instance is NULL");
  43. return false;
  44. }
  45. if (!app_instance->submenu)
  46. {
  47. FURI_LOG_E(TAG, "Friends submenu is NULL");
  48. return false;
  49. }
  50. if (!flip_social_friends)
  51. {
  52. FURI_LOG_E(TAG, "Friends model is NULL");
  53. return false;
  54. }
  55. // Add submenu items for the users
  56. submenu_reset(app_instance->submenu);
  57. submenu_set_header(app_instance->submenu, "Friends");
  58. for (int i = 0; i < flip_social_friends->count; i++)
  59. {
  60. submenu_add_item(app_instance->submenu, flip_social_friends->usernames[i], FlipSocialSubmenuLoggedInIndexFriendsStart + i, callback_submenu_choices, app_instance);
  61. }
  62. return true;
  63. }
  64. bool friends_parse_json(FlipperHTTP *fhttp)
  65. {
  66. if (!fhttp)
  67. {
  68. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  69. return false;
  70. }
  71. // load the received data from the saved file
  72. FuriString *friend_data = flipper_http_load_from_file(fhttp->file_path);
  73. if (friend_data == NULL)
  74. {
  75. FURI_LOG_E(TAG, "Failed to load received data from file.");
  76. return false;
  77. }
  78. // Allocate memory for each username only if not already allocated
  79. flip_social_friends = alloc_friends_model();
  80. if (flip_social_friends == NULL)
  81. {
  82. FURI_LOG_E(TAG, "Failed to allocate memory for friends usernames.");
  83. furi_string_free(friend_data);
  84. return false;
  85. }
  86. // Initialize friends count
  87. flip_social_friends->count = 0;
  88. // Reset the friends submenu
  89. submenu_reset(app_instance->submenu);
  90. submenu_set_header(app_instance->submenu, "Friends");
  91. // Extract the users array from the JSON
  92. for (int i = 0; i < MAX_FRIENDS; i++)
  93. {
  94. FuriString *friend = get_json_array_value_furi("friends", i, friend_data);
  95. if (friend == NULL)
  96. {
  97. FURI_LOG_E(TAG, "Failed to parse friend %d.", i);
  98. furi_string_free(friend_data);
  99. break;
  100. }
  101. snprintf(flip_social_friends->usernames[i], MAX_USER_LENGTH, "%s", furi_string_get_cstr(friend));
  102. submenu_add_item(app_instance->submenu, flip_social_friends->usernames[i], FlipSocialSubmenuLoggedInIndexFriendsStart + i, callback_submenu_choices, app_instance);
  103. flip_social_friends->count++;
  104. furi_string_free(friend);
  105. }
  106. furi_string_free(friend_data);
  107. return true;
  108. }