flip_social_messages.c 11 KB

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