veilidchat/lib/contacts/models/contact.dart

133 lines
4.5 KiB
Dart
Raw Normal View History

2023-08-05 21:01:27 -04:00
import 'dart:convert';
2023-08-02 21:09:28 -04:00
2023-08-04 01:00:38 -04:00
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
2023-08-02 21:09:28 -04:00
2023-12-26 20:26:54 -05:00
import '../../proto/proto.dart' as proto;
2023-08-05 23:58:13 -04:00
2023-12-27 22:56:24 -05:00
import '../../../packages/veilid_support/veilid_support.dart';
2023-12-26 20:26:54 -05:00
import '../../tools/tools.dart';
2023-08-02 21:09:28 -04:00
import 'account.dart';
2023-08-06 19:46:40 -04:00
import 'chat.dart';
2023-08-02 21:09:28 -04:00
2023-08-04 01:00:38 -04:00
part 'contact.g.dart';
2023-08-05 21:01:27 -04:00
Future<void> createContact({
required ActiveAccountInfo activeAccountInfo,
required proto.Profile profile,
required IdentityMaster remoteIdentity,
2023-08-07 11:02:29 -04:00
required TypedKey remoteConversationRecordKey,
required TypedKey localConversationRecordKey,
2023-08-05 21:01:27 -04:00
}) async {
final accountRecordKey =
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
// Create Contact
2023-12-18 20:05:23 -05:00
final contact = proto.Contact()
2023-08-05 21:01:27 -04:00
..editedProfile = profile
..remoteProfile = profile
2023-08-05 23:58:13 -04:00
..identityMasterJson = jsonEncode(remoteIdentity.toJson())
..identityPublicKey = TypedKey(
kind: remoteIdentity.identityRecordKey.kind,
value: remoteIdentity.identityPublicKey)
.toProto()
2023-08-07 11:02:29 -04:00
..remoteConversationRecordKey = remoteConversationRecordKey.toProto()
..localConversationRecordKey = localConversationRecordKey.toProto()
2023-08-05 21:01:27 -04:00
..showAvailability = false;
// Add Contact to account's list
// if this fails, don't keep retrying, user can try again later
await (await DHTShortArray.openOwned(
proto.OwnedDHTRecordPointerProto.fromProto(
activeAccountInfo.account.contactList),
parent: accountRecordKey))
.scope((contactList) async {
if (await contactList.tryAddItem(contact.writeToBuffer()) == false) {
2023-08-06 15:33:40 -04:00
throw Exception('Failed to add contact');
2023-08-05 13:50:31 -04:00
}
});
2023-08-05 12:38:03 -04:00
}
2023-08-05 23:58:13 -04:00
Future<void> deleteContact(
{required ActiveAccountInfo activeAccountInfo,
2023-12-18 20:05:23 -05:00
required proto.Contact contact}) async {
2023-08-05 23:58:13 -04:00
final pool = await DHTRecordPool.instance();
2023-08-04 01:00:38 -04:00
final accountRecordKey =
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
2023-09-28 12:51:44 -04:00
final localConversationKey =
proto.TypedKeyProto.fromProto(contact.localConversationRecordKey);
2023-08-06 19:46:40 -04:00
final remoteConversationKey =
2023-08-07 11:02:29 -04:00
proto.TypedKeyProto.fromProto(contact.remoteConversationRecordKey);
2023-08-06 19:46:40 -04:00
// Remove any chats for this contact
await deleteChat(
activeAccountInfo: activeAccountInfo,
remoteConversationRecordKey: remoteConversationKey);
2023-08-04 01:00:38 -04:00
2023-08-05 23:58:13 -04:00
// Remove Contact from account's list
2023-08-04 01:00:38 -04:00
await (await DHTShortArray.openOwned(
proto.OwnedDHTRecordPointerProto.fromProto(
2023-08-05 23:58:13 -04:00
activeAccountInfo.account.contactList),
2023-08-04 01:00:38 -04:00
parent: accountRecordKey))
2023-08-05 23:58:13 -04:00
.scope((contactList) async {
for (var i = 0; i < contactList.length; i++) {
final item =
await contactList.getItemProtobuf(proto.Contact.fromBuffer, i);
if (item == null) {
2023-08-06 15:33:40 -04:00
throw Exception('Failed to get contact');
2023-08-05 23:58:13 -04:00
}
2023-08-07 11:02:29 -04:00
if (item.remoteConversationRecordKey ==
contact.remoteConversationRecordKey) {
2023-08-05 23:58:13 -04:00
await contactList.tryRemoveItem(i);
break;
2023-08-04 01:00:38 -04:00
}
}
2023-09-28 12:51:44 -04:00
try {
await (await pool.openRead(localConversationKey,
parent: accountRecordKey))
.delete();
} on Exception catch (e) {
log.debug('error removing local conversation record key: $e', e);
}
try {
if (localConversationKey != remoteConversationKey) {
await (await pool.openRead(remoteConversationKey,
parent: accountRecordKey))
.delete();
}
} on Exception catch (e) {
log.debug('error removing remote conversation record key: $e', e);
2023-09-28 12:51:44 -04:00
}
2023-08-04 01:00:38 -04:00
});
}
/// Get the active account contact list
@riverpod
2023-12-18 20:05:23 -05:00
Future<IList<proto.Contact>?> fetchContactList(FetchContactListRef ref) async {
2023-08-04 01:00:38 -04:00
// See if we've logged into this account or if it is locked
final activeAccountInfo = await ref.watch(fetchActiveAccountProvider.future);
if (activeAccountInfo == null) {
return null;
}
final accountRecordKey =
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
// Decode the contact list from the DHT
2023-12-18 20:05:23 -05:00
IList<proto.Contact> out = const IListConst([]);
2023-08-04 01:00:38 -04:00
await (await DHTShortArray.openOwned(
proto.OwnedDHTRecordPointerProto.fromProto(
activeAccountInfo.account.contactList),
parent: accountRecordKey))
.scope((cList) async {
for (var i = 0; i < cList.length; i++) {
final cir = await cList.getItem(i);
if (cir == null) {
2023-08-06 15:33:40 -04:00
throw Exception('Failed to get contact');
2023-08-04 01:00:38 -04:00
}
2023-12-18 20:05:23 -05:00
out = out.add(proto.Contact.fromBuffer(cir));
2023-08-04 01:00:38 -04:00
}
});
return out;
}