flip_social_messages.c 15 KB

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