flip_social_messages.c 14 KB

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