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.

112 lines
3.4 KiB

  1. import 'dart:convert';
  2. import 'package:uuid/uuid.dart';
  3. import 'package:Envelope/models/conversation_users.dart';
  4. import 'package:intl/intl.dart';
  5. import 'package:http/http.dart' as http;
  6. import 'package:flutter_dotenv/flutter_dotenv.dart';
  7. import 'package:pointycastle/export.dart';
  8. import 'package:sqflite/sqflite.dart';
  9. import 'package:shared_preferences/shared_preferences.dart';
  10. import '/utils/storage/session_cookie.dart';
  11. import '/utils/storage/encryption_keys.dart';
  12. import '/utils/storage/database.dart';
  13. import '/models/conversations.dart';
  14. import '/models/messages.dart';
  15. Future<void> updateMessageThread(Conversation conversation, {RSAPrivateKey? privKey}) async {
  16. privKey ??= await getPrivateKey();
  17. final preferences = await SharedPreferences.getInstance();
  18. String username = preferences.getString('username')!;
  19. ConversationUser currentUser = await getConversationUserByUsername(conversation, username);
  20. var resp = await http.get(
  21. Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/messages/${currentUser.associationKey}'),
  22. headers: {
  23. 'cookie': await getSessionCookie(),
  24. }
  25. );
  26. if (resp.statusCode != 200) {
  27. throw Exception(resp.body);
  28. }
  29. List<dynamic> messageThreadJson = jsonDecode(resp.body);
  30. final db = await getDatabaseConnection();
  31. for (var i = 0; i < messageThreadJson.length; i++) {
  32. Message message = Message.fromJson(
  33. messageThreadJson[i] as Map<String, dynamic>,
  34. privKey,
  35. );
  36. ConversationUser messageUser = await getConversationUserById(conversation, message.senderId);
  37. message.senderUsername = messageUser.username;
  38. await db.insert(
  39. 'messages',
  40. message.toMap(),
  41. conflictAlgorithm: ConflictAlgorithm.replace,
  42. );
  43. }
  44. }
  45. Future<void> updateMessageThreads({List<Conversation>? conversations}) async {
  46. RSAPrivateKey privKey = await getPrivateKey();
  47. conversations ??= await getConversations();
  48. for (var i = 0; i < conversations.length; i++) {
  49. await updateMessageThread(conversations[i], privKey: privKey);
  50. }
  51. }
  52. Future<void> sendMessage(Conversation conversation, String data) async {
  53. final preferences = await SharedPreferences.getInstance();
  54. final userId = preferences.getString('userId');
  55. final username = preferences.getString('username');
  56. if (userId == null || username == null) {
  57. throw Exception('Invalid user id');
  58. }
  59. var uuid = const Uuid();
  60. final String messageDataId = uuid.v4();
  61. ConversationUser currentUser = await getConversationUserByUsername(conversation, username);
  62. Message message = Message(
  63. id: messageDataId,
  64. symmetricKey: '',
  65. userSymmetricKey: '',
  66. senderId: userId,
  67. senderUsername: username,
  68. data: data,
  69. createdAt: DateTime.now().toIso8601String(),
  70. associationKey: currentUser.associationKey,
  71. );
  72. final db = await getDatabaseConnection();
  73. print(await db.query('messages'));
  74. await db.insert(
  75. 'messages',
  76. message.toMap(),
  77. conflictAlgorithm: ConflictAlgorithm.replace,
  78. );
  79. String messageJson = await message.toJson(conversation, messageDataId);
  80. final resp = await http.post(
  81. Uri.parse('${dotenv.env["SERVER_URL"]}api/v1/auth/message'),
  82. headers: <String, String>{
  83. 'Content-Type': 'application/json; charset=UTF-8',
  84. 'cookie': await getSessionCookie(),
  85. },
  86. body: messageJson,
  87. );
  88. // TODO: If statusCode not successfull, mark as needing resend
  89. print(resp.statusCode);
  90. }