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-08-05 12:38:03 -04:00
|
|
|
import '../entities/identity.dart';
|
2023-08-02 21:09:28 -04:00
|
|
|
import '../entities/proto.dart' as proto;
|
2023-08-05 23:58:13 -04:00
|
|
|
import '../entities/proto.dart' show Contact;
|
|
|
|
|
2023-08-02 21:09:28 -04:00
|
|
|
import '../veilid_support/veilid_support.dart';
|
|
|
|
import 'account.dart';
|
|
|
|
|
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-05 23:58:13 -04:00
|
|
|
required TypedKey remoteConversationKey,
|
2023-08-05 21:01:27 -04:00
|
|
|
required OwnedDHTRecordPointer localConversation,
|
|
|
|
}) async {
|
|
|
|
final accountRecordKey =
|
|
|
|
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
|
|
|
|
|
|
|
// Create Contact
|
|
|
|
final contact = Contact()
|
|
|
|
..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()
|
|
|
|
..remoteConversationKey = remoteConversationKey.toProto()
|
2023-08-05 21:01:27 -04:00
|
|
|
..localConversation = localConversation.toProto()
|
|
|
|
..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) {
|
|
|
|
throw StateError('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,
|
|
|
|
required Contact contact}) async {
|
|
|
|
final pool = await DHTRecordPool.instance();
|
2023-08-04 01:00:38 -04:00
|
|
|
final accountRecordKey =
|
|
|
|
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
|
|
|
|
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) {
|
|
|
|
throw StateError('Failed to get contact');
|
|
|
|
}
|
|
|
|
if (item.remoteConversationKey == contact.remoteConversationKey) {
|
|
|
|
await contactList.tryRemoveItem(i);
|
|
|
|
break;
|
2023-08-04 01:00:38 -04:00
|
|
|
}
|
|
|
|
}
|
2023-08-05 23:58:13 -04:00
|
|
|
await (await pool.openOwned(
|
|
|
|
proto.OwnedDHTRecordPointerProto.fromProto(
|
|
|
|
contact.localConversation),
|
|
|
|
parent: accountRecordKey))
|
|
|
|
.delete();
|
|
|
|
await (await pool.openRead(
|
|
|
|
proto.TypedKeyProto.fromProto(contact.remoteConversationKey),
|
|
|
|
parent: accountRecordKey))
|
|
|
|
.delete();
|
2023-08-04 01:00:38 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Get the active account contact list
|
|
|
|
@riverpod
|
|
|
|
Future<IList<Contact>?> fetchContactList(FetchContactListRef ref) async {
|
|
|
|
// 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
|
|
|
|
IList<Contact> out = const IListConst([]);
|
|
|
|
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) {
|
|
|
|
throw StateError('Failed to get contact');
|
|
|
|
}
|
|
|
|
out = out.add(Contact.fromBuffer(cir));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|