messages.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. char directory[128];
  73. snprintf(directory, sizeof(directory), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/messages");
  74. // Create the directory
  75. Storage *storage = furi_record_open(RECORD_STORAGE);
  76. storage_common_mkdir(storage, directory);
  77. char command[128];
  78. snprintf(
  79. fhttp->file_path,
  80. sizeof(fhttp->file_path),
  81. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/messages/message_users.json");
  82. fhttp->save_received_data = true;
  83. alloc_headers();
  84. snprintf(command, 128, "https://www.jblanked.com/flipper/api/messages/%s/get/list/%d/", app_instance->login_username_logged_out, MAX_MESSAGE_USERS);
  85. if (!flipper_http_request(fhttp, GET, command, auth_headers, NULL))
  86. {
  87. FURI_LOG_E(TAG, "Failed to send HTTP request for messages");
  88. fhttp->state = ISSUE;
  89. return false;
  90. }
  91. fhttp->state = RECEIVING;
  92. return true;
  93. }
  94. // Get all the messages between the logged in user and the selected user
  95. bool messages_get_messages_with_user(FlipperHTTP *fhttp)
  96. {
  97. if (!app_instance)
  98. {
  99. FURI_LOG_E(TAG, "App instance is NULL");
  100. return false;
  101. }
  102. if (!fhttp)
  103. {
  104. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  105. return false;
  106. }
  107. if (app_instance->login_username_logged_out == NULL)
  108. {
  109. FURI_LOG_E(TAG, "Username is NULL");
  110. return false;
  111. }
  112. if (strlen(flip_social_message_users->usernames[flip_social_message_users->index]) == 0)
  113. {
  114. FURI_LOG_E(TAG, "Username is NULL");
  115. return false;
  116. }
  117. char directory[128];
  118. snprintf(directory, sizeof(directory), STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/messages");
  119. // Create the directory
  120. Storage *storage = furi_record_open(RECORD_STORAGE);
  121. storage_common_mkdir(storage, directory);
  122. char command[256];
  123. snprintf(
  124. fhttp->file_path,
  125. sizeof(fhttp->file_path),
  126. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/messages/%s_messages.json",
  127. flip_social_message_users->usernames[flip_social_message_users->index]);
  128. fhttp->save_received_data = true;
  129. alloc_headers();
  130. 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);
  131. if (!flipper_http_request(fhttp, GET, command, auth_headers, NULL))
  132. {
  133. FURI_LOG_E(TAG, "Failed to send HTTP request for messages");
  134. fhttp->state = ISSUE;
  135. return false;
  136. }
  137. fhttp->state = RECEIVING;
  138. return true;
  139. }
  140. // Parse the users that have sent messages to the logged-in user
  141. bool messages_parse_json_message_users(FlipperHTTP *fhttp)
  142. {
  143. if (!fhttp)
  144. {
  145. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  146. return false;
  147. }
  148. // load the received data from the saved file
  149. FuriString *message_data = flipper_http_load_from_file(fhttp->file_path);
  150. if (message_data == NULL)
  151. {
  152. FURI_LOG_E(TAG, "Failed to load received data from file.");
  153. return false;
  154. }
  155. // Allocate memory for each username only if not already allocated
  156. flip_social_message_users = alloc_messages();
  157. if (flip_social_message_users == NULL)
  158. {
  159. FURI_LOG_E(TAG, "Failed to allocate memory for message users.");
  160. furi_string_free(message_data);
  161. return false;
  162. }
  163. // Initialize message users count
  164. flip_social_message_users->count = 0;
  165. for (int i = 0; i < MAX_MESSAGE_USERS; i++)
  166. {
  167. FuriString *user = get_json_array_value_furi("users", i, message_data);
  168. if (user == NULL)
  169. {
  170. break;
  171. }
  172. snprintf(flip_social_message_users->usernames[i], MAX_USER_LENGTH, "%s", furi_string_get_cstr(user));
  173. flip_social_message_users->count++;
  174. furi_string_free(user);
  175. }
  176. // Add submenu items for the users
  177. messages_submenu_update();
  178. // Free the JSON data
  179. furi_string_free(message_data);
  180. return true;
  181. }
  182. // Parse the users that the logged in user can message
  183. bool messages_parse_json_message_user_choices(FlipperHTTP *fhttp)
  184. {
  185. if (!fhttp)
  186. {
  187. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  188. return false;
  189. }
  190. // load the received data from the saved file
  191. FuriString *user_data = flipper_http_load_from_file(fhttp->file_path);
  192. if (user_data == NULL)
  193. {
  194. FURI_LOG_E(TAG, "Failed to load received data from file.");
  195. return false;
  196. }
  197. // Allocate memory for each username only if not already allocated
  198. flip_social_explore = alloc_explore();
  199. if (flip_social_explore == NULL)
  200. {
  201. FURI_LOG_E(TAG, "Failed to allocate memory for explore usernames.");
  202. furi_string_free(user_data);
  203. return false;
  204. }
  205. // Initialize explore count
  206. flip_social_explore->count = 0;
  207. for (int i = 0; i < MAX_MESSAGE_USERS; i++)
  208. {
  209. FuriString *user = get_json_array_value_furi("users", i, user_data);
  210. if (user == NULL)
  211. {
  212. break;
  213. }
  214. snprintf(flip_social_explore->usernames[i], MAX_USER_LENGTH, "%s", furi_string_get_cstr(user));
  215. flip_social_explore->count++;
  216. furi_string_free(user);
  217. }
  218. // Add submenu items for the users
  219. messages_update_submenu_user_choices();
  220. // Free the JSON data
  221. furi_string_free(user_data);
  222. return flip_social_explore->count > 0;
  223. }
  224. // parse messages between the logged in user and the selected user
  225. bool messages_parse_json_messages(FlipperHTTP *fhttp)
  226. {
  227. if (!fhttp)
  228. {
  229. FURI_LOG_E(TAG, "FlipperHTTP is NULL");
  230. return false;
  231. }
  232. // load the received data from the saved file
  233. FuriString *message_data = flipper_http_load_from_file(fhttp->file_path);
  234. if (message_data == NULL)
  235. {
  236. FURI_LOG_E(TAG, "Failed to load received data from file.");
  237. return false;
  238. }
  239. // Allocate memory for each message only if not already allocated
  240. flip_social_messages = alloc_user_messages();
  241. if (!flip_social_messages)
  242. {
  243. FURI_LOG_E(TAG, "Failed to allocate memory for messages.");
  244. furi_string_free(message_data);
  245. return false;
  246. }
  247. // Initialize messages count
  248. flip_social_messages->count = 0;
  249. // Iterate through the messages array
  250. for (int i = 0; i < MAX_MESSAGES; i++)
  251. {
  252. // Parse each item in the array
  253. FuriString *item = get_json_array_value_furi("conversations", i, message_data);
  254. if (item == NULL)
  255. {
  256. break;
  257. }
  258. // Extract individual fields from the JSON object
  259. FuriString *sender = get_json_value_furi("sender", item);
  260. FuriString *content = get_json_value_furi("content", item);
  261. if (sender == NULL || content == NULL)
  262. {
  263. FURI_LOG_E(TAG, "Failed to parse item fields.");
  264. if (sender)
  265. furi_string_free(sender);
  266. if (content)
  267. furi_string_free(content);
  268. furi_string_free(item);
  269. continue;
  270. }
  271. // Store parsed values in pre-allocated memory
  272. snprintf(flip_social_messages->usernames[i], MAX_USER_LENGTH, "%s", furi_string_get_cstr(sender));
  273. snprintf(flip_social_messages->messages[i], MAX_MESSAGE_LENGTH, "%s", furi_string_get_cstr(content));
  274. flip_social_messages->count++;
  275. furi_string_free(item);
  276. furi_string_free(sender);
  277. furi_string_free(content);
  278. }
  279. if (!allow_messages_dialog(true))
  280. {
  281. FURI_LOG_E(TAG, "Failed to allocate and set messages dialog.");
  282. furi_string_free(message_data);
  283. return false;
  284. }
  285. furi_string_free(message_data);
  286. return true;
  287. }