flip_social_messages.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #include "flip_social_messages.h"
  2. FlipSocialModel2 *flip_social_messages_alloc()
  3. {
  4. if (!app_instance->submenu_messages)
  5. {
  6. if (!easy_flipper_set_submenu(&app_instance->submenu_messages, FlipSocialViewLoggedInMessagesSubmenu, "Messages", flip_social_callback_to_submenu_logged_in, &app_instance->view_dispatcher))
  7. {
  8. return NULL;
  9. }
  10. }
  11. // Allocate memory for each username only if not already allocated
  12. FlipSocialModel2 *users = malloc(sizeof(FlipSocialModel2));
  13. if (users == NULL)
  14. {
  15. FURI_LOG_E(TAG, "Failed to allocate memory for message users");
  16. return NULL;
  17. }
  18. for (size_t i = 0; i < MAX_MESSAGE_USERS; i++)
  19. {
  20. if (users->usernames[i] == NULL)
  21. {
  22. users->usernames[i] = malloc(MAX_USER_LENGTH);
  23. if (users->usernames[i] == NULL)
  24. {
  25. FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
  26. return NULL; // Return false on memory allocation failure
  27. }
  28. }
  29. }
  30. return users;
  31. }
  32. FlipSocialMessage *flip_social_user_messages_alloc()
  33. {
  34. // Allocate memory for each username only if not already allocated
  35. FlipSocialMessage *messages = malloc(sizeof(FlipSocialMessage));
  36. if (messages == NULL)
  37. {
  38. FURI_LOG_E(TAG, "Failed to allocate memory for messages");
  39. return NULL;
  40. }
  41. return messages;
  42. }
  43. void flip_social_free_message_users()
  44. {
  45. if (flip_social_message_users == NULL)
  46. {
  47. return;
  48. }
  49. free(flip_social_message_users);
  50. flip_social_message_users = NULL;
  51. }
  52. void flip_social_free_messages()
  53. {
  54. if (app_instance->submenu_messages)
  55. {
  56. submenu_free(app_instance->submenu_messages);
  57. app_instance->submenu_messages = NULL;
  58. view_dispatcher_remove_view(app_instance->view_dispatcher, FlipSocialViewLoggedInMessagesSubmenu);
  59. }
  60. if (flip_social_messages == NULL)
  61. {
  62. return;
  63. }
  64. free(flip_social_messages);
  65. flip_social_messages = NULL;
  66. }
  67. bool flip_social_update_messages_submenu()
  68. {
  69. if (app_instance->submenu_messages == NULL)
  70. {
  71. FURI_LOG_E(TAG, "Submenu is NULL");
  72. return false;
  73. }
  74. if (flip_social_message_users == NULL)
  75. {
  76. FURI_LOG_E(TAG, "Message users model is NULL");
  77. return false;
  78. }
  79. submenu_reset(app_instance->submenu_messages);
  80. submenu_set_header(app_instance->submenu_messages, "Messages");
  81. submenu_add_item(app_instance->submenu_messages, "[New Message]", FlipSocialSubmenuLoggedInIndexMessagesNewMessage, flip_social_callback_submenu_choices, app_instance);
  82. for (int i = 0; i < flip_social_message_users->count; i++)
  83. {
  84. submenu_add_item(app_instance->submenu_messages, flip_social_message_users->usernames[i], FlipSocialSubmenuLoggedInIndexMessagesUsersStart + i, flip_social_callback_submenu_choices, app_instance);
  85. }
  86. return true;
  87. }
  88. bool flip_social_update_submenu_user_choices()
  89. {
  90. if (app_instance->submenu_messages_user_choices == NULL)
  91. {
  92. FURI_LOG_E(TAG, "Submenu is NULL");
  93. return false;
  94. }
  95. if (flip_social_explore == NULL)
  96. {
  97. FURI_LOG_E(TAG, "Explore model is NULL");
  98. return false;
  99. }
  100. submenu_reset(app_instance->submenu_messages_user_choices);
  101. submenu_set_header(app_instance->submenu_messages_user_choices, "Users");
  102. for (int i = 0; i < flip_social_explore->count; i++)
  103. {
  104. submenu_add_item(app_instance->submenu_messages_user_choices, flip_social_explore->usernames[i], FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart + i, flip_social_callback_submenu_choices, app_instance);
  105. }
  106. return true;
  107. }
  108. // Get all the users that have sent messages to the logged in user
  109. bool flip_social_get_message_users()
  110. {
  111. if (app_instance->login_username_logged_out == NULL)
  112. {
  113. FURI_LOG_E(TAG, "Username is NULL");
  114. return false;
  115. }
  116. if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  117. {
  118. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  119. return false;
  120. }
  121. char command[128];
  122. snprintf(
  123. fhttp.file_path,
  124. sizeof(fhttp.file_path),
  125. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/message_users.json");
  126. fhttp.save_received_data = true;
  127. auth_headers_alloc();
  128. snprintf(command, 128, "https://www.flipsocial.net/api/messages/%s/get/list/%d/", app_instance->login_username_logged_out, MAX_MESSAGE_USERS);
  129. if (!flipper_http_get_request_with_headers(command, auth_headers))
  130. {
  131. FURI_LOG_E(TAG, "Failed to send HTTP request for messages");
  132. fhttp.state = ISSUE;
  133. return false;
  134. }
  135. fhttp.state = RECEIVING;
  136. return true;
  137. }
  138. // Get all the messages between the logged in user and the selected user
  139. bool flip_social_get_messages_with_user()
  140. {
  141. if (!flipper_http_init(flipper_http_rx_callback, app_instance))
  142. {
  143. FURI_LOG_E(TAG, "Failed to initialize FlipperHTTP");
  144. return false;
  145. }
  146. if (app_instance->login_username_logged_out == NULL)
  147. {
  148. FURI_LOG_E(TAG, "Username is NULL");
  149. return false;
  150. }
  151. if (!flip_social_message_users->usernames[flip_social_message_users->index] || strlen(flip_social_message_users->usernames[flip_social_message_users->index]) == 0)
  152. {
  153. FURI_LOG_E(TAG, "Username is NULL");
  154. return false;
  155. }
  156. char command[256];
  157. snprintf(
  158. fhttp.file_path,
  159. sizeof(fhttp.file_path),
  160. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/%s_messages.json",
  161. flip_social_message_users->usernames[flip_social_message_users->index]);
  162. fhttp.save_received_data = true;
  163. auth_headers_alloc();
  164. 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);
  165. if (!flipper_http_get_request_with_headers(command, auth_headers))
  166. {
  167. FURI_LOG_E(TAG, "Failed to send HTTP request for messages");
  168. fhttp.state = ISSUE;
  169. return false;
  170. }
  171. fhttp.state = RECEIVING;
  172. return true;
  173. }
  174. // Parse the users that have sent messages to the logged-in user
  175. bool flip_social_parse_json_message_users()
  176. {
  177. // load the received data from the saved file
  178. FuriString *message_data = flipper_http_load_from_file(fhttp.file_path);
  179. if (message_data == NULL)
  180. {
  181. FURI_LOG_E(TAG, "Failed to load received data from file.");
  182. return false;
  183. }
  184. flipper_http_deinit();
  185. char *data_cstr = (char *)furi_string_get_cstr(message_data);
  186. if (data_cstr == NULL)
  187. {
  188. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  189. furi_string_free(message_data);
  190. return false;
  191. }
  192. // Allocate memory for each username only if not already allocated
  193. flip_social_message_users = flip_social_messages_alloc();
  194. if (flip_social_message_users == NULL)
  195. {
  196. FURI_LOG_E(TAG, "Failed to allocate memory for message users.");
  197. furi_string_free(message_data);
  198. free(data_cstr);
  199. return false;
  200. }
  201. // Initialize message users count
  202. flip_social_message_users->count = 0;
  203. // Extract the users array from the JSON
  204. char *json_users = get_json_value("users", data_cstr, 64);
  205. if (json_users == NULL)
  206. {
  207. FURI_LOG_E(TAG, "Failed to parse users array.");
  208. furi_string_free(message_data);
  209. free(data_cstr);
  210. return false;
  211. }
  212. // Manual tokenization for comma-separated values
  213. char *start = json_users + 1; // Skip the opening bracket
  214. char *end;
  215. while ((end = strchr(start, ',')) != NULL && flip_social_message_users->count < MAX_MESSAGE_USERS)
  216. {
  217. *end = '\0'; // Null-terminate the current token
  218. // Remove quotes
  219. if (*start == '"')
  220. start++;
  221. if (*(end - 1) == '"')
  222. *(end - 1) = '\0';
  223. // Copy username to pre-allocated memory
  224. snprintf(flip_social_message_users->usernames[flip_social_message_users->count], MAX_USER_LENGTH, "%s", start);
  225. flip_social_message_users->count++;
  226. start = end + 1;
  227. }
  228. // Handle the last token
  229. if (*start != '\0' && flip_social_message_users->count < MAX_MESSAGE_USERS)
  230. {
  231. if (*start == '"')
  232. start++;
  233. if (*(start + strlen(start) - 1) == ']')
  234. *(start + strlen(start) - 1) = '\0';
  235. if (*(start + strlen(start) - 1) == '"')
  236. *(start + strlen(start) - 1) = '\0';
  237. snprintf(flip_social_message_users->usernames[flip_social_message_users->count], MAX_USER_LENGTH, "%s", start);
  238. flip_social_message_users->count++;
  239. }
  240. // Add submenu items for the users
  241. flip_social_update_messages_submenu();
  242. // Free the JSON data
  243. free(json_users);
  244. free(start);
  245. free(end);
  246. furi_string_free(message_data);
  247. free(data_cstr);
  248. return true;
  249. }
  250. // Parse the users that the logged in user can message
  251. bool flip_social_parse_json_message_user_choices()
  252. {
  253. // load the received data from the saved file
  254. FuriString *user_data = flipper_http_load_from_file(fhttp.file_path);
  255. if (user_data == NULL)
  256. {
  257. FURI_LOG_E(TAG, "Failed to load received data from file.");
  258. return false;
  259. }
  260. flipper_http_deinit();
  261. char *data_cstr = (char *)furi_string_get_cstr(user_data);
  262. if (data_cstr == NULL)
  263. {
  264. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  265. furi_string_free(user_data);
  266. return false;
  267. }
  268. // Allocate memory for each username only if not already allocated
  269. flip_social_explore = flip_social_explore_alloc();
  270. if (flip_social_explore == NULL)
  271. {
  272. FURI_LOG_E(TAG, "Failed to allocate memory for explore usernames.");
  273. furi_string_free(user_data);
  274. free(data_cstr);
  275. return false;
  276. }
  277. // Initialize explore count
  278. flip_social_explore->count = 0;
  279. // Extract the users array from the JSON
  280. char *json_users = get_json_value("users", data_cstr, 64);
  281. if (json_users == NULL)
  282. {
  283. FURI_LOG_E(TAG, "Failed to parse users array.");
  284. furi_string_free(user_data);
  285. free(data_cstr);
  286. return false;
  287. }
  288. // Manual tokenization for comma-separated values
  289. char *start = json_users + 1; // Skip the opening bracket
  290. char *end;
  291. while ((end = strchr(start, ',')) != NULL && flip_social_explore->count < MAX_EXPLORE_USERS)
  292. {
  293. *end = '\0'; // Null-terminate the current token
  294. // Remove quotes
  295. if (*start == '"')
  296. start++;
  297. if (*(end - 1) == '"')
  298. *(end - 1) = '\0';
  299. // Copy username to pre-allocated memory
  300. snprintf(flip_social_explore->usernames[flip_social_explore->count], MAX_USER_LENGTH, "%s", start);
  301. flip_social_explore->count++;
  302. start = end + 1;
  303. }
  304. // Handle the last token
  305. if (*start != '\0' && flip_social_explore->count < MAX_EXPLORE_USERS)
  306. {
  307. if (*start == '"')
  308. start++;
  309. if (*(start + strlen(start) - 1) == ']')
  310. *(start + strlen(start) - 1) = '\0';
  311. if (*(start + strlen(start) - 1) == '"')
  312. *(start + strlen(start) - 1) = '\0';
  313. snprintf(flip_social_explore->usernames[flip_social_explore->count], MAX_USER_LENGTH, "%s", start);
  314. flip_social_explore->count++;
  315. }
  316. // Add submenu items for the users
  317. flip_social_update_submenu_user_choices();
  318. // Free the JSON data
  319. free(json_users);
  320. free(start);
  321. free(end);
  322. furi_string_free(user_data);
  323. free(data_cstr);
  324. return true;
  325. }
  326. // parse messages between the logged in user and the selected user
  327. bool flip_social_parse_json_messages()
  328. {
  329. // load the received data from the saved file
  330. FuriString *message_data = flipper_http_load_from_file(fhttp.file_path);
  331. if (message_data == NULL)
  332. {
  333. FURI_LOG_E(TAG, "Failed to load received data from file.");
  334. return false;
  335. }
  336. flipper_http_deinit();
  337. char *data_cstr = (char *)furi_string_get_cstr(message_data);
  338. if (data_cstr == NULL)
  339. {
  340. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  341. furi_string_free(message_data);
  342. return false;
  343. }
  344. // Allocate memory for each message only if not already allocated
  345. flip_social_messages = flip_social_user_messages_alloc();
  346. if (!flip_social_messages)
  347. {
  348. FURI_LOG_E(TAG, "Failed to allocate memory for messages.");
  349. furi_string_free(message_data);
  350. free(data_cstr);
  351. return false;
  352. }
  353. // Initialize messages count
  354. flip_social_messages->count = 0;
  355. // Iterate through the messages array
  356. for (int i = 0; i < MAX_MESSAGES; i++)
  357. {
  358. // Parse each item in the array
  359. char *item = get_json_array_value("conversations", i, data_cstr, 64);
  360. if (item == NULL)
  361. {
  362. break;
  363. }
  364. // Extract individual fields from the JSON object
  365. char *sender = get_json_value("sender", item, 8);
  366. char *content = get_json_value("content", item, 8);
  367. if (sender == NULL || content == NULL)
  368. {
  369. FURI_LOG_E(TAG, "Failed to parse item fields.");
  370. free(item);
  371. continue;
  372. }
  373. // Store parsed values in pre-allocated memory
  374. snprintf(flip_social_messages->usernames[i], MAX_USER_LENGTH, "%s", sender);
  375. snprintf(flip_social_messages->messages[i], MAX_MESSAGE_LENGTH, "%s", content);
  376. flip_social_messages->count++;
  377. free(item);
  378. free(sender);
  379. free(content);
  380. }
  381. if (!messages_dialog_alloc(true))
  382. {
  383. FURI_LOG_E(TAG, "Failed to allocate and set messages dialog.");
  384. furi_string_free(message_data);
  385. free(data_cstr);
  386. return false;
  387. }
  388. furi_string_free(message_data);
  389. free(data_cstr);
  390. return true;
  391. }