flip_social_messages.c 13 KB

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