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

  1. import '/database/models/friends.dart';
  2. import '/utils/encryption/crypto_utils.dart';
  3. import '/utils/storage/database.dart';
  4. class FriendsRepository {
  5. static Future<Friend> getFriendByFriendId(String userId) async {
  6. final db = await getDatabaseConnection();
  7. final List<Map<String, dynamic>> maps = await db.query(
  8. 'friends',
  9. where: 'friend_id = ?',
  10. whereArgs: [userId],
  11. );
  12. if (maps.length != 1) {
  13. throw ArgumentError('Invalid user id');
  14. }
  15. return Friend(
  16. id: maps[0]['id'],
  17. userId: maps[0]['user_id'],
  18. friendId: maps[0]['friend_id'],
  19. friendSymmetricKey: maps[0]['symmetric_key'],
  20. publicKey: CryptoUtils.rsaPublicKeyFromPem(maps[0]['asymmetric_public_key']),
  21. acceptedAt: maps[0]['accepted_at'] != null ? DateTime.parse(maps[0]['accepted_at']) : null,
  22. username: maps[0]['username'],
  23. );
  24. }
  25. static Future<List<Friend>> getFriends({bool? accepted}) async {
  26. final db = await getDatabaseConnection();
  27. String? where;
  28. if (accepted == true) {
  29. where = 'accepted_at IS NOT NULL';
  30. }
  31. if (accepted == false) {
  32. where = 'accepted_at IS NULL';
  33. }
  34. final List<Map<String, dynamic>> maps = await db.query(
  35. 'friends',
  36. where: where,
  37. orderBy: 'accepted_at IS NOT NULL',
  38. );
  39. return List.generate(maps.length, (i) {
  40. return Friend(
  41. id: maps[i]['id'],
  42. userId: maps[i]['user_id'],
  43. friendId: maps[i]['friend_id'],
  44. friendSymmetricKey: maps[i]['symmetric_key'],
  45. publicKey: CryptoUtils.rsaPublicKeyFromPem(maps[i]['asymmetric_public_key']),
  46. acceptedAt: maps[i]['accepted_at'] != null ? DateTime.parse(maps[i]['accepted_at']) : null,
  47. username: maps[i]['username'],
  48. );
  49. });
  50. }
  51. }