flip_social_messages.c 11 KB

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