- import 'dart:convert';
- import "package:pointycastle/export.dart";
- import '/utils/encryption/crypto_utils.dart';
- import '/utils/storage/database.dart';
-
- Friend findFriendByFriendId(List<Friend> friends, String id) {
- for (var friend in friends) {
- if (friend.friendId == id) {
- return friend;
- }
- }
- // Or return `null`.
- throw ArgumentError.value(id, "id", "No element with that id");
- }
-
- class Friend{
- String id;
- String userId;
- String username;
- String friendId;
- String friendSymmetricKey;
- String asymmetricPublicKey;
- String acceptedAt;
- bool? selected;
- Friend({
- required this.id,
- required this.userId,
- required this.username,
- required this.friendId,
- required this.friendSymmetricKey,
- required this.asymmetricPublicKey,
- required this.acceptedAt,
- this.selected,
- });
-
- factory Friend.fromJson(Map<String, dynamic> json, RSAPrivateKey privKey) {
- var friendIdDecrypted = CryptoUtils.rsaDecrypt(
- base64.decode(json['friend_id']),
- privKey,
- );
-
- var friendSymmetricKeyDecrypted = CryptoUtils.rsaDecrypt(
- base64.decode(json['symmetric_key']),
- privKey,
- );
-
- return Friend(
- id: json['id'],
- userId: json['user_id'],
- username: '',
- friendId: String.fromCharCodes(friendIdDecrypted),
- friendSymmetricKey: base64.encode(friendSymmetricKeyDecrypted),
- asymmetricPublicKey: '',
- acceptedAt: json['accepted_at'],
- );
- }
-
- @override
- String toString() {
- return '''
-
-
- id: $id
- userId: $userId
- username: $username
- friendId: $friendId
- accepted_at: $acceptedAt''';
- }
-
- Map<String, dynamic> toMap() {
- return {
- 'id': id,
- 'user_id': userId,
- 'username': username,
- 'friend_id': friendId,
- 'symmetric_key': friendSymmetricKey,
- 'asymmetric_public_key': asymmetricPublicKey,
- 'accepted_at': acceptedAt,
- };
- }
- }
-
-
- // A method that retrieves all the dogs from the dogs table.
- Future<List<Friend>> getFriends() async {
- final db = await getDatabaseConnection();
-
- final List<Map<String, dynamic>> maps = await db.query('friends');
-
- 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'],
- asymmetricPublicKey: maps[i]['asymmetric_public_key'],
- acceptedAt: maps[i]['accepted_at'],
- username: maps[i]['username'],
- );
- });
- }
-
- Future<Friend> getFriendByFriendId(String userId) async {
- final db = await getDatabaseConnection();
-
- List<dynamic> whereArguments = [userId];
-
- final List<Map<String, dynamic>> maps = await db.query(
- 'friends',
- where: 'friend_id = ?',
- whereArgs: whereArguments,
- );
-
- 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'],
- asymmetricPublicKey: maps[0]['asymmetric_public_key'],
- acceptedAt: maps[0]['accepted_at'],
- username: maps[0]['username'],
- );
- }
|