veilidchat/lib/contacts/cubits/contact_list_cubit.dart

156 lines
5.4 KiB
Dart
Raw Normal View History

2024-01-30 17:03:14 -05:00
import 'dart:async';
import 'dart:convert';
2024-06-15 00:01:08 -04:00
import 'package:async_tools/async_tools.dart';
import 'package:protobuf/protobuf.dart';
2024-01-30 17:03:14 -05:00
import 'package:veilid_support/veilid_support.dart';
import '../../account_manager/account_manager.dart';
import '../../proto/proto.dart' as proto;
import '../../tools/tools.dart';
2024-06-15 00:01:08 -04:00
import '../../conversation/cubits/conversation_cubit.dart';
2024-01-30 17:03:14 -05:00
//////////////////////////////////////////////////
// Mutable state for per-account contacts
class ContactListCubit extends DHTShortArrayCubit<proto.Contact> {
ContactListCubit({
required UnlockedAccountInfo unlockedAccountInfo,
2024-01-30 17:03:14 -05:00
required proto.Account account,
}) : _activeAccountInfo = unlockedAccountInfo,
super(
open: () => _open(unlockedAccountInfo, account),
2024-01-30 17:03:14 -05:00
decodeElement: proto.Contact.fromBuffer);
static Future<DHTShortArray> _open(
UnlockedAccountInfo activeAccountInfo, proto.Account account) async {
2024-01-30 17:03:14 -05:00
final accountRecordKey =
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
2024-02-12 09:10:07 -05:00
final contactListRecordKey = account.contactList.toVeilid();
2024-01-30 17:03:14 -05:00
final dhtRecord = await DHTShortArray.openOwned(contactListRecordKey,
debugName: 'ContactListCubit::_open::ContactList',
2024-01-30 17:03:14 -05:00
parent: accountRecordKey);
return dhtRecord;
}
2024-06-15 00:01:08 -04:00
@override
Future<void> close() async {
await _contactProfileUpdateMap.close();
await super.close();
}
////////////////////////////////////////////////////////////////////////////
// Public Interface
void followContactProfileChanges(TypedKey localConversationRecordKey,
Stream<proto.Profile?> profileStream, proto.Profile? profileState) {
_contactProfileUpdateMap
.follow(localConversationRecordKey, profileStream, profileState,
(remoteProfile) async {
if (remoteProfile == null) {
return;
}
return updateContactRemoteProfile(
localConversationRecordKey: localConversationRecordKey,
remoteProfile: remoteProfile);
});
}
Future<void> updateContactRemoteProfile({
required TypedKey localConversationRecordKey,
required proto.Profile remoteProfile,
}) async {
// Update contact's remoteProfile
await operateWriteEventual((writer) async {
for (var pos = 0; pos < writer.length; pos++) {
final c = await writer.getProtobuf(proto.Contact.fromBuffer, pos);
if (c != null &&
c.localConversationRecordKey.toVeilid() ==
localConversationRecordKey) {
if (c.remoteProfile == remoteProfile) {
// Unchanged
break;
}
final newContact = c.deepCopy()..remoteProfile = remoteProfile;
final updated = await writer.tryWriteItemProtobuf(
proto.Contact.fromBuffer, pos, newContact);
if (!updated) {
throw DHTExceptionTryAgain();
}
}
}
});
}
2024-01-30 17:03:14 -05:00
Future<void> createContact({
required proto.Profile remoteProfile,
2024-06-07 14:42:04 -04:00
required SuperIdentity remoteSuperIdentity,
2024-01-30 17:03:14 -05:00
required TypedKey remoteConversationRecordKey,
required TypedKey localConversationRecordKey,
}) async {
// Create Contact
final contact = proto.Contact()
..editedProfile = remoteProfile
..remoteProfile = remoteProfile
2024-06-07 14:42:04 -04:00
..superIdentityJson = jsonEncode(remoteSuperIdentity.toJson())
..identityPublicKey =
remoteSuperIdentity.currentInstance.typedPublicKey.toProto()
2024-01-30 17:03:14 -05:00
..remoteConversationRecordKey = remoteConversationRecordKey.toProto()
..localConversationRecordKey = localConversationRecordKey.toProto()
..showAvailability = false;
// Add Contact to account's list
// if this fails, don't keep retrying, user can try again later
2024-04-01 09:04:54 -04:00
await operateWrite((writer) async {
2024-06-09 14:43:28 -04:00
await writer.add(contact.writeToBuffer());
2024-02-27 12:45:58 -05:00
});
2024-01-30 17:03:14 -05:00
}
Future<void> deleteContact({required proto.Contact contact}) async {
final remoteIdentityPublicKey = contact.identityPublicKey.toVeilid();
final localConversationRecordKey =
contact.localConversationRecordKey.toVeilid();
final remoteConversationRecordKey =
2024-02-12 09:10:07 -05:00
contact.remoteConversationRecordKey.toVeilid();
2024-01-30 17:03:14 -05:00
// Remove Contact from account's list
2024-05-13 10:03:25 -04:00
final deletedItem = await operateWrite((writer) async {
2024-04-01 09:04:54 -04:00
for (var i = 0; i < writer.length; i++) {
2024-05-28 22:01:50 -04:00
final item = await writer.getProtobuf(proto.Contact.fromBuffer, i);
2024-02-27 12:45:58 -05:00
if (item == null) {
throw Exception('Failed to get contact');
}
2024-05-27 18:04:00 -04:00
if (item.localConversationRecordKey ==
contact.localConversationRecordKey) {
2024-05-28 22:01:50 -04:00
await writer.remove(i);
2024-05-13 10:03:25 -04:00
return item;
2024-02-27 12:45:58 -05:00
}
2024-01-30 17:03:14 -05:00
}
2024-03-28 22:46:26 -04:00
return null;
});
2024-05-13 10:03:25 -04:00
if (deletedItem != null) {
2024-02-27 12:45:58 -05:00
try {
// Make a conversation cubit to manipulate the conversation
final conversationCubit = ConversationCubit(
activeAccountInfo: _activeAccountInfo,
remoteIdentityPublicKey: remoteIdentityPublicKey,
localConversationRecordKey: localConversationRecordKey,
remoteConversationRecordKey: remoteConversationRecordKey,
);
// Delete the local and remote conversation records
await conversationCubit.delete();
2024-02-27 12:45:58 -05:00
} on Exception catch (e) {
log.debug('error deleting conversation records: $e', e);
2024-01-30 17:03:14 -05:00
}
2024-03-28 22:46:26 -04:00
}
2024-01-30 17:03:14 -05:00
}
final UnlockedAccountInfo _activeAccountInfo;
2024-06-15 00:01:08 -04:00
final _contactProfileUpdateMap =
SingleStateProcessorMap<TypedKey, proto.Profile?>();
2024-01-30 17:03:14 -05:00
}