flip_social_messages.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. #include "flip_social_messages.h"
  2. FlipSocialModel2 *flip_social_messages_alloc()
  3. {
  4. // Allocate memory for each username only if not already allocated
  5. FlipSocialModel2 *users = malloc(sizeof(FlipSocialModel2));
  6. if (users == NULL)
  7. {
  8. FURI_LOG_E(TAG, "Failed to allocate memory for message users");
  9. return NULL;
  10. }
  11. return users;
  12. }
  13. FlipSocialMessage *flip_social_user_messages_alloc()
  14. {
  15. // Allocate memory for each username only if not already allocated
  16. FlipSocialMessage *messages = malloc(sizeof(FlipSocialMessage));
  17. if (messages == NULL)
  18. {
  19. FURI_LOG_E(TAG, "Failed to allocate memory for messages");
  20. return NULL;
  21. }
  22. return messages;
  23. }
  24. void flip_social_free_message_users()
  25. {
  26. if (flip_social_message_users == NULL)
  27. {
  28. return;
  29. }
  30. free(flip_social_message_users);
  31. flip_social_message_users = NULL;
  32. }
  33. void flip_social_free_messages()
  34. {
  35. if (flip_social_messages == NULL)
  36. {
  37. return;
  38. }
  39. free(flip_social_messages);
  40. flip_social_messages = NULL;
  41. }
  42. bool flip_social_update_messages_submenu()
  43. {
  44. if (!app_instance)
  45. {
  46. FURI_LOG_E(TAG, "App instance is NULL");
  47. return false;
  48. }
  49. if (app_instance->submenu == NULL)
  50. {
  51. FURI_LOG_E(TAG, "Submenu is NULL");
  52. return false;
  53. }
  54. if (flip_social_message_users == NULL)
  55. {
  56. FURI_LOG_E(TAG, "Message users model is NULL");
  57. return false;
  58. }
  59. submenu_reset(app_instance->submenu);
  60. submenu_set_header(app_instance->submenu, "Messages");
  61. submenu_add_item(app_instance->submenu, "[New Message]", FlipSocialSubmenuLoggedInIndexMessagesNewMessage, flip_social_callback_submenu_choices, app_instance);
  62. for (int i = 0; i < flip_social_message_users->count; i++)
  63. {
  64. submenu_add_item(app_instance->submenu, flip_social_message_users->usernames[i], FlipSocialSubmenuLoggedInIndexMessagesUsersStart + i, flip_social_callback_submenu_choices, app_instance);
  65. }
  66. return true;
  67. }
  68. bool flip_social_update_submenu_user_choices()
  69. {
  70. if (app_instance == NULL)
  71. {
  72. FURI_LOG_E(TAG, "App instance is NULL");
  73. return false;
  74. }
  75. if (app_instance->submenu == NULL)
  76. {
  77. FURI_LOG_E(TAG, "Submenu is NULL");
  78. return false;
  79. }
  80. if (flip_social_explore == NULL)
  81. {
  82. FURI_LOG_E(TAG, "Explore model is NULL");
  83. return false;
  84. }
  85. submenu_reset(app_instance->submenu);
  86. submenu_set_header(app_instance->submenu, "Users");
  87. for (int i = 0; i < flip_social_explore->count; i++)
  88. {
  89. submenu_add_item(app_instance->submenu, flip_social_explore->usernames[i], FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart + i, flip_social_callback_submenu_choices, app_instance);
  90. }
  91. return true;
  92. }
  93. // Get all the users that have sent messages to the logged in user
  94. bool flip_social_get_message_users()
  95. {
  96. if (app_instance->login_username_logged_out == NULL)
  97. {
  98. FURI_LOG_E(TAG, "Username is NULL");
  99. return false;
  100. }
  101. if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  102. {
  103. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  104. return false;
  105. }
  106. char directory[128];
  107. snprintf(directory, sizeof(directory), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/messages");
  108. // Create the directory
  109. Storage *storage = furi_record_open(RECORD_STORAGE);
  110. storage_common_mkdir(storage, directory);
  111. char command[128];
  112. snprintf(
  113. fhttp.file_path,
  114. sizeof(fhttp.file_path),
  115. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/messages/message_users.json");
  116. fhttp.save_received_data = true;
  117. auth_headers_alloc();
  118. snprintf(command, 128, "https://www.flipsocial.net/api/messages/%s/get/list/%d/", app_instance->login_username_logged_out, MAX_MESSAGE_USERS);
  119. if (!flipper_http_get_request_with_headers(command, auth_headers))
  120. {
  121. FURI_LOG_E(TAG, "Failed to send HTTP request for messages");
  122. fhttp.state = ISSUE;
  123. flipper_http_deinit();
  124. return false;
  125. }
  126. fhttp.state = RECEIVING;
  127. return true;
  128. }
  129. // Get all the messages between the logged in user and the selected user
  130. bool flip_social_get_messages_with_user()
  131. {
  132. if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  133. {
  134. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  135. return false;
  136. }
  137. if (app_instance->login_username_logged_out == NULL)
  138. {
  139. FURI_LOG_E(TAG, "Username is NULL");
  140. return false;
  141. }
  142. if (strlen(flip_social_message_users->usernames[flip_social_message_users->index]) == 0)
  143. {
  144. FURI_LOG_E(TAG, "Username is NULL");
  145. return false;
  146. }
  147. char directory[128];
  148. snprintf(directory, sizeof(directory), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/messages");
  149. // Create the directory
  150. Storage *storage = furi_record_open(RECORD_STORAGE);
  151. storage_common_mkdir(storage, directory);
  152. char command[256];
  153. snprintf(
  154. fhttp.file_path,
  155. sizeof(fhttp.file_path),
  156. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/messages/%s_messages.json",
  157. flip_social_message_users->usernames[flip_social_message_users->index]);
  158. fhttp.save_received_data = true;
  159. auth_headers_alloc();
  160. snprintf(command, sizeof(command), "https://www.flipsocial.net/api/messages/%s/get/%s/%d/", app_instance->login_username_logged_out, flip_social_message_users->usernames[flip_social_message_users->index], MAX_MESSAGES);
  161. if (!flipper_http_get_request_with_headers(command, auth_headers))
  162. {
  163. FURI_LOG_E(TAG, "Failed to send HTTP request for messages");
  164. fhttp.state = ISSUE;
  165. return false;
  166. }
  167. fhttp.state = RECEIVING;
  168. return true;
  169. }
  170. // Parse the users that have sent messages to the logged-in user
  171. bool flip_social_parse_json_message_users()
  172. {
  173. // load the received data from the saved file
  174. FuriString *message_data = flipper_http_load_from_file(fhttp.file_path);
  175. if (message_data == NULL)
  176. {
  177. FURI_LOG_E(TAG, "Failed to load received data from file.");
  178. flipper_http_deinit();
  179. return false;
  180. }
  181. flipper_http_deinit();
  182. // Allocate memory for each username only if not already allocated
  183. flip_social_message_users = flip_social_messages_alloc();
  184. if (flip_social_message_users == NULL)
  185. {
  186. FURI_LOG_E(TAG, "Failed to allocate memory for message users.");
  187. furi_string_free(message_data);
  188. return false;
  189. }
  190. // Initialize message users count
  191. flip_social_message_users->count = 0;
  192. for (int i = 0; i < MAX_MESSAGE_USERS; i++)
  193. {
  194. FuriString *user = get_json_array_value_furi("users", i, message_data);
  195. if (user == NULL)
  196. {
  197. break;
  198. }
  199. snprintf(flip_social_message_users->usernames[i], MAX_USER_LENGTH, "%s", furi_string_get_cstr(user));
  200. flip_social_message_users->count++;
  201. furi_string_free(user);
  202. }
  203. // Add submenu items for the users
  204. flip_social_update_messages_submenu();
  205. // Free the JSON data
  206. furi_string_free(message_data);
  207. return true;
  208. }
  209. // Parse the users that the logged in user can message
  210. bool flip_social_parse_json_message_user_choices()
  211. {
  212. // load the received data from the saved file
  213. FuriString *user_data = flipper_http_load_from_file(fhttp.file_path);
  214. if (user_data == NULL)
  215. {
  216. FURI_LOG_E(TAG, "Failed to load received data from file.");
  217. flipper_http_deinit();
  218. return false;
  219. }
  220. flipper_http_deinit();
  221. // Allocate memory for each username only if not already allocated
  222. flip_social_explore = flip_social_explore_alloc();
  223. if (flip_social_explore == NULL)
  224. {
  225. FURI_LOG_E(TAG, "Failed to allocate memory for explore usernames.");
  226. furi_string_free(user_data);
  227. return false;
  228. }
  229. // Initialize explore count
  230. flip_social_explore->count = 0;
  231. for (int i = 0; i < MAX_MESSAGE_USERS; i++)
  232. {
  233. FuriString *user = get_json_array_value_furi("users", i, user_data);
  234. if (user == NULL)
  235. {
  236. break;
  237. }
  238. snprintf(flip_social_explore->usernames[i], MAX_USER_LENGTH, "%s", furi_string_get_cstr(user));
  239. flip_social_explore->count++;
  240. furi_string_free(user);
  241. }
  242. // Add submenu items for the users
  243. flip_social_update_submenu_user_choices();
  244. // Free the JSON data
  245. furi_string_free(user_data);
  246. return flip_social_explore->count > 0;
  247. }
  248. // parse messages between the logged in user and the selected user
  249. bool flip_social_parse_json_messages()
  250. {
  251. // load the received data from the saved file
  252. FuriString *message_data = flipper_http_load_from_file(fhttp.file_path);
  253. if (message_data == NULL)
  254. {
  255. FURI_LOG_E(TAG, "Failed to load received data from file.");
  256. flipper_http_deinit();
  257. return false;
  258. }
  259. flipper_http_deinit();
  260. // Allocate memory for each message only if not already allocated
  261. flip_social_messages = flip_social_user_messages_alloc();
  262. if (!flip_social_messages)
  263. {
  264. FURI_LOG_E(TAG, "Failed to allocate memory for messages.");
  265. furi_string_free(message_data);
  266. return false;
  267. }
  268. // Initialize messages count
  269. flip_social_messages->count = 0;
  270. // Iterate through the messages array
  271. for (int i = 0; i < MAX_MESSAGES; i++)
  272. {
  273. // Parse each item in the array
  274. FuriString *item = get_json_array_value_furi("conversations", i, message_data);
  275. if (item == NULL)
  276. {
  277. break;
  278. }
  279. // Extract individual fields from the JSON object
  280. FuriString *sender = get_json_value_furi("sender", item);
  281. FuriString *content = get_json_value_furi("content", item);
  282. if (sender == NULL || content == NULL)
  283. {
  284. FURI_LOG_E(TAG, "Failed to parse item fields.");
  285. furi_string_free(item);
  286. continue;
  287. }
  288. // Store parsed values in pre-allocated memory
  289. snprintf(flip_social_messages->usernames[i], MAX_USER_LENGTH, "%s", furi_string_get_cstr(sender));
  290. snprintf(flip_social_messages->messages[i], MAX_MESSAGE_LENGTH, "%s", furi_string_get_cstr(content));
  291. flip_social_messages->count++;
  292. furi_string_free(item);
  293. furi_string_free(sender);
  294. furi_string_free(content);
  295. }
  296. if (!messages_dialog_alloc(true))
  297. {
  298. FURI_LOG_E(TAG, "Failed to allocate and set messages dialog.");
  299. furi_string_free(message_data);
  300. return false;
  301. }
  302. furi_string_free(message_data);
  303. return true;
  304. }