flip_social_messages.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #include "flip_social_messages.h"
  2. FlipSocialModel2* flip_social_messages_alloc() {
  3. // Allocate memory for each username only if not already allocated
  4. FlipSocialModel2* users = malloc(sizeof(FlipSocialModel2));
  5. if(users == NULL) {
  6. FURI_LOG_E(TAG, "Failed to allocate memory for message users");
  7. return NULL;
  8. }
  9. for(size_t i = 0; i < MAX_MESSAGE_USERS; i++) {
  10. if(users->usernames[i] == NULL) {
  11. users->usernames[i] = malloc(MAX_USER_LENGTH);
  12. if(users->usernames[i] == NULL) {
  13. FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
  14. return NULL; // Return false on memory allocation failure
  15. }
  16. }
  17. }
  18. return users;
  19. }
  20. FlipSocialMessage* flip_social_user_messages_alloc() {
  21. // Allocate memory for each username only if not already allocated
  22. FlipSocialMessage* messages = malloc(sizeof(FlipSocialMessage));
  23. if(messages == NULL) {
  24. FURI_LOG_E(TAG, "Failed to allocate memory for messages");
  25. return NULL;
  26. }
  27. for(size_t i = 0; i < MAX_MESSAGE_USERS; i++) {
  28. if(messages->usernames[i] == NULL) {
  29. messages->usernames[i] = malloc(MAX_USER_LENGTH);
  30. if(messages->usernames[i] == NULL) {
  31. FURI_LOG_E(TAG, "Failed to allocate memory for username %zu", i);
  32. return NULL; // Return false on memory allocation failure
  33. }
  34. }
  35. if(messages->messages[i] == NULL) {
  36. messages->messages[i] = malloc(MAX_MESSAGE_LENGTH);
  37. if(messages->messages[i] == NULL) {
  38. FURI_LOG_E(TAG, "Failed to allocate memory for message %zu", i);
  39. return NULL; // Return false on memory allocation failure
  40. }
  41. }
  42. }
  43. return messages;
  44. }
  45. void flip_social_free_message_users() {
  46. if(flip_social_message_users == NULL) {
  47. FURI_LOG_E(TAG, "Message users model is NULL");
  48. return;
  49. }
  50. for(int i = 0; i < flip_social_message_users->count; i++) {
  51. if(flip_social_message_users->usernames[i]) {
  52. free(flip_social_message_users->usernames[i]);
  53. }
  54. }
  55. }
  56. void flip_social_free_messages() {
  57. if(flip_social_messages == NULL) {
  58. FURI_LOG_E(TAG, "Messages model is NULL");
  59. return;
  60. }
  61. for(int i = 0; i < flip_social_messages->count; i++) {
  62. if(flip_social_messages->usernames[i]) {
  63. free(flip_social_messages->usernames[i]);
  64. }
  65. if(flip_social_messages->messages[i]) {
  66. free(flip_social_messages->messages[i]);
  67. }
  68. }
  69. }
  70. bool flip_social_update_messages_submenu() {
  71. if(app_instance->submenu_messages == NULL) {
  72. FURI_LOG_E(TAG, "Submenu is NULL");
  73. return false;
  74. }
  75. if(flip_social_message_users == NULL) {
  76. FURI_LOG_E(TAG, "Message users model is NULL");
  77. return false;
  78. }
  79. submenu_reset(app_instance->submenu_messages);
  80. submenu_set_header(app_instance->submenu_messages, "Messages");
  81. submenu_add_item(
  82. app_instance->submenu_messages,
  83. "[New Message]",
  84. FlipSocialSubmenuLoggedInIndexMessagesNewMessage,
  85. flip_social_callback_submenu_choices,
  86. app_instance);
  87. for(int i = 0; i < flip_social_message_users->count; i++) {
  88. submenu_add_item(
  89. app_instance->submenu_messages,
  90. flip_social_message_users->usernames[i],
  91. FlipSocialSubmenuLoggedInIndexMessagesUsersStart + i,
  92. flip_social_callback_submenu_choices,
  93. app_instance);
  94. }
  95. return true;
  96. }
  97. bool flip_social_update_submenu_user_choices() {
  98. if(app_instance->submenu_messages_user_choices == NULL) {
  99. FURI_LOG_E(TAG, "Submenu is NULL");
  100. return false;
  101. }
  102. if(flip_social_explore == NULL) {
  103. FURI_LOG_E(TAG, "Explore model is NULL");
  104. return false;
  105. }
  106. submenu_reset(app_instance->submenu_messages_user_choices);
  107. submenu_set_header(app_instance->submenu_messages_user_choices, "Users");
  108. for(int i = 0; i < flip_social_explore->count; i++) {
  109. submenu_add_item(
  110. app_instance->submenu_messages_user_choices,
  111. flip_social_explore->usernames[i],
  112. FlipSocialSubmenuLoggedInIndexMessagesUserChoicesIndexStart + i,
  113. flip_social_callback_submenu_choices,
  114. app_instance);
  115. }
  116. return true;
  117. }
  118. // Get all the users that have sent messages to the logged in user
  119. bool flip_social_get_message_users() {
  120. if(app_instance->login_username_logged_out == NULL) {
  121. FURI_LOG_E(TAG, "Username is NULL");
  122. return false;
  123. }
  124. char command[128];
  125. snprintf(
  126. fhttp.file_path,
  127. sizeof(fhttp.file_path),
  128. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/message_users.txt");
  129. fhttp.save_received_data = true;
  130. auth_headers_alloc();
  131. snprintf(
  132. command,
  133. 128,
  134. "https://www.flipsocial.net/api/messages/%s/get/list/",
  135. app_instance->login_username_logged_out);
  136. if(!flipper_http_get_request_with_headers(command, auth_headers)) {
  137. FURI_LOG_E(TAG, "Failed to send HTTP request for messages");
  138. fhttp.state = ISSUE;
  139. return false;
  140. }
  141. fhttp.state = RECEIVING;
  142. return true;
  143. }
  144. // Get all the messages between the logged in user and the selected user
  145. bool flip_social_get_messages_with_user() {
  146. if(app_instance->login_username_logged_out == NULL) {
  147. FURI_LOG_E(TAG, "Username is NULL");
  148. return false;
  149. }
  150. if(!flip_social_message_users->usernames[flip_social_message_users->index] ||
  151. strlen(flip_social_message_users->usernames[flip_social_message_users->index]) == 0) {
  152. FURI_LOG_E(TAG, "Username is NULL");
  153. return false;
  154. }
  155. char command[128];
  156. snprintf(
  157. fhttp.file_path,
  158. sizeof(fhttp.file_path),
  159. STORAGE_EXT_PATH_PREFIX "/apps_data/flip_social/messages.txt");
  160. fhttp.save_received_data = true;
  161. auth_headers_alloc();
  162. snprintf(
  163. command,
  164. 128,
  165. "https://www.flipsocial.net/api/messages/%s/get/%s/",
  166. app_instance->login_username_logged_out,
  167. flip_social_message_users->usernames[flip_social_message_users->index]);
  168. if(!flipper_http_get_request_with_headers(command, auth_headers)) {
  169. FURI_LOG_E(TAG, "Failed to send HTTP request for messages");
  170. fhttp.state = ISSUE;
  171. return false;
  172. }
  173. fhttp.state = RECEIVING;
  174. return true;
  175. }
  176. // Parse the users that have sent messages to the logged-in user
  177. bool flip_social_parse_json_message_users() {
  178. // load the received data from the saved file
  179. FuriString* message_data = flipper_http_load_from_file(fhttp.file_path);
  180. if(message_data == NULL) {
  181. FURI_LOG_E(TAG, "Failed to load received data from file.");
  182. return false;
  183. }
  184. char* data_cstr = (char*)furi_string_get_cstr(message_data);
  185. if(data_cstr == NULL) {
  186. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  187. furi_string_free(message_data);
  188. return false;
  189. }
  190. // Allocate memory for each username only if not already allocated
  191. flip_social_message_users = flip_social_messages_alloc();
  192. if(flip_social_message_users == NULL) {
  193. FURI_LOG_E(TAG, "Failed to allocate memory for message users.");
  194. furi_string_free(message_data);
  195. free(data_cstr);
  196. return false;
  197. }
  198. // Remove newlines
  199. char* pos = data_cstr;
  200. while((pos = strchr(pos, '\n')) != NULL) {
  201. *pos = ' ';
  202. }
  203. // Initialize message users count
  204. flip_social_message_users->count = 0;
  205. // Extract the users array from the JSON
  206. char* json_users = get_json_value("users", data_cstr, MAX_TOKENS);
  207. if(json_users == NULL) {
  208. FURI_LOG_E(TAG, "Failed to parse users array.");
  209. furi_string_free(message_data);
  210. free(data_cstr);
  211. return false;
  212. }
  213. // Manual tokenization for comma-separated values
  214. char* start = json_users + 1; // Skip the opening bracket
  215. char* end;
  216. while((end = strchr(start, ',')) != NULL &&
  217. flip_social_message_users->count < MAX_MESSAGE_USERS) {
  218. *end = '\0'; // Null-terminate the current token
  219. // Remove quotes
  220. if(*start == '"') start++;
  221. if(*(end - 1) == '"') *(end - 1) = '\0';
  222. // Copy username to pre-allocated memory
  223. snprintf(
  224. flip_social_message_users->usernames[flip_social_message_users->count],
  225. MAX_USER_LENGTH,
  226. "%s",
  227. start);
  228. flip_social_message_users->count++;
  229. start = end + 1;
  230. }
  231. // Handle the last token
  232. if(*start != '\0' && flip_social_message_users->count < MAX_MESSAGE_USERS) {
  233. if(*start == '"') start++;
  234. if(*(start + strlen(start) - 1) == ']') *(start + strlen(start) - 1) = '\0';
  235. if(*(start + strlen(start) - 1) == '"') *(start + strlen(start) - 1) = '\0';
  236. snprintf(
  237. flip_social_message_users->usernames[flip_social_message_users->count],
  238. MAX_USER_LENGTH,
  239. "%s",
  240. start);
  241. flip_social_message_users->count++;
  242. }
  243. // Add submenu items for the users
  244. flip_social_update_messages_submenu();
  245. // Free the JSON data
  246. free(json_users);
  247. free(start);
  248. free(end);
  249. furi_string_free(message_data);
  250. free(data_cstr);
  251. return true;
  252. }
  253. // Parse the users that the logged in user can message
  254. bool flip_social_parse_json_message_user_choices() {
  255. // load the received data from the saved file
  256. FuriString* user_data = flipper_http_load_from_file(fhttp.file_path);
  257. if(user_data == NULL) {
  258. FURI_LOG_E(TAG, "Failed to load received data from file.");
  259. return false;
  260. }
  261. char* data_cstr = (char*)furi_string_get_cstr(user_data);
  262. if(data_cstr == NULL) {
  263. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  264. furi_string_free(user_data);
  265. return false;
  266. }
  267. // Allocate memory for each username only if not already allocated
  268. flip_social_explore = flip_social_explore_alloc();
  269. if(flip_social_explore == NULL) {
  270. FURI_LOG_E(TAG, "Failed to allocate memory for explore usernames.");
  271. furi_string_free(user_data);
  272. free(data_cstr);
  273. return false;
  274. }
  275. // Remove newlines
  276. char* pos = data_cstr;
  277. while((pos = strchr(pos, '\n')) != NULL) {
  278. *pos = ' ';
  279. }
  280. // Initialize explore count
  281. flip_social_explore->count = 0;
  282. // Extract the users array from the JSON
  283. char* json_users = get_json_value("users", data_cstr, MAX_TOKENS);
  284. if(json_users == NULL) {
  285. FURI_LOG_E(TAG, "Failed to parse users array.");
  286. furi_string_free(user_data);
  287. free(data_cstr);
  288. return false;
  289. }
  290. // Manual tokenization for comma-separated values
  291. char* start = json_users + 1; // Skip the opening bracket
  292. char* end;
  293. while((end = strchr(start, ',')) != NULL && flip_social_explore->count < MAX_EXPLORE_USERS) {
  294. *end = '\0'; // Null-terminate the current token
  295. // Remove quotes
  296. if(*start == '"') start++;
  297. if(*(end - 1) == '"') *(end - 1) = '\0';
  298. // Copy username to pre-allocated memory
  299. snprintf(
  300. flip_social_explore->usernames[flip_social_explore->count],
  301. MAX_USER_LENGTH,
  302. "%s",
  303. start);
  304. flip_social_explore->count++;
  305. start = end + 1;
  306. }
  307. // Handle the last token
  308. if(*start != '\0' && flip_social_explore->count < MAX_EXPLORE_USERS) {
  309. if(*start == '"') start++;
  310. if(*(start + strlen(start) - 1) == ']') *(start + strlen(start) - 1) = '\0';
  311. if(*(start + strlen(start) - 1) == '"') *(start + strlen(start) - 1) = '\0';
  312. snprintf(
  313. flip_social_explore->usernames[flip_social_explore->count],
  314. MAX_USER_LENGTH,
  315. "%s",
  316. start);
  317. flip_social_explore->count++;
  318. }
  319. // Add submenu items for the users
  320. flip_social_update_submenu_user_choices();
  321. // Free the JSON data
  322. free(json_users);
  323. free(start);
  324. free(end);
  325. furi_string_free(user_data);
  326. free(data_cstr);
  327. return true;
  328. }
  329. // parse messages between the logged in user and the selected user
  330. bool flip_social_parse_json_messages() {
  331. // load the received data from the saved file
  332. FuriString* message_data = flipper_http_load_from_file(fhttp.file_path);
  333. if(message_data == NULL) {
  334. FURI_LOG_E(TAG, "Failed to load received data from file.");
  335. return false;
  336. }
  337. char* data_cstr = (char*)furi_string_get_cstr(message_data);
  338. if(data_cstr == NULL) {
  339. FURI_LOG_E(TAG, "Failed to get C-string from FuriString.");
  340. furi_string_free(message_data);
  341. return false;
  342. }
  343. // Allocate memory for each message only if not already allocated
  344. flip_social_messages = flip_social_user_messages_alloc();
  345. if(!flip_social_messages) {
  346. FURI_LOG_E(TAG, "Failed to allocate memory for messages.");
  347. furi_string_free(message_data);
  348. free(data_cstr);
  349. return false;
  350. }
  351. // Remove newlines
  352. char* pos = data_cstr;
  353. while((pos = strchr(pos, '\n')) != NULL) {
  354. *pos = ' ';
  355. }
  356. // Initialize messages count
  357. flip_social_messages->count = 0;
  358. // Iterate through the messages array
  359. for(int i = 0; i < MAX_MESSAGES; i++) {
  360. // Parse each item in the array
  361. char* item = get_json_array_value("conversations", i, data_cstr, MAX_TOKENS);
  362. if(item == NULL) {
  363. break;
  364. }
  365. // Extract individual fields from the JSON object
  366. char* sender = get_json_value("sender", item, MAX_TOKENS);
  367. char* content = get_json_value("content", item, MAX_TOKENS);
  368. if(sender == NULL || content == NULL) {
  369. FURI_LOG_E(TAG, "Failed to parse item fields.");
  370. free(item);
  371. continue;
  372. }
  373. // Store parsed values in pre-allocated memory
  374. snprintf(flip_social_messages->usernames[i], MAX_USER_LENGTH, "%s", sender);
  375. snprintf(flip_social_messages->messages[i], MAX_MESSAGE_LENGTH, "%s", content);
  376. flip_social_messages->count++;
  377. free(item);
  378. free(sender);
  379. free(content);
  380. }
  381. furi_string_free(message_data);
  382. free(data_cstr);
  383. return true;
  384. }