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.

142 lines
4.1 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. if (conversationsJson.isEmpty) {
  29. return;
  30. }
  31. for (var i = 0; i < conversationsJson.length; i++) {
  32. Conversation conversation = Conversation.fromJson(
  33. conversationsJson[i] as Map<String, dynamic>,
  34. privKey,
  35. );
  36. conversations.add(conversation);
  37. conversationsDetailIds.add(conversation.conversationDetailId);
  38. }
  39. Map<String, String> params = {};
  40. params['conversation_detail_ids'] = conversationsDetailIds.join(',');
  41. var uri = Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversation_details');
  42. uri = uri.replace(queryParameters: params);
  43. resp = await http.get(
  44. uri,
  45. headers: {
  46. 'cookie': await getSessionCookie(),
  47. }
  48. );
  49. if (resp.statusCode != 200) {
  50. throw Exception(resp.body);
  51. }
  52. final db = await getDatabaseConnection();
  53. List<dynamic> conversationsDetailsJson = jsonDecode(resp.body);
  54. for (var i = 0; i < conversationsDetailsJson.length; i++) {
  55. var conversationDetailJson = conversationsDetailsJson[i] as Map<String, dynamic>;
  56. var conversation = findConversationByDetailId(conversations, conversationDetailJson['id']);
  57. conversation.name = AesHelper.aesDecrypt(
  58. base64.decode(conversation.symmetricKey),
  59. base64.decode(conversationDetailJson['name']),
  60. );
  61. await db.insert(
  62. 'conversations',
  63. conversation.toMap(),
  64. conflictAlgorithm: ConflictAlgorithm.replace,
  65. );
  66. List<dynamic> usersData = json.decode(
  67. AesHelper.aesDecrypt(
  68. base64.decode(conversation.symmetricKey),
  69. base64.decode(conversationDetailJson['users']),
  70. )
  71. );
  72. for (var i = 0; i < usersData.length; i++) {
  73. ConversationUser conversationUser = ConversationUser.fromJson(
  74. usersData[i] as Map<String, dynamic>,
  75. conversation.id,
  76. );
  77. await db.insert(
  78. 'conversation_users',
  79. conversationUser.toMap(),
  80. conflictAlgorithm: ConflictAlgorithm.replace,
  81. );
  82. }
  83. }
  84. // } catch (SocketException) {
  85. // return;
  86. // }
  87. }
  88. Future<void> uploadConversation(Conversation conversation) async {
  89. String sessionCookie = await getSessionCookie();
  90. Map<String, dynamic> conversationJson = await conversation.payloadJson();
  91. var x = await http.post(
  92. Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversations'),
  93. headers: <String, String>{
  94. 'Content-Type': 'application/json; charset=UTF-8',
  95. 'cookie': sessionCookie,
  96. },
  97. body: jsonEncode(conversationJson),
  98. );
  99. // TODO: Handle errors here
  100. print(x.statusCode);
  101. }
  102. Future<void> updateConversation(Conversation conversation, { includeUsers = true } ) async {
  103. String sessionCookie = await getSessionCookie();
  104. Map<String, dynamic> conversationJson = await conversation.payloadJson(includeUsers: includeUsers);
  105. var x = await http.put(
  106. Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversations'),
  107. headers: <String, String>{
  108. 'Content-Type': 'application/json; charset=UTF-8',
  109. 'cookie': sessionCookie,
  110. },
  111. body: jsonEncode(conversationJson),
  112. );
  113. // TODO: Handle errors here
  114. print(x.statusCode);
  115. }