flip_social_friends.c 4.0 KB

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