messages.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. #include <messages/messages.h>
  2. #include <alloc/alloc.h>
  3. bool messages_submenu_update()
  4. {
  5. if (!app_instance)
  6. {
  7. FURI_LOG_E(TAG, "App instance is NULL");
  8. return false;
  9. }
  10. if (app_instance->submenu == NULL)
  11. {
  12. FURI_LOG_E(TAG, "Submenu is NULL");
  13. return false;
  14. }
  15. if (flip_social_message_users == NULL)
  16. {
  17. FURI_LOG_E(TAG, "Message users model is NULL");
  18. return false;
  19. }
  20. submenu_reset(app_instance->submenu);
  21. submenu_set_header(app_instance->submenu, "Messages");
  22. submenu_add_item(app_instance->submenu, "[New Message]", FlipSocialSubmenuLoggedInIndexMessagesNewMessage, callback_submenu_choices, app_instance);
  23. for (int i = 0; i < flip_social_message_users->count; i++)
  24. {
  25. submenu_add_item(app_instance->submenu, flip_social_message_users->usernames[i], FlipSocialSubmenuLoggedInIndexMessagesUsersStart + i, callback_submenu_choices, app_instance);
  26. }
  27. return true;
  28. }
  29. bool messages_update_submenu_user_choices()
  30. {
  31. if (app_instance == NULL)
  32. {
  33. FURI_LOG_E(TAG, "App instance is NULL");
  34. return false;
  35. }
  36. if (app_instance->submenu == NULL)
  37. {
  38. FURI_LOG_E(TAG, "Submenu is NULL");
  39. return false;
  40. }
  41. if (flip_social_explore == NULL)
  42. {
  43. FURI_LOG_E(TAG, "Explore model is NULL");
  44. return false;
  45. }
  46. submenu_reset(app_instance->submenu);
  47. submenu_set_header(app_instance->submenu, "Users");
  48. for (int i = 0; i < flip_social_explore->count; i++)
  49. {
  50. submenu_add_item(app_instance->submenu, flip_social_explore->usernames[i], FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart + i, callback_submenu_choices, app_instance);
  51. }
  52. return true;
  53. }
  54. // Get all the users that have sent messages to the logged in user
  55. bool messages_get_message_users(FlipperHTTP *fhttp)
  56. {
  57. if (!app_instance)
  58. {
  59. FURI_LOG_E(TAG, "App instance is NULL");
  60. return false;
  61. }
  62. if (!fhttp)
  63. {
  64. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  65. return false;
  66. }
  67. if (app_instance->login_username_logged_out == NULL)
  68. {
  69. FURI_LOG_E(TAG, "Username is NULL");
  70. return false;
  71. }
  72. // create the messages directory
  73. if (!flip_social_subfolder_mkdir("messages"))
  74. {
  75. FURI_LOG_E(TAG, "Failed to create messages directory");
  76. return false;
  77. }
  78. char command[128];
  79. snprintf(
  80. fhttp->file_path,
  81. sizeof(fhttp->file_path),
  82. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/messages/message_users.json");
  83. fhttp->save_received_data = true;
  84. alloc_headers();
  85. snprintf(command, 128, "https://www.jblanked.com/flipper/api/messages/%s/get/list/%d/", app_instance->login_username_logged_out, MAX_MESSAGE_USERS);
  86. if (!flipper_http_request(fhttp, GET, command, auth_headers, NULL))
  87. {
  88. FURI_LOG_E(TAG, "Failed to send HTTP request for messages");
  89. fhttp->state = ISSUE;
  90. return false;
  91. }
  92. fhttp->state = RECEIVING;
  93. return true;
  94. }
  95. // Get all the messages between the logged in user and the selected user
  96. bool messages_get_messages_with_user(FlipperHTTP *fhttp)
  97. {
  98. if (!app_instance)
  99. {
  100. FURI_LOG_E(TAG, "App instance is NULL");
  101. return false;
  102. }
  103. if (!fhttp)
  104. {
  105. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  106. return false;
  107. }
  108. if (app_instance->login_username_logged_out == NULL)
  109. {
  110. FURI_LOG_E(TAG, "Username is NULL");
  111. return false;
  112. }
  113. if (strlen(flip_social_message_users->usernames[flip_social_message_users->index]) == 0)
  114. {
  115. FURI_LOG_E(TAG, "Username is NULL");
  116. return false;
  117. }
  118. // create the messages directory
  119. if (!flip_social_subfolder_mkdir("messages"))
  120. {
  121. FURI_LOG_E(TAG, "Failed to create messages directory");
  122. return false;
  123. }
  124. char command[256];
  125. snprintf(
  126. fhttp->file_path,
  127. sizeof(fhttp->file_path),
  128. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/messages/%s_messages.json",
  129. flip_social_message_users->usernames[flip_social_message_users->index]);
  130. fhttp->save_received_data = true;
  131. alloc_headers();
  132. snprintf(command, sizeof(command), "https://www.jblanked.com/flipper/api/messages/%s/get/%s/%d/", app_instance->login_username_logged_out, flip_social_message_users->usernames[flip_social_message_users->index], MAX_MESSAGES);
  133. if (!flipper_http_request(fhttp, GET, command, auth_headers, NULL))
  134. {
  135. FURI_LOG_E(TAG, "Failed to send HTTP request for messages");
  136. fhttp->state = ISSUE;
  137. return false;
  138. }
  139. fhttp->state = RECEIVING;
  140. return true;
  141. }
  142. // Parse the users that have sent messages to the logged-in user
  143. bool messages_parse_json_message_users(FlipperHTTP *fhttp)
  144. {
  145. if (!fhttp)
  146. {
  147. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  148. return false;
  149. }
  150. // load the received data from the saved file
  151. FuriString *message_data = flipper_http_load_from_file(fhttp->file_path);
  152. if (message_data == NULL)
  153. {
  154. FURI_LOG_E(TAG, "Failed to load received data from file.");
  155. return false;
  156. }
  157. // Allocate memory for each username only if not already allocated
  158. flip_social_message_users = alloc_messages();
  159. if (flip_social_message_users == NULL)
  160. {
  161. FURI_LOG_E(TAG, "Failed to allocate memory for message users.");
  162. furi_string_free(message_data);
  163. return false;
  164. }
  165. // Initialize message users count
  166. flip_social_message_users->count = 0;
  167. for (int i = 0; i < MAX_MESSAGE_USERS; i++)
  168. {
  169. FuriString *user = get_json_array_value_furi("users", i, message_data);
  170. if (user == NULL)
  171. {
  172. break;
  173. }
  174. snprintf(flip_social_message_users->usernames[i], MAX_USER_LENGTH, "%s", furi_string_get_cstr(user));
  175. flip_social_message_users->count++;
  176. furi_string_free(user);
  177. }
  178. // Add submenu items for the users
  179. messages_submenu_update();
  180. // Free the JSON data
  181. furi_string_free(message_data);
  182. return true;
  183. }
  184. // Parse the users that the logged in user can message
  185. bool messages_parse_json_message_user_choices(FlipperHTTP *fhttp)
  186. {
  187. if (!fhttp)
  188. {
  189. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  190. return false;
  191. }
  192. // load the received data from the saved file
  193. FuriString *user_data = flipper_http_load_from_file(fhttp->file_path);
  194. if (user_data == NULL)
  195. {
  196. FURI_LOG_E(TAG, "Failed to load received data from file.");
  197. return false;
  198. }
  199. // Allocate memory for each username only if not already allocated
  200. flip_social_explore = alloc_explore();
  201. if (flip_social_explore == NULL)
  202. {
  203. FURI_LOG_E(TAG, "Failed to allocate memory for explore usernames.");
  204. furi_string_free(user_data);
  205. return false;
  206. }
  207. // Initialize explore count
  208. flip_social_explore->count = 0;
  209. for (int i = 0; i < MAX_MESSAGE_USERS; i++)
  210. {
  211. FuriString *user = get_json_array_value_furi("users", i, user_data);
  212. if (user == NULL)
  213. {
  214. break;
  215. }
  216. snprintf(flip_social_explore->usernames[i], MAX_USER_LENGTH, "%s", furi_string_get_cstr(user));
  217. flip_social_explore->count++;
  218. furi_string_free(user);
  219. }
  220. // Add submenu items for the users
  221. messages_update_submenu_user_choices();
  222. // Free the JSON data
  223. furi_string_free(user_data);
  224. return flip_social_explore->count > 0;
  225. }
  226. // parse messages between the logged in user and the selected user
  227. bool messages_parse_json_messages(FlipperHTTP *fhttp)
  228. {
  229. if (!fhttp)
  230. {
  231. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  232. return false;
  233. }
  234. // load the received data from the saved file
  235. FuriString *message_data = flipper_http_load_from_file(fhttp->file_path);
  236. if (message_data == NULL)
  237. {
  238. FURI_LOG_E(TAG, "Failed to load received data from file.");
  239. return false;
  240. }
  241. // Allocate memory for each message only if not already allocated
  242. flip_social_messages = alloc_user_messages();
  243. if (!flip_social_messages)
  244. {
  245. FURI_LOG_E(TAG, "Failed to allocate memory for messages.");
  246. furi_string_free(message_data);
  247. return false;
  248. }
  249. // Initialize messages count
  250. flip_social_messages->count = 0;
  251. // Iterate through the messages array
  252. for (int i = 0; i < MAX_MESSAGES; i++)
  253. {
  254. // Parse each item in the array
  255. FuriString *item = get_json_array_value_furi("conversations", i, message_data);
  256. if (item == NULL)
  257. {
  258. break;
  259. }
  260. // Extract individual fields from the JSON object
  261. FuriString *sender = get_json_value_furi("sender", item);
  262. FuriString *content = get_json_value_furi("content", item);
  263. if (sender == NULL || content == NULL)
  264. {
  265. FURI_LOG_E(TAG, "Failed to parse item fields.");
  266. if (sender)
  267. furi_string_free(sender);
  268. if (content)
  269. furi_string_free(content);
  270. furi_string_free(item);
  271. continue;
  272. }
  273. // Store parsed values in pre-allocated memory
  274. snprintf(flip_social_messages->usernames[i], MAX_USER_LENGTH, "%s", furi_string_get_cstr(sender));
  275. snprintf(flip_social_messages->messages[i], MAX_MESSAGE_LENGTH, "%s", furi_string_get_cstr(content));
  276. flip_social_messages->count++;
  277. furi_string_free(item);
  278. furi_string_free(sender);
  279. furi_string_free(content);
  280. }
  281. if (!allow_messages_dialog(true))
  282. {
  283. FURI_LOG_E(TAG, "Failed to allocate and set messages dialog.");
  284. furi_string_free(message_data);
  285. return false;
  286. }
  287. furi_string_free(message_data);
  288. return true;
  289. }