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.

63 lines
1.8 KiB

import '/database/models/friends.dart';
import '/utils/encryption/crypto_utils.dart';
import '/utils/storage/database.dart';
class FriendsRepository {
static Future<Friend> getFriendByFriendId(String userId) async {
final db = await getDatabaseConnection();
final List<Map<String, dynamic>> maps = await db.query(
'friends',
where: 'friend_id = ?',
whereArgs: [userId],
);
if (maps.length != 1) {
throw ArgumentError('Invalid user id');
}
return Friend(
id: maps[0]['id'],
userId: maps[0]['user_id'],
friendId: maps[0]['friend_id'],
friendSymmetricKey: maps[0]['symmetric_key'],
publicKey: CryptoUtils.rsaPublicKeyFromPem(maps[0]['asymmetric_public_key']),
acceptedAt: maps[0]['accepted_at'] != null ? DateTime.parse(maps[0]['accepted_at']) : null,
username: maps[0]['username'],
);
}
static Future<List<Friend>> getFriends({bool? accepted}) async {
final db = await getDatabaseConnection();
String? where;
if (accepted == true) {
where = 'accepted_at IS NOT NULL';
}
if (accepted == false) {
where = 'accepted_at IS NULL';
}
final List<Map<String, dynamic>> maps = await db.query(
'friends',
where: where,
orderBy: 'accepted_at IS NOT NULL',
);
return List.generate(maps.length, (i) {
return Friend(
id: maps[i]['id'],
userId: maps[i]['user_id'],
friendId: maps[i]['friend_id'],
friendSymmetricKey: maps[i]['symmetric_key'],
publicKey: CryptoUtils.rsaPublicKeyFromPem(maps[i]['asymmetric_public_key']),
acceptedAt: maps[i]['accepted_at'] != null ? DateTime.parse(maps[i]['accepted_at']) : null,
username: maps[i]['username'],
);
});
}
}