flip_social_messages.c 9.8 KB

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