Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.5 KiB

  1. import 'dart:convert';
  2. import 'package:http/http.dart' as http;
  3. import 'package:flutter_dotenv/flutter_dotenv.dart';
  4. import 'package:pointycastle/export.dart';
  5. import 'package:sqflite/sqflite.dart';
  6. import '/models/my_profile.dart';
  7. import '/models/conversations.dart';
  8. import '/models/conversation_users.dart';
  9. import '/utils/storage/database.dart';
  10. import '/utils/storage/session_cookie.dart';
  11. import '/utils/encryption/aes_helper.dart';
  12. // TODO: Refactor this function
  13. Future<void> updateConversations() async {
  14. RSAPrivateKey privKey = await MyProfile.getPrivateKey();
  15. // try {
  16. var resp = await http.get(
  17. Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversations'),
  18. headers: {
  19. 'cookie': await getSessionCookie(),
  20. }
  21. );
  22. if (resp.statusCode != 200) {
  23. throw Exception(resp.body);
  24. }
  25. List<Conversation> conversations = [];
  26. List<String> conversationsDetailIds = [];
  27. List<dynamic> conversationsJson = jsonDecode(resp.body);
  28. for (var i = 0; i < conversationsJson.length; i++) {
  29. Conversation conversation = Conversation.fromJson(
  30. conversationsJson[i] as Map<String, dynamic>,
  31. privKey,
  32. );
  33. conversations.add(conversation);
  34. conversationsDetailIds.add(conversation.conversationDetailId);
  35. }
  36. Map<String, String> params = {};
  37. params['conversation_detail_ids'] = conversationsDetailIds.join(',');
  38. var uri = Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversation_details');
  39. uri = uri.replace(queryParameters: params);
  40. resp = await http.get(
  41. uri,
  42. headers: {
  43. 'cookie': await getSessionCookie(),
  44. }
  45. );
  46. if (resp.statusCode != 200) {
  47. throw Exception(resp.body);
  48. }
  49. final db = await getDatabaseConnection();
  50. List<dynamic> conversationsDetailsJson = jsonDecode(resp.body);
  51. for (var i = 0; i < conversationsDetailsJson.length; i++) {
  52. var conversationDetailJson = conversationsDetailsJson[i] as Map<String, dynamic>;
  53. var conversation = findConversationByDetailId(conversations, conversationDetailJson['id']);
  54. conversation.name = AesHelper.aesDecrypt(
  55. base64.decode(conversation.symmetricKey),
  56. base64.decode(conversationDetailJson['name']),
  57. );
  58. await db.insert(
  59. 'conversations',
  60. conversation.toMap(),
  61. conflictAlgorithm: ConflictAlgorithm.replace,
  62. );
  63. List<dynamic> usersData = json.decode(
  64. AesHelper.aesDecrypt(
  65. base64.decode(conversation.symmetricKey),
  66. base64.decode(conversationDetailJson['users']),
  67. )
  68. );
  69. for (var i = 0; i < usersData.length; i++) {
  70. ConversationUser conversationUser = ConversationUser.fromJson(
  71. usersData[i] as Map<String, dynamic>,
  72. conversation.id,
  73. );
  74. await db.insert(
  75. 'conversation_users',
  76. conversationUser.toMap(),
  77. conflictAlgorithm: ConflictAlgorithm.replace,
  78. );
  79. }
  80. }
  81. // } catch (SocketException) {
  82. // return;
  83. // }
  84. }
  85. Future<void> uploadConversation(Conversation conversation) async {
  86. String sessionCookie = await getSessionCookie();
  87. Map<String, dynamic> conversationJson = await conversation.toJson();
  88. var x = await http.post(
  89. Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversations'),
  90. headers: <String, String>{
  91. 'Content-Type': 'application/json; charset=UTF-8',
  92. 'cookie': sessionCookie,
  93. },
  94. body: jsonEncode(conversationJson),
  95. );
  96. print(x.statusCode);
  97. }