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.

177 lines
5.0 KiB

  1. import 'dart:convert';
  2. import 'dart:io';
  3. import 'package:Envelope/database/repositories/conversation_users_repository.dart';
  4. import 'package:Envelope/database/repositories/conversations_repository.dart';
  5. import 'package:http/http.dart' as http;
  6. import 'package:sqflite/sqflite.dart';
  7. import 'package:uuid/uuid.dart';
  8. import '/database/models/messages.dart';
  9. import '/database/models/image_message.dart';
  10. import '/database/models/text_messages.dart';
  11. import '/database/models/conversation_users.dart';
  12. import '/database/models/conversations.dart';
  13. import '/database/models/my_profile.dart';
  14. import '/utils/storage/database.dart';
  15. import '/utils/storage/session_cookie.dart';
  16. import '/utils/storage/write_file.dart';
  17. class MessagesService {
  18. static Future<void> sendMessage(Conversation conversation, {
  19. String? data,
  20. List<File> files = const []
  21. }) async {
  22. MyProfile profile = await MyProfile.getProfile();
  23. var uuid = const Uuid();
  24. ConversationUser currentUser = await ConversationUsersRepository.getConversationUser(conversation, profile.id);
  25. List<Message> messages = [];
  26. List<Map<String, dynamic>> messagesToSend = [];
  27. final db = await getDatabaseConnection();
  28. if (data != null) {
  29. TextMessage message = TextMessage(
  30. id: uuid.v4(),
  31. symmetricKey: '',
  32. userSymmetricKey: '',
  33. senderId: currentUser.userId,
  34. senderUsername: profile.username,
  35. associationKey: currentUser.associationKey,
  36. createdAt: DateTime.now().toIso8601String(),
  37. failedToSend: false,
  38. text: data,
  39. );
  40. messages.add(message);
  41. messagesToSend.add(await message.payloadJson(
  42. conversation,
  43. ));
  44. await db.insert(
  45. 'messages',
  46. message.toMap(),
  47. conflictAlgorithm: ConflictAlgorithm.replace,
  48. );
  49. }
  50. for (File file in files) {
  51. String messageId = uuid.v4();
  52. File writtenFile = await writeImage(
  53. messageId,
  54. file.readAsBytesSync(),
  55. );
  56. ImageMessage message = ImageMessage(
  57. id: messageId,
  58. symmetricKey: '',
  59. userSymmetricKey: '',
  60. senderId: currentUser.userId,
  61. senderUsername: profile.username,
  62. associationKey: currentUser.associationKey,
  63. createdAt: DateTime.now().toIso8601String(),
  64. failedToSend: false,
  65. file: writtenFile,
  66. );
  67. messages.add(message);
  68. messagesToSend.add(await message.payloadJson(
  69. conversation,
  70. ));
  71. await db.insert(
  72. 'messages',
  73. message.toMap(),
  74. conflictAlgorithm: ConflictAlgorithm.replace,
  75. );
  76. }
  77. String sessionCookie = await getSessionCookie();
  78. return http.post(
  79. await MyProfile.getServerUrl('api/v1/auth/message'),
  80. headers: <String, String>{
  81. 'Content-Type': 'application/json; charset=UTF-8',
  82. 'cookie': sessionCookie,
  83. },
  84. body: jsonEncode(messagesToSend),
  85. )
  86. .then((resp) {
  87. if (resp.statusCode != 204) {
  88. throw Exception('Unable to send message');
  89. }
  90. })
  91. .catchError((exception) {
  92. for (Message message in messages) {
  93. message.failedToSend = true;
  94. db.update(
  95. 'messages',
  96. message.toMap(),
  97. where: 'id = ?',
  98. whereArgs: [message.id],
  99. );
  100. }
  101. throw exception;
  102. });
  103. }
  104. static Future<void> updateMessageThread(Conversation conversation, {MyProfile? profile}) async {
  105. profile ??= await MyProfile.getProfile();
  106. ConversationUser currentUser = await ConversationUsersRepository.getConversationUser(conversation, profile.id);
  107. var resp = await http.get(
  108. await MyProfile.getServerUrl('api/v1/auth/messages/${currentUser.associationKey}'),
  109. headers: {
  110. 'cookie': await getSessionCookie(),
  111. }
  112. );
  113. if (resp.statusCode != 200) {
  114. throw Exception(resp.body);
  115. }
  116. List<dynamic> messageThreadJson = jsonDecode(resp.body);
  117. final db = await getDatabaseConnection();
  118. for (var i = 0; i < messageThreadJson.length; i++) {
  119. var messageJson = messageThreadJson[i] as Map<String, dynamic>;
  120. var message = messageJson['message_data']['attachment_id'] != null ?
  121. await ImageMessage.fromJson(
  122. messageJson,
  123. profile.privateKey!,
  124. ) :
  125. TextMessage.fromJson(
  126. messageJson,
  127. profile.privateKey!,
  128. );
  129. ConversationUser messageUser = await ConversationUsersRepository.getConversationUser(conversation, message.senderId);
  130. message.senderUsername = messageUser.username;
  131. await db.insert(
  132. 'messages',
  133. message.toMap(),
  134. conflictAlgorithm: ConflictAlgorithm.replace,
  135. );
  136. }
  137. }
  138. static Future<void> updateMessageThreads({List<Conversation>? conversations}) async {
  139. MyProfile profile = await MyProfile.getProfile();
  140. conversations ??= await ConversationsRepository.getConversations();
  141. for (var i = 0; i < conversations.length; i++) {
  142. await updateMessageThread(conversations[i], profile: profile);
  143. }
  144. }
  145. }