mirror of
https://gitlab.com/veilid/veilidchat.git
synced 2025-06-06 21:58:49 -04:00
more refactor
This commit is contained in:
parent
0291ff7224
commit
b35b618a4d
10 changed files with 162 additions and 152 deletions
|
@ -13,7 +13,10 @@ class ActiveAccountInfo {
|
||||||
});
|
});
|
||||||
//
|
//
|
||||||
|
|
||||||
KeyPair getConversationWriter() {
|
TypedKey get accountRecordKey =>
|
||||||
|
userLogin.accountRecordInfo.accountRecord.recordKey;
|
||||||
|
|
||||||
|
KeyPair get conversationWriter {
|
||||||
final identityKey = localAccount.identityMaster.identityPublicKey;
|
final identityKey = localAccount.identityMaster.identityPublicKey;
|
||||||
final identitySecret = userLogin.identitySecret;
|
final identitySecret = userLogin.identitySecret;
|
||||||
return KeyPair(key: identityKey, secret: identitySecret.value);
|
return KeyPair(key: identityKey, secret: identitySecret.value);
|
||||||
|
|
|
@ -1 +1,2 @@
|
||||||
export 'views/views.dart';
|
export 'views/views.dart';
|
||||||
|
export 'repository/contact_invitation_repository.dart';
|
||||||
|
|
19
lib/contact_invitation/models/accepted_contact.dart
Normal file
19
lib/contact_invitation/models/accepted_contact.dart
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:veilid_support/veilid_support.dart';
|
||||||
|
|
||||||
|
import '../../proto/proto.dart' as proto;
|
||||||
|
|
||||||
|
@immutable
|
||||||
|
class AcceptedContact {
|
||||||
|
const AcceptedContact({
|
||||||
|
required this.profile,
|
||||||
|
required this.remoteIdentity,
|
||||||
|
required this.remoteConversationRecordKey,
|
||||||
|
required this.localConversationRecordKey,
|
||||||
|
});
|
||||||
|
|
||||||
|
final proto.Profile profile;
|
||||||
|
final IdentityMaster remoteIdentity;
|
||||||
|
final TypedKey remoteConversationRecordKey;
|
||||||
|
final TypedKey localConversationRecordKey;
|
||||||
|
}
|
2
lib/contact_invitation/models/models.dart
Normal file
2
lib/contact_invitation/models/models.dart
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
export 'accepted_contact.dart';
|
||||||
|
export 'valid_contact_invitation.dart';
|
|
@ -1,16 +1,11 @@
|
||||||
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
|
|
||||||
import 'package:fixnum/fixnum.dart';
|
import 'package:fixnum/fixnum.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
import 'package:veilid_support/veilid_support.dart';
|
||||||
import 'package:mutex/mutex.dart';
|
|
||||||
|
|
||||||
import '../../entities/entities.dart';
|
import '../../account_manager/account_manager.dart';
|
||||||
import '../../proto/proto.dart' as proto;
|
import '../../proto/proto.dart' as proto;
|
||||||
import '../../tools/tools.dart';
|
import '../../tools/tools.dart';
|
||||||
import '../../../packages/veilid_support/veilid_support.dart';
|
import '../models/models.dart';
|
||||||
import 'account.dart';
|
|
||||||
|
|
||||||
part 'contact_invitation_list_manager.g.dart';
|
|
||||||
|
|
||||||
//////////////////////////////////////////////////
|
//////////////////////////////////////////////////
|
||||||
|
|
||||||
|
@ -24,22 +19,6 @@ typedef GetEncryptionKeyCallback = Future<SecretKey?> Function(
|
||||||
EncryptionKeyType encryptionKeyType,
|
EncryptionKeyType encryptionKeyType,
|
||||||
Uint8List encryptedSecret);
|
Uint8List encryptedSecret);
|
||||||
|
|
||||||
//////////////////////////////////////////////////
|
|
||||||
@immutable
|
|
||||||
class AcceptedContact {
|
|
||||||
const AcceptedContact({
|
|
||||||
required this.profile,
|
|
||||||
required this.remoteIdentity,
|
|
||||||
required this.remoteConversationRecordKey,
|
|
||||||
required this.localConversationRecordKey,
|
|
||||||
});
|
|
||||||
|
|
||||||
final proto.Profile profile;
|
|
||||||
final IdentityMaster remoteIdentity;
|
|
||||||
final TypedKey remoteConversationRecordKey;
|
|
||||||
final TypedKey localConversationRecordKey;
|
|
||||||
}
|
|
||||||
|
|
||||||
@immutable
|
@immutable
|
||||||
class InvitationStatus {
|
class InvitationStatus {
|
||||||
const InvitationStatus({required this.acceptedContact});
|
const InvitationStatus({required this.acceptedContact});
|
||||||
|
@ -50,75 +29,65 @@ class InvitationStatus {
|
||||||
|
|
||||||
//////////////////////////////////////////////////
|
//////////////////////////////////////////////////
|
||||||
// Mutable state for per-account contact invitations
|
// Mutable state for per-account contact invitations
|
||||||
@riverpod
|
class ContactInvitationRepository {
|
||||||
class ContactInvitationListManager extends _$ContactInvitationListManager {
|
ContactInvitationRepository._({
|
||||||
ContactInvitationListManager._({
|
|
||||||
required ActiveAccountInfo activeAccountInfo,
|
required ActiveAccountInfo activeAccountInfo,
|
||||||
|
required proto.Account account,
|
||||||
required DHTShortArray dhtRecord,
|
required DHTShortArray dhtRecord,
|
||||||
}) : _activeAccountInfo = activeAccountInfo,
|
}) : _activeAccountInfo = activeAccountInfo,
|
||||||
_dhtRecord = dhtRecord,
|
_account = account,
|
||||||
_records = IList();
|
_dhtRecord = dhtRecord;
|
||||||
|
|
||||||
@override
|
static Future<ContactInvitationRepository> open(
|
||||||
FutureOr<IList<proto.ContactInvitationRecord>> build(
|
ActiveAccountInfo activeAccountInfo, proto.Account account) async {
|
||||||
ActiveAccountInfo activeAccountInfo) async {
|
|
||||||
// Load initial todo list from the remote repository
|
|
||||||
ref.onDispose xxxx call close and pass dhtrecord through... could use a context object
|
|
||||||
and a DHTValueChangeProvider that we watch in build that updates when dht records change
|
|
||||||
return _open(activeAccountInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
static Future<ContactInvitationListManager> _open(
|
|
||||||
ActiveAccountInfo activeAccountInfo) async {
|
|
||||||
final accountRecordKey =
|
final accountRecordKey =
|
||||||
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
||||||
|
|
||||||
final dhtRecord = await DHTShortArray.openOwned(
|
final contactInvitationListRecordKey =
|
||||||
proto.OwnedDHTRecordPointerProto.fromProto(
|
proto.OwnedDHTRecordPointerProto.fromProto(
|
||||||
activeAccountInfo.account.contactInvitationRecords),
|
account.contactInvitationRecords);
|
||||||
|
|
||||||
|
final dhtRecord = await DHTShortArray.openOwned(
|
||||||
|
contactInvitationListRecordKey,
|
||||||
parent: accountRecordKey);
|
parent: accountRecordKey);
|
||||||
|
|
||||||
return ContactInvitationListManager._(
|
return ContactInvitationRepository._(
|
||||||
activeAccountInfo: activeAccountInfo, dhtRecord: dhtRecord);
|
activeAccountInfo: activeAccountInfo,
|
||||||
|
account: account,
|
||||||
|
dhtRecord: dhtRecord);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> close() async {
|
Future<void> close() async {
|
||||||
state = "";
|
|
||||||
await _dhtRecord.close();
|
await _dhtRecord.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> refresh() async {
|
// Future<void> refresh() async {
|
||||||
for (var i = 0; i < _dhtRecord.length; i++) {
|
// for (var i = 0; i < _dhtRecord.length; i++) {
|
||||||
final cir = await _dhtRecord.getItem(i);
|
// final cir = await _dhtRecord.getItem(i);
|
||||||
if (cir == null) {
|
// if (cir == null) {
|
||||||
throw Exception('Failed to get contact invitation record');
|
// throw Exception('Failed to get contact invitation record');
|
||||||
}
|
// }
|
||||||
_records = _records.add(proto.ContactInvitationRecord.fromBuffer(cir));
|
// _records = _records.add(proto.ContactInvitationRecord.fromBuffer(cir));
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
Future<Uint8List> createInvitation(
|
Future<Uint8List> createInvitation(
|
||||||
{required EncryptionKeyType encryptionKeyType,
|
{required EncryptionKeyType encryptionKeyType,
|
||||||
required String encryptionKey,
|
required String encryptionKey,
|
||||||
required String message,
|
required String message,
|
||||||
required Timestamp? expiration}) async {
|
required Timestamp? expiration}) async {
|
||||||
final pool = await DHTRecordPool.instance();
|
final pool = DHTRecordPool.instance;
|
||||||
final accountRecordKey =
|
|
||||||
_activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
|
||||||
final identityKey =
|
|
||||||
_activeAccountInfo.localAccount.identityMaster.identityPublicKey;
|
|
||||||
final identitySecret = _activeAccountInfo.userLogin.identitySecret.value;
|
|
||||||
|
|
||||||
// Generate writer keypair to share with new contact
|
// Generate writer keypair to share with new contact
|
||||||
final cs = await pool.veilid.bestCryptoSystem();
|
final cs = await pool.veilid.bestCryptoSystem();
|
||||||
final contactRequestWriter = await cs.generateKeyPair();
|
final contactRequestWriter = await cs.generateKeyPair();
|
||||||
final conversationWriter = _activeAccountInfo.getConversationWriter();
|
final conversationWriter = _activeAccountInfo.conversationWriter;
|
||||||
|
|
||||||
// Encrypt the writer secret with the encryption key
|
// Encrypt the writer secret with the encryption key
|
||||||
final encryptedSecret = await encryptionKeyType.encryptSecretToBytes(
|
final encryptedSecret = await encryptionKeyType.encryptSecretToBytes(
|
||||||
secret: contactRequestWriter.secret,
|
secret: contactRequestWriter.secret,
|
||||||
cryptoKind: cs.kind(),
|
cryptoKind: cs.kind(),
|
||||||
encryptionKey: encryptionKey,
|
encryptionKey: encryptionKey,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Create local chat DHT record with the account record key as its parent
|
// Create local chat DHT record with the account record key as its parent
|
||||||
|
@ -127,7 +96,7 @@ class ContactInvitationListManager extends _$ContactInvitationListManager {
|
||||||
// identity key
|
// identity key
|
||||||
late final Uint8List signedContactInvitationBytes;
|
late final Uint8List signedContactInvitationBytes;
|
||||||
await (await pool.create(
|
await (await pool.create(
|
||||||
parent: accountRecordKey,
|
parent: _activeAccountInfo.accountRecordKey,
|
||||||
schema: DHTSchema.smpl(oCnt: 0, members: [
|
schema: DHTSchema.smpl(oCnt: 0, members: [
|
||||||
DHTSchemaMember(mKey: conversationWriter.key, mCnt: 1)
|
DHTSchemaMember(mKey: conversationWriter.key, mCnt: 1)
|
||||||
])))
|
])))
|
||||||
|
@ -136,7 +105,7 @@ class ContactInvitationListManager extends _$ContactInvitationListManager {
|
||||||
// Make ContactRequestPrivate and encrypt with the writer secret
|
// Make ContactRequestPrivate and encrypt with the writer secret
|
||||||
final crpriv = proto.ContactRequestPrivate()
|
final crpriv = proto.ContactRequestPrivate()
|
||||||
..writerKey = contactRequestWriter.key.toProto()
|
..writerKey = contactRequestWriter.key.toProto()
|
||||||
..profile = _activeAccountInfo.account.profile
|
..profile = _account.profile
|
||||||
..identityMasterRecordKey =
|
..identityMasterRecordKey =
|
||||||
_activeAccountInfo.userLogin.accountMasterRecordKey.toProto()
|
_activeAccountInfo.userLogin.accountMasterRecordKey.toProto()
|
||||||
..chatRecordKey = localConversation.key.toProto()
|
..chatRecordKey = localConversation.key.toProto()
|
||||||
|
@ -152,7 +121,7 @@ class ContactInvitationListManager extends _$ContactInvitationListManager {
|
||||||
|
|
||||||
// Create DHT unicast inbox for ContactRequest
|
// Create DHT unicast inbox for ContactRequest
|
||||||
await (await pool.create(
|
await (await pool.create(
|
||||||
parent: accountRecordKey,
|
parent: _activeAccountInfo.accountRecordKey,
|
||||||
schema: DHTSchema.smpl(oCnt: 1, members: [
|
schema: DHTSchema.smpl(oCnt: 1, members: [
|
||||||
DHTSchemaMember(mCnt: 1, mKey: contactRequestWriter.key)
|
DHTSchemaMember(mCnt: 1, mKey: contactRequestWriter.key)
|
||||||
]),
|
]),
|
||||||
|
@ -168,8 +137,9 @@ class ContactInvitationListManager extends _$ContactInvitationListManager {
|
||||||
final cinvbytes = cinv.writeToBuffer();
|
final cinvbytes = cinv.writeToBuffer();
|
||||||
final scinv = proto.SignedContactInvitation()
|
final scinv = proto.SignedContactInvitation()
|
||||||
..contactInvitation = cinvbytes
|
..contactInvitation = cinvbytes
|
||||||
..identitySignature =
|
..identitySignature = (await cs.sign(
|
||||||
(await cs.sign(identityKey, identitySecret, cinvbytes)).toProto();
|
conversationWriter.key, conversationWriter.secret, cinvbytes))
|
||||||
|
.toProto();
|
||||||
signedContactInvitationBytes = scinv.writeToBuffer();
|
signedContactInvitationBytes = scinv.writeToBuffer();
|
||||||
|
|
||||||
// Create ContactInvitationRecord
|
// Create ContactInvitationRecord
|
||||||
|
@ -185,15 +155,9 @@ class ContactInvitationListManager extends _$ContactInvitationListManager {
|
||||||
|
|
||||||
// Add ContactInvitationRecord to account's list
|
// Add ContactInvitationRecord to account's list
|
||||||
// if this fails, don't keep retrying, user can try again later
|
// if this fails, don't keep retrying, user can try again later
|
||||||
await (await DHTShortArray.openOwned(
|
if (await _dhtRecord.tryAddItem(cinvrec.writeToBuffer()) == false) {
|
||||||
proto.OwnedDHTRecordPointerProto.fromProto(
|
throw Exception('Failed to add contact invitation record');
|
||||||
_activeAccountInfo.account.contactInvitationRecords),
|
}
|
||||||
parent: accountRecordKey))
|
|
||||||
.scope((cirList) async {
|
|
||||||
if (await cirList.tryAddItem(cinvrec.writeToBuffer()) == false) {
|
|
||||||
throw Exception('Failed to add contact invitation record');
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -203,77 +167,71 @@ class ContactInvitationListManager extends _$ContactInvitationListManager {
|
||||||
Future<void> deleteInvitation(
|
Future<void> deleteInvitation(
|
||||||
{required bool accepted,
|
{required bool accepted,
|
||||||
required proto.ContactInvitationRecord contactInvitationRecord}) async {
|
required proto.ContactInvitationRecord contactInvitationRecord}) async {
|
||||||
final pool = await DHTRecordPool.instance();
|
final pool = DHTRecordPool.instance;
|
||||||
final accountRecordKey =
|
final accountRecordKey =
|
||||||
_activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
_activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
||||||
|
|
||||||
// Remove ContactInvitationRecord from account's list
|
// Remove ContactInvitationRecord from account's list
|
||||||
await (await DHTShortArray.openOwned(
|
for (var i = 0; i < _dhtRecord.length; i++) {
|
||||||
|
final item = await _dhtRecord.getItemProtobuf(
|
||||||
|
proto.ContactInvitationRecord.fromBuffer, i);
|
||||||
|
if (item == null) {
|
||||||
|
throw Exception('Failed to get contact invitation record');
|
||||||
|
}
|
||||||
|
if (item.contactRequestInbox.recordKey ==
|
||||||
|
contactInvitationRecord.contactRequestInbox.recordKey) {
|
||||||
|
await _dhtRecord.tryRemoveItem(i);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
await (await pool.openOwned(
|
||||||
proto.OwnedDHTRecordPointerProto.fromProto(
|
proto.OwnedDHTRecordPointerProto.fromProto(
|
||||||
_activeAccountInfo.account.contactInvitationRecords),
|
contactInvitationRecord.contactRequestInbox),
|
||||||
parent: accountRecordKey))
|
parent: accountRecordKey))
|
||||||
.scope((cirList) async {
|
.scope((contactRequestInbox) async {
|
||||||
for (var i = 0; i < cirList.length; i++) {
|
// Wipe out old invitation so it shows up as invalid
|
||||||
final item = await cirList.getItemProtobuf(
|
await contactRequestInbox.tryWriteBytes(Uint8List(0));
|
||||||
proto.ContactInvitationRecord.fromBuffer, i);
|
await contactRequestInbox.delete();
|
||||||
if (item == null) {
|
|
||||||
throw Exception('Failed to get contact invitation record');
|
|
||||||
}
|
|
||||||
if (item.contactRequestInbox.recordKey ==
|
|
||||||
contactInvitationRecord.contactRequestInbox.recordKey) {
|
|
||||||
await cirList.tryRemoveItem(i);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
await (await pool.openOwned(
|
|
||||||
proto.OwnedDHTRecordPointerProto.fromProto(
|
|
||||||
contactInvitationRecord.contactRequestInbox),
|
|
||||||
parent: accountRecordKey))
|
|
||||||
.scope((contactRequestInbox) async {
|
|
||||||
// Wipe out old invitation so it shows up as invalid
|
|
||||||
await contactRequestInbox.tryWriteBytes(Uint8List(0));
|
|
||||||
await contactRequestInbox.delete();
|
|
||||||
});
|
|
||||||
if (!accepted) {
|
|
||||||
await (await pool.openRead(
|
|
||||||
proto.TypedKeyProto.fromProto(
|
|
||||||
contactInvitationRecord.localConversationRecordKey),
|
|
||||||
parent: accountRecordKey))
|
|
||||||
.delete();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
if (!accepted) {
|
||||||
|
await (await pool.openRead(
|
||||||
|
proto.TypedKeyProto.fromProto(
|
||||||
|
contactInvitationRecord.localConversationRecordKey),
|
||||||
|
parent: accountRecordKey))
|
||||||
|
.delete();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<ValidContactInvitation?> validateInvitation(
|
Future<ValidContactInvitation?> validateInvitation(
|
||||||
{required Uint8List inviteData,
|
{required Uint8List inviteData,
|
||||||
required GetEncryptionKeyCallback getEncryptionKeyCallback}) async {
|
required GetEncryptionKeyCallback getEncryptionKeyCallback}) async {
|
||||||
final accountRecordKey =
|
final pool = DHTRecordPool.instance;
|
||||||
_activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
|
||||||
|
|
||||||
|
// Get contact request inbox from invitation
|
||||||
final signedContactInvitation =
|
final signedContactInvitation =
|
||||||
proto.SignedContactInvitation.fromBuffer(inviteData);
|
proto.SignedContactInvitation.fromBuffer(inviteData);
|
||||||
|
|
||||||
final contactInvitationBytes =
|
final contactInvitationBytes =
|
||||||
Uint8List.fromList(signedContactInvitation.contactInvitation);
|
Uint8List.fromList(signedContactInvitation.contactInvitation);
|
||||||
final contactInvitation =
|
final contactInvitation =
|
||||||
proto.ContactInvitation.fromBuffer(contactInvitationBytes);
|
proto.ContactInvitation.fromBuffer(contactInvitationBytes);
|
||||||
|
|
||||||
final contactRequestInboxKey =
|
final contactRequestInboxKey =
|
||||||
proto.TypedKeyProto.fromProto(contactInvitation.contactRequestInboxKey);
|
proto.TypedKeyProto.fromProto(contactInvitation.contactRequestInboxKey);
|
||||||
|
|
||||||
ValidContactInvitation? out;
|
ValidContactInvitation? out;
|
||||||
|
|
||||||
final pool = await DHTRecordPool.instance();
|
|
||||||
final cs = await pool.veilid.getCryptoSystem(contactRequestInboxKey.kind);
|
final cs = await pool.veilid.getCryptoSystem(contactRequestInboxKey.kind);
|
||||||
|
|
||||||
// See if we're chatting to ourselves, if so, don't delete it here
|
// See if we're chatting to ourselves, if so, don't delete it here
|
||||||
|
final isSelf = _contactIdentityMaster.identityPublicKey ==
|
||||||
|
_activeAccountInfo.localAccount.identityMaster.identityPublicKey;
|
||||||
|
xxx this doesn't work and the upper one doesnt either
|
||||||
final isSelf = _records.indexWhere((cir) =>
|
final isSelf = _records.indexWhere((cir) =>
|
||||||
proto.TypedKeyProto.fromProto(cir.contactRequestInbox.recordKey) ==
|
proto.TypedKeyProto.fromProto(cir.contactRequestInbox.recordKey) ==
|
||||||
contactRequestInboxKey) !=
|
contactRequestInboxKey) !=
|
||||||
-1;
|
-1;
|
||||||
|
|
||||||
await (await pool.openRead(contactRequestInboxKey,
|
await (await pool.openRead(contactRequestInboxKey,
|
||||||
parent: accountRecordKey))
|
parent: _activeAccountInfo.accountRecordKey))
|
||||||
.maybeDeleteScope(!isSelf, (contactRequestInbox) async {
|
.maybeDeleteScope(!isSelf, (contactRequestInbox) async {
|
||||||
//
|
//
|
||||||
final contactRequest = await contactRequestInbox
|
final contactRequest = await contactRequestInbox
|
||||||
|
@ -315,8 +273,8 @@ class ContactInvitationListManager extends _$ContactInvitationListManager {
|
||||||
key: proto.CryptoKeyProto.fromProto(contactRequestPrivate.writerKey),
|
key: proto.CryptoKeyProto.fromProto(contactRequestPrivate.writerKey),
|
||||||
secret: writerSecret);
|
secret: writerSecret);
|
||||||
|
|
||||||
out = ValidContactInvitation._(
|
out = ValidContactInvitation(
|
||||||
contactInvitationManager: this,
|
activeAccountInfo: _activeAccountInfo,
|
||||||
signedContactInvitation: signedContactInvitation,
|
signedContactInvitation: signedContactInvitation,
|
||||||
contactInvitation: contactInvitation,
|
contactInvitation: contactInvitation,
|
||||||
contactRequestInboxKey: contactRequestInboxKey,
|
contactRequestInboxKey: contactRequestInboxKey,
|
||||||
|
@ -334,7 +292,7 @@ class ContactInvitationListManager extends _$ContactInvitationListManager {
|
||||||
required proto.ContactInvitationRecord contactInvitationRecord}) async {
|
required proto.ContactInvitationRecord contactInvitationRecord}) async {
|
||||||
// Open the contact request inbox
|
// Open the contact request inbox
|
||||||
try {
|
try {
|
||||||
final pool = await DHTRecordPool.instance();
|
final pool = DHTRecordPool.instance;
|
||||||
final accountRecordKey =
|
final accountRecordKey =
|
||||||
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
||||||
final writerKey =
|
final writerKey =
|
||||||
|
@ -436,6 +394,7 @@ class ContactInvitationListManager extends _$ContactInvitationListManager {
|
||||||
//
|
//
|
||||||
|
|
||||||
final ActiveAccountInfo _activeAccountInfo;
|
final ActiveAccountInfo _activeAccountInfo;
|
||||||
|
final proto.Account _account;
|
||||||
final DHTShortArray _dhtRecord;
|
final DHTShortArray _dhtRecord;
|
||||||
IList<proto.ContactInvitationRecord> _records;
|
//IList<proto.ContactInvitationRecord> _records;
|
||||||
}
|
}
|
|
@ -1,9 +1,19 @@
|
||||||
|
import 'package:meta/meta.dart';
|
||||||
|
import 'package:veilid_support/veilid_support.dart';
|
||||||
|
|
||||||
|
import '../../account_manager/account_manager.dart';
|
||||||
|
import '../../proto/proto.dart' as proto;
|
||||||
|
import '../../tools/tools.dart';
|
||||||
|
import '../models/models.dart';
|
||||||
|
import 'contact_invitation_repository.dart';
|
||||||
|
|
||||||
//////////////////////////////////////////////////
|
//////////////////////////////////////////////////
|
||||||
///
|
///
|
||||||
|
|
||||||
class ValidContactInvitation {
|
class ValidContactInvitation {
|
||||||
ValidContactInvitation._(
|
@internal
|
||||||
{required ContactInvitationListManager contactInvitationManager,
|
ValidContactInvitation(
|
||||||
|
{required ActiveAccountInfo activeAccountInfo,
|
||||||
required proto.SignedContactInvitation signedContactInvitation,
|
required proto.SignedContactInvitation signedContactInvitation,
|
||||||
required proto.ContactInvitation contactInvitation,
|
required proto.ContactInvitation contactInvitation,
|
||||||
required TypedKey contactRequestInboxKey,
|
required TypedKey contactRequestInboxKey,
|
||||||
|
@ -11,7 +21,7 @@ class ValidContactInvitation {
|
||||||
required proto.ContactRequestPrivate contactRequestPrivate,
|
required proto.ContactRequestPrivate contactRequestPrivate,
|
||||||
required IdentityMaster contactIdentityMaster,
|
required IdentityMaster contactIdentityMaster,
|
||||||
required KeyPair writer})
|
required KeyPair writer})
|
||||||
: _contactInvitationManager = contactInvitationManager,
|
: _activeAccountInfo = activeAccountInfo,
|
||||||
_signedContactInvitation = signedContactInvitation,
|
_signedContactInvitation = signedContactInvitation,
|
||||||
_contactInvitation = contactInvitation,
|
_contactInvitation = contactInvitation,
|
||||||
_contactRequestInboxKey = contactRequestInboxKey,
|
_contactRequestInboxKey = contactRequestInboxKey,
|
||||||
|
@ -21,14 +31,12 @@ class ValidContactInvitation {
|
||||||
_writer = writer;
|
_writer = writer;
|
||||||
|
|
||||||
Future<AcceptedContact?> accept() async {
|
Future<AcceptedContact?> accept() async {
|
||||||
final pool = await DHTRecordPool.instance();
|
final pool = DHTRecordPool.instance;
|
||||||
final activeAccountInfo = _contactInvitationManager._activeAccountInfo;
|
|
||||||
try {
|
try {
|
||||||
// Ensure we don't delete this if we're trying to chat to self
|
// Ensure we don't delete this if we're trying to chat to self
|
||||||
final isSelf = _contactIdentityMaster.identityPublicKey ==
|
final isSelf = _contactIdentityMaster.identityPublicKey ==
|
||||||
activeAccountInfo.localAccount.identityMaster.identityPublicKey;
|
_activeAccountInfo.localAccount.identityMaster.identityPublicKey;
|
||||||
final accountRecordKey =
|
final accountRecordKey = _activeAccountInfo.accountRecordKey;
|
||||||
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
|
||||||
|
|
||||||
return (await pool.openWrite(_contactRequestInboxKey, _writer,
|
return (await pool.openWrite(_contactRequestInboxKey, _writer,
|
||||||
parent: accountRecordKey))
|
parent: accountRecordKey))
|
||||||
|
@ -37,14 +45,14 @@ class ValidContactInvitation {
|
||||||
// Create local conversation key for this
|
// Create local conversation key for this
|
||||||
// contact and send via contact response
|
// contact and send via contact response
|
||||||
return createConversation(
|
return createConversation(
|
||||||
activeAccountInfo: activeAccountInfo,
|
activeAccountInfo: _activeAccountInfo,
|
||||||
remoteIdentityPublicKey:
|
remoteIdentityPublicKey:
|
||||||
_contactIdentityMaster.identityPublicTypedKey(),
|
_contactIdentityMaster.identityPublicTypedKey(),
|
||||||
callback: (localConversation) async {
|
callback: (localConversation) async {
|
||||||
final contactResponse = proto.ContactResponse()
|
final contactResponse = proto.ContactResponse()
|
||||||
..accept = true
|
..accept = true
|
||||||
..remoteConversationRecordKey = localConversation.key.toProto()
|
..remoteConversationRecordKey = localConversation.key.toProto()
|
||||||
..identityMasterRecordKey = activeAccountInfo
|
..identityMasterRecordKey = _activeAccountInfo
|
||||||
.localAccount.identityMaster.masterRecordKey
|
.localAccount.identityMaster.masterRecordKey
|
||||||
.toProto();
|
.toProto();
|
||||||
final contactResponseBytes = contactResponse.writeToBuffer();
|
final contactResponseBytes = contactResponse.writeToBuffer();
|
||||||
|
@ -53,9 +61,8 @@ class ValidContactInvitation {
|
||||||
.getCryptoSystem(_contactRequestInboxKey.kind);
|
.getCryptoSystem(_contactRequestInboxKey.kind);
|
||||||
|
|
||||||
final identitySignature = await cs.sign(
|
final identitySignature = await cs.sign(
|
||||||
activeAccountInfo
|
_activeAccountInfo.conversationWriter.key,
|
||||||
.localAccount.identityMaster.identityPublicKey,
|
_activeAccountInfo.conversationWriter.secret,
|
||||||
activeAccountInfo.userLogin.identitySecret.value,
|
|
||||||
contactResponseBytes);
|
contactResponseBytes);
|
||||||
|
|
||||||
final signedContactResponse = proto.SignedContactResponse()
|
final signedContactResponse = proto.SignedContactResponse()
|
||||||
|
@ -86,14 +93,13 @@ class ValidContactInvitation {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> reject() async {
|
Future<bool> reject() async {
|
||||||
final pool = await DHTRecordPool.instance();
|
final pool = DHTRecordPool.instance;
|
||||||
final activeAccountInfo = _contactInvitationManager._activeAccountInfo;
|
|
||||||
|
|
||||||
// Ensure we don't delete this if we're trying to chat to self
|
// Ensure we don't delete this if we're trying to chat to self
|
||||||
final isSelf = _contactIdentityMaster.identityPublicKey ==
|
final isSelf = _contactIdentityMaster.identityPublicKey ==
|
||||||
activeAccountInfo.localAccount.identityMaster.identityPublicKey;
|
_activeAccountInfo.localAccount.identityMaster.identityPublicKey;
|
||||||
final accountRecordKey =
|
final accountRecordKey =
|
||||||
activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
_activeAccountInfo.userLogin.accountRecordInfo.accountRecord.recordKey;
|
||||||
|
|
||||||
return (await pool.openWrite(_contactRequestInboxKey, _writer,
|
return (await pool.openWrite(_contactRequestInboxKey, _writer,
|
||||||
parent: accountRecordKey))
|
parent: accountRecordKey))
|
||||||
|
@ -103,14 +109,14 @@ class ValidContactInvitation {
|
||||||
|
|
||||||
final contactResponse = proto.ContactResponse()
|
final contactResponse = proto.ContactResponse()
|
||||||
..accept = false
|
..accept = false
|
||||||
..identityMasterRecordKey = activeAccountInfo
|
..identityMasterRecordKey = _activeAccountInfo
|
||||||
.localAccount.identityMaster.masterRecordKey
|
.localAccount.identityMaster.masterRecordKey
|
||||||
.toProto();
|
.toProto();
|
||||||
final contactResponseBytes = contactResponse.writeToBuffer();
|
final contactResponseBytes = contactResponse.writeToBuffer();
|
||||||
|
|
||||||
final identitySignature = await cs.sign(
|
final identitySignature = await cs.sign(
|
||||||
activeAccountInfo.localAccount.identityMaster.identityPublicKey,
|
_activeAccountInfo.conversationWriter.key,
|
||||||
activeAccountInfo.userLogin.identitySecret.value,
|
_activeAccountInfo.conversationWriter.secret,
|
||||||
contactResponseBytes);
|
contactResponseBytes);
|
||||||
|
|
||||||
final signedContactResponse = proto.SignedContactResponse()
|
final signedContactResponse = proto.SignedContactResponse()
|
||||||
|
@ -130,12 +136,12 @@ class ValidContactInvitation {
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
ContactInvitationListManager _contactInvitationManager;
|
final ActiveAccountInfo _activeAccountInfo;
|
||||||
|
final TypedKey _contactRequestInboxKey;
|
||||||
|
final IdentityMaster _contactIdentityMaster;
|
||||||
|
final KeyPair _writer;
|
||||||
proto.SignedContactInvitation _signedContactInvitation;
|
proto.SignedContactInvitation _signedContactInvitation;
|
||||||
proto.ContactInvitation _contactInvitation;
|
proto.ContactInvitation _contactInvitation;
|
||||||
TypedKey _contactRequestInboxKey;
|
|
||||||
proto.ContactRequest _contactRequest;
|
proto.ContactRequest _contactRequest;
|
||||||
proto.ContactRequestPrivate _contactRequestPrivate;
|
proto.ContactRequestPrivate _contactRequestPrivate;
|
||||||
IdentityMaster _contactIdentityMaster;
|
|
||||||
KeyPair _writer;
|
|
||||||
}
|
}
|
|
@ -7,7 +7,6 @@ import 'package:flutter_translate/flutter_translate.dart';
|
||||||
import 'package:go_router/go_router.dart';
|
import 'package:go_router/go_router.dart';
|
||||||
import 'package:veilid_support/veilid_support.dart';
|
import 'package:veilid_support/veilid_support.dart';
|
||||||
|
|
||||||
import '../../proto/proto.dart' as proto;
|
|
||||||
import '../account_manager/account_manager.dart';
|
import '../account_manager/account_manager.dart';
|
||||||
import '../chat/chat.dart';
|
import '../chat/chat.dart';
|
||||||
import '../theme/theme.dart';
|
import '../theme/theme.dart';
|
||||||
|
@ -115,7 +114,7 @@ class HomePageState extends State<HomePage> with TickerProviderStateMixin {
|
||||||
}
|
}
|
||||||
|
|
||||||
final account = AccountRepository.instance
|
final account = AccountRepository.instance
|
||||||
.getAccountInfo(accountMasterRecordKey: activeUserLogin);
|
.getAccountInfo(accountMasterRecordKey: activeUserLogin)!;
|
||||||
|
|
||||||
switch (account.status) {
|
switch (account.status) {
|
||||||
case AccountInfoStatus.noAccount:
|
case AccountInfoStatus.noAccount:
|
||||||
|
@ -152,7 +151,7 @@ class HomePageState extends State<HomePage> with TickerProviderStateMixin {
|
||||||
context,
|
context,
|
||||||
localAccounts,
|
localAccounts,
|
||||||
activeUserLogin,
|
activeUserLogin,
|
||||||
account.accountRecord!,
|
account.activeAccountInfo!.accountRecord,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
// ignore_for_file: prefer_const_constructors
|
|
||||||
|
|
||||||
import 'package:awesome_extensions/awesome_extensions.dart';
|
import 'package:awesome_extensions/awesome_extensions.dart';
|
||||||
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
|
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
|
@ -58,6 +56,8 @@ class AccountPageState extends State<AccountPage> {
|
||||||
final textTheme = theme.textTheme;
|
final textTheme = theme.textTheme;
|
||||||
final scale = theme.extension<ScaleScheme>()!;
|
final scale = theme.extension<ScaleScheme>()!;
|
||||||
|
|
||||||
|
final records = widget.account.contactInvitationRecords;
|
||||||
|
|
||||||
final contactInvitationRecordList =
|
final contactInvitationRecordList =
|
||||||
ref.watch(fetchContactInvitationRecordsProvider).asData?.value ??
|
ref.watch(fetchContactInvitationRecordsProvider).asData?.value ??
|
||||||
const IListConst([]);
|
const IListConst([]);
|
|
@ -497,6 +497,9 @@ class DHTShortArray {
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<bool> trySwapItem(int aPos, int bPos) async {
|
Future<bool> trySwapItem(int aPos, int bPos) async {
|
||||||
|
if (aPos == bPos) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
await _refreshHead(onlyUpdates: true);
|
await _refreshHead(onlyUpdates: true);
|
||||||
|
|
||||||
final oldHead = _DHTShortArrayCache.from(_head);
|
final oldHead = _DHTShortArrayCache.from(_head);
|
||||||
|
@ -517,6 +520,9 @@ class DHTShortArray {
|
||||||
_head = oldHead;
|
_head = oldHead;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A change happened, notify any listeners
|
||||||
|
_watchController?.sink.add(null);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -539,7 +545,12 @@ class DHTShortArray {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
return record!.get(subkey: recordSubkey);
|
final result = await record!.get(subkey: recordSubkey);
|
||||||
|
if (result != null) {
|
||||||
|
// A change happened, notify any listeners
|
||||||
|
_watchController?.sink.add(null);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
} on Exception catch (_) {
|
} on Exception catch (_) {
|
||||||
// Exception on write means state needs to be reverted
|
// Exception on write means state needs to be reverted
|
||||||
_head = oldHead;
|
_head = oldHead;
|
||||||
|
@ -575,6 +586,10 @@ class DHTShortArray {
|
||||||
_head = oldHead;
|
_head = oldHead;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A change happened, notify any listeners
|
||||||
|
_watchController?.sink.add(null);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -591,7 +606,13 @@ class DHTShortArray {
|
||||||
final record = await _getOrCreateLinkedRecord(recordNumber);
|
final record = await _getOrCreateLinkedRecord(recordNumber);
|
||||||
|
|
||||||
final recordSubkey = (index % _stride) + ((recordNumber == 0) ? 1 : 0);
|
final recordSubkey = (index % _stride) + ((recordNumber == 0) ? 1 : 0);
|
||||||
return record.tryWriteBytes(newValue, subkey: recordSubkey);
|
final result = await record.tryWriteBytes(newValue, subkey: recordSubkey);
|
||||||
|
|
||||||
|
if (result != null) {
|
||||||
|
// A change happened, notify any listeners
|
||||||
|
_watchController?.sink.add(null);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> eventualWriteItem(int pos, Uint8List newValue) async {
|
Future<void> eventualWriteItem(int pos, Uint8List newValue) async {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue