flip_social_messages.c 14 KB

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