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.

165 lines
4.8 KiB

  1. import 'dart:convert';
  2. import 'package:Envelope/components/flash_message.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_dotenv/flutter_dotenv.dart';
  5. import 'package:http/http.dart' as http;
  6. import 'package:pointycastle/export.dart';
  7. import 'package:sqflite/sqflite.dart';
  8. import '/models/conversation_users.dart';
  9. import '/models/conversations.dart';
  10. import '/models/my_profile.dart';
  11. import '/utils/encryption/aes_helper.dart';
  12. import '/utils/storage/database.dart';
  13. import '/utils/storage/session_cookie.dart';
  14. Future<void> updateConversation(Conversation conversation, { includeUsers = true } ) async {
  15. String sessionCookie = await getSessionCookie();
  16. Map<String, dynamic> conversationJson = await conversation.payloadJson(includeUsers: includeUsers);
  17. var x = await http.put(
  18. Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversations'),
  19. headers: <String, String>{
  20. 'Content-Type': 'application/json; charset=UTF-8',
  21. 'cookie': sessionCookie,
  22. },
  23. body: jsonEncode(conversationJson),
  24. );
  25. // TODO: Handle errors here
  26. print(x.statusCode);
  27. }
  28. // TODO: Refactor this function
  29. Future<void> updateConversations() async {
  30. RSAPrivateKey privKey = await MyProfile.getPrivateKey();
  31. // try {
  32. var resp = await http.get(
  33. Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversations'),
  34. headers: {
  35. 'cookie': await getSessionCookie(),
  36. }
  37. );
  38. if (resp.statusCode != 200) {
  39. throw Exception(resp.body);
  40. }
  41. List<Conversation> conversations = [];
  42. List<String> conversationsDetailIds = [];
  43. List<dynamic> conversationsJson = jsonDecode(resp.body);
  44. if (conversationsJson.isEmpty) {
  45. return;
  46. }
  47. for (var i = 0; i < conversationsJson.length; i++) {
  48. Conversation conversation = Conversation.fromJson(
  49. conversationsJson[i] as Map<String, dynamic>,
  50. privKey,
  51. );
  52. conversations.add(conversation);
  53. conversationsDetailIds.add(conversation.id);
  54. }
  55. Map<String, String> params = {};
  56. params['conversation_detail_ids'] = conversationsDetailIds.join(',');
  57. var uri = Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversation_details');
  58. uri = uri.replace(queryParameters: params);
  59. resp = await http.get(
  60. uri,
  61. headers: {
  62. 'cookie': await getSessionCookie(),
  63. }
  64. );
  65. if (resp.statusCode != 200) {
  66. throw Exception(resp.body);
  67. }
  68. final db = await getDatabaseConnection();
  69. List<dynamic> conversationsDetailsJson = jsonDecode(resp.body);
  70. for (var i = 0; i < conversationsDetailsJson.length; i++) {
  71. var conversationDetailJson = conversationsDetailsJson[i] as Map<String, dynamic>;
  72. var conversation = findConversationByDetailId(conversations, conversationDetailJson['id']);
  73. conversation.twoUser = AesHelper.aesDecrypt(
  74. base64.decode(conversation.symmetricKey),
  75. base64.decode(conversationDetailJson['two_user']),
  76. ) == 'true';
  77. if (conversation.twoUser) {
  78. MyProfile profile = await MyProfile.getProfile();
  79. final db = await getDatabaseConnection();
  80. List<Map<String, dynamic>> maps = await db.query(
  81. 'conversation_users',
  82. where: 'conversation_id = ? AND user_id != ?',
  83. whereArgs: [ conversation.id, profile.id ],
  84. );
  85. if (maps.length != 1) {
  86. throw ArgumentError('Invalid user id');
  87. }
  88. conversation.name = maps[0]['username'];
  89. } else {
  90. conversation.name = AesHelper.aesDecrypt(
  91. base64.decode(conversation.symmetricKey),
  92. base64.decode(conversationDetailJson['name']),
  93. );
  94. }
  95. await db.insert(
  96. 'conversations',
  97. conversation.toMap(),
  98. conflictAlgorithm: ConflictAlgorithm.replace,
  99. );
  100. List<dynamic> usersData = conversationDetailJson['users'];
  101. for (var i = 0; i < usersData.length; i++) {
  102. ConversationUser conversationUser = ConversationUser.fromJson(
  103. usersData[i] as Map<String, dynamic>,
  104. base64.decode(conversation.symmetricKey),
  105. );
  106. await db.insert(
  107. 'conversation_users',
  108. conversationUser.toMap(),
  109. conflictAlgorithm: ConflictAlgorithm.replace,
  110. );
  111. }
  112. }
  113. // } catch (SocketException) {
  114. // return;
  115. // }
  116. }
  117. Future<void> uploadConversation(Conversation conversation, BuildContext context) async {
  118. String sessionCookie = await getSessionCookie();
  119. Map<String, dynamic> conversationJson = await conversation.payloadJson();
  120. var x = await http.post(
  121. Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/conversations'),
  122. headers: <String, String>{
  123. 'Content-Type': 'application/json; charset=UTF-8',
  124. 'cookie': sessionCookie,
  125. },
  126. body: jsonEncode(conversationJson),
  127. );
  128. if (x.statusCode != 200) {
  129. showMessage('Failed to create conversation', context);
  130. }
  131. }