|
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<ConversationUser> getConversationUser(Conversation conversation, String userId) async {
|
|
final db = await getDatabaseConnection();
|
|
|
|
final List<Map<String, dynamic>> 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<List<ConversationUser>> getConversationUsers(Conversation conversation) async {
|
|
final db = await getDatabaseConnection();
|
|
|
|
final List<Map<String, dynamic>> maps = await db.query(
|
|
'conversation_users',
|
|
where: 'conversation_id = ?',
|
|
whereArgs: [ conversation.id ],
|
|
orderBy: 'username',
|
|
);
|
|
|
|
List<ConversationUser> 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<ConversationUser> finalConversationUsers = [];
|
|
|
|
for (ConversationUser conversationUser in conversationUsers) {
|
|
if (!conversationUser.admin) {
|
|
finalConversationUsers.add(conversationUser);
|
|
continue;
|
|
}
|
|
|
|
finalConversationUsers.insert(index, conversationUser);
|
|
index++;
|
|
}
|
|
|
|
return finalConversationUsers;
|
|
}
|
|
|
|
static Future<List<Map<String, dynamic>>> getEncryptedConversationUsers(Conversation conversation, Uint8List symKey) async {
|
|
final db = await getDatabaseConnection();
|
|
|
|
final List<Map<String, dynamic>> maps = await db.query(
|
|
'conversation_users',
|
|
where: 'conversation_id = ?',
|
|
whereArgs: [conversation.id],
|
|
orderBy: 'username',
|
|
);
|
|
|
|
List<Map<String, dynamic>> 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;
|
|
}
|
|
}
|