flip_social_messages.h 13 KB

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