flip_social_messages.h 12 KB

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