import 'dart:typed_data'; import '/database/models/conversation_users.dart'; import '/database/models/conversations.dart'; import '/utils/storage/database.dart'; import '/utils/encryption/aes_helper.dart'; import '/utils/encryption/crypto_utils.dart'; class ConversationUsersRepository { static Future getConversationUser(Conversation conversation, String userId) async { final db = await getDatabaseConnection(); final List> maps = await db.query( 'conversation_users', where: 'conversation_id = ? AND user_id = ?', whereArgs: [ conversation.id, userId ], ); if (maps.length != 1) { throw ArgumentError('Invalid conversation_id or username'); } return ConversationUser( id: maps[0]['id'], userId: maps[0]['user_id'], conversationId: maps[0]['conversation_id'], username: maps[0]['username'], associationKey: maps[0]['association_key'], publicKey: CryptoUtils.rsaPublicKeyFromPem(maps[0]['asymmetric_public_key']), admin: maps[0]['admin'] == 1, ); } static Future> getConversationUsers(Conversation conversation) async { final db = await getDatabaseConnection(); final List> maps = await db.query( 'conversation_users', where: 'conversation_id = ?', whereArgs: [ conversation.id ], orderBy: 'username', ); List conversationUsers = List.generate(maps.length, (i) { return ConversationUser( id: maps[i]['id'], userId: maps[i]['user_id'], conversationId: maps[i]['conversation_id'], username: maps[i]['username'], associationKey: maps[i]['association_key'], publicKey: CryptoUtils.rsaPublicKeyFromPem(maps[i]['asymmetric_public_key']), admin: maps[i]['admin'] == 1, ); }); int index = 0; List finalConversationUsers = []; for (ConversationUser conversationUser in conversationUsers) { if (!conversationUser.admin) { finalConversationUsers.add(conversationUser); continue; } finalConversationUsers.insert(index, conversationUser); index++; } return finalConversationUsers; } static Future>> getEncryptedConversationUsers(Conversation conversation, Uint8List symKey) async { final db = await getDatabaseConnection(); final List> maps = await db.query( 'conversation_users', where: 'conversation_id = ?', whereArgs: [conversation.id], orderBy: 'username', ); List> conversationUsers = List.generate(maps.length, (i) { return { 'id': maps[i]['id'], 'conversation_id': maps[i]['conversation_id'], 'user_id': AesHelper.aesEncrypt(symKey, Uint8List.fromList(maps[i]['user_id'].codeUnits)), 'username': AesHelper.aesEncrypt(symKey, Uint8List.fromList(maps[i]['username'].codeUnits)), 'association_key': AesHelper.aesEncrypt(symKey, Uint8List.fromList(maps[i]['association_key'].codeUnits)), 'public_key': AesHelper.aesEncrypt(symKey, Uint8List.fromList(maps[i]['asymmetric_public_key'].codeUnits)), 'admin': AesHelper.aesEncrypt(symKey, Uint8List.fromList((maps[i]['admin'] == 1 ? 'true' : 'false').codeUnits)), }; }); return conversationUsers; } }