flip_social_messages.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. char command[128];
  166. snprintf(
  167. fhttp.file_path,
  168. sizeof(fhttp.file_path),
  169. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/messages.txt");
  170. fhttp.save_received_data = true;
  171. auth_headers_alloc();
  172. 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]);
  173. if (!flipper_http_get_request_with_headers(command, auth_headers))
  174. {
  175. FURI_LOG_E(TAG, "Failed to send HTTP request for messages");
  176. fhttp.state = ISSUE;
  177. return false;
  178. }
  179. fhttp.state = RECEIVING;
  180. return true;
  181. }
  182. // Parse the users that have sent messages to the logged-in user
  183. bool flip_social_parse_json_message_users()
  184. {
  185. // load the received data from the saved file
  186. FuriString *message_data = flipper_http_load_from_file(fhttp.file_path);
  187. if (message_data == NULL)
  188. {
  189. FURI_LOG_E(TAG, "Failed to load received data from file.");
  190. return false;
  191. }
  192. char *data_cstr = (char *)furi_string_get_cstr(message_data);
  193. if (data_cstr == NULL)
  194. {
  195. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  196. furi_string_free(message_data);
  197. return false;
  198. }
  199. // Allocate memory for each username only if not already allocated
  200. flip_social_message_users = flip_social_messages_alloc();
  201. if (flip_social_message_users == NULL)
  202. {
  203. FURI_LOG_E(TAG, "Failed to allocate memory for message users.");
  204. furi_string_free(message_data);
  205. free(data_cstr);
  206. return false;
  207. }
  208. // Remove newlines
  209. char *pos = data_cstr;
  210. while ((pos = strchr(pos, '\n')) != NULL)
  211. {
  212. *pos = ' ';
  213. }
  214. // Initialize message users count
  215. flip_social_message_users->count = 0;
  216. // Extract the users array from the JSON
  217. char *json_users = get_json_value("users", data_cstr, MAX_TOKENS);
  218. if (json_users == NULL)
  219. {
  220. FURI_LOG_E(TAG, "Failed to parse users array.");
  221. furi_string_free(message_data);
  222. free(data_cstr);
  223. return false;
  224. }
  225. // Manual tokenization for comma-separated values
  226. char *start = json_users + 1; // Skip the opening bracket
  227. char *end;
  228. while ((end = strchr(start, ',')) != NULL && flip_social_message_users->count < MAX_MESSAGE_USERS)
  229. {
  230. *end = '\0'; // Null-terminate the current token
  231. // Remove quotes
  232. if (*start == '"')
  233. start++;
  234. if (*(end - 1) == '"')
  235. *(end - 1) = '\0';
  236. // Copy username to pre-allocated memory
  237. snprintf(flip_social_message_users->usernames[flip_social_message_users->count], MAX_USER_LENGTH, "%s", start);
  238. flip_social_message_users->count++;
  239. start = end + 1;
  240. }
  241. // Handle the last token
  242. if (*start != '\0' && flip_social_message_users->count < MAX_MESSAGE_USERS)
  243. {
  244. if (*start == '"')
  245. start++;
  246. if (*(start + strlen(start) - 1) == ']')
  247. *(start + strlen(start) - 1) = '\0';
  248. if (*(start + strlen(start) - 1) == '"')
  249. *(start + strlen(start) - 1) = '\0';
  250. snprintf(flip_social_message_users->usernames[flip_social_message_users->count], MAX_USER_LENGTH, "%s", start);
  251. flip_social_message_users->count++;
  252. }
  253. // Add submenu items for the users
  254. flip_social_update_messages_submenu();
  255. // Free the JSON data
  256. free(json_users);
  257. free(start);
  258. free(end);
  259. furi_string_free(message_data);
  260. free(data_cstr);
  261. return true;
  262. }
  263. // Parse the users that the logged in user can message
  264. bool flip_social_parse_json_message_user_choices()
  265. {
  266. // load the received data from the saved file
  267. FuriString *user_data = flipper_http_load_from_file(fhttp.file_path);
  268. if (user_data == NULL)
  269. {
  270. FURI_LOG_E(TAG, "Failed to load received data from file.");
  271. return false;
  272. }
  273. char *data_cstr = (char *)furi_string_get_cstr(user_data);
  274. if (data_cstr == NULL)
  275. {
  276. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  277. furi_string_free(user_data);
  278. return false;
  279. }
  280. // Allocate memory for each username only if not already allocated
  281. flip_social_explore = flip_social_explore_alloc();
  282. if (flip_social_explore == NULL)
  283. {
  284. FURI_LOG_E(TAG, "Failed to allocate memory for explore usernames.");
  285. furi_string_free(user_data);
  286. free(data_cstr);
  287. return false;
  288. }
  289. // Remove newlines
  290. char *pos = data_cstr;
  291. while ((pos = strchr(pos, '\n')) != NULL)
  292. {
  293. *pos = ' ';
  294. }
  295. // Initialize explore count
  296. flip_social_explore->count = 0;
  297. // Extract the users array from the JSON
  298. char *json_users = get_json_value("users", data_cstr, MAX_TOKENS);
  299. if (json_users == NULL)
  300. {
  301. FURI_LOG_E(TAG, "Failed to parse users array.");
  302. furi_string_free(user_data);
  303. free(data_cstr);
  304. return false;
  305. }
  306. // Manual tokenization for comma-separated values
  307. char *start = json_users + 1; // Skip the opening bracket
  308. char *end;
  309. while ((end = strchr(start, ',')) != NULL && flip_social_explore->count < MAX_EXPLORE_USERS)
  310. {
  311. *end = '\0'; // Null-terminate the current token
  312. // Remove quotes
  313. if (*start == '"')
  314. start++;
  315. if (*(end - 1) == '"')
  316. *(end - 1) = '\0';
  317. // Copy username to pre-allocated memory
  318. snprintf(flip_social_explore->usernames[flip_social_explore->count], MAX_USER_LENGTH, "%s", start);
  319. flip_social_explore->count++;
  320. start = end + 1;
  321. }
  322. // Handle the last token
  323. if (*start != '\0' && flip_social_explore->count < MAX_EXPLORE_USERS)
  324. {
  325. if (*start == '"')
  326. start++;
  327. if (*(start + strlen(start) - 1) == ']')
  328. *(start + strlen(start) - 1) = '\0';
  329. if (*(start + strlen(start) - 1) == '"')
  330. *(start + strlen(start) - 1) = '\0';
  331. snprintf(flip_social_explore->usernames[flip_social_explore->count], MAX_USER_LENGTH, "%s", start);
  332. flip_social_explore->count++;
  333. }
  334. // Add submenu items for the users
  335. flip_social_update_submenu_user_choices();
  336. // Free the JSON data
  337. free(json_users);
  338. free(start);
  339. free(end);
  340. furi_string_free(user_data);
  341. free(data_cstr);
  342. return true;
  343. }
  344. // parse messages between the logged in user and the selected user
  345. bool flip_social_parse_json_messages()
  346. {
  347. // load the received data from the saved file
  348. FuriString *message_data = flipper_http_load_from_file(fhttp.file_path);
  349. if (message_data == NULL)
  350. {
  351. FURI_LOG_E(TAG, "Failed to load received data from file.");
  352. return false;
  353. }
  354. char *data_cstr = (char *)furi_string_get_cstr(message_data);
  355. if (data_cstr == NULL)
  356. {
  357. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  358. furi_string_free(message_data);
  359. return false;
  360. }
  361. // Allocate memory for each message only if not already allocated
  362. flip_social_messages = flip_social_user_messages_alloc();
  363. if (!flip_social_messages)
  364. {
  365. FURI_LOG_E(TAG, "Failed to allocate memory for messages.");
  366. furi_string_free(message_data);
  367. free(data_cstr);
  368. return false;
  369. }
  370. // Remove newlines
  371. char *pos = data_cstr;
  372. while ((pos = strchr(pos, '\n')) != NULL)
  373. {
  374. *pos = ' ';
  375. }
  376. // Initialize messages count
  377. flip_social_messages->count = 0;
  378. // Iterate through the messages array
  379. for (int i = 0; i < MAX_MESSAGES; i++)
  380. {
  381. // Parse each item in the array
  382. char *item = get_json_array_value("conversations", i, data_cstr, MAX_TOKENS);
  383. if (item == NULL)
  384. {
  385. break;
  386. }
  387. // Extract individual fields from the JSON object
  388. char *sender = get_json_value("sender", item, MAX_TOKENS);
  389. char *content = get_json_value("content", item, MAX_TOKENS);
  390. if (sender == NULL || content == NULL)
  391. {
  392. FURI_LOG_E(TAG, "Failed to parse item fields.");
  393. free(item);
  394. continue;
  395. }
  396. // Store parsed values in pre-allocated memory
  397. snprintf(flip_social_messages->usernames[i], MAX_USER_LENGTH, "%s", sender);
  398. snprintf(flip_social_messages->messages[i], MAX_MESSAGE_LENGTH, "%s", content);
  399. flip_social_messages->count++;
  400. free(item);
  401. free(sender);
  402. free(content);
  403. }
  404. furi_string_free(message_data);
  405. free(data_cstr);
  406. return true;
  407. }