veilidchat/lib/contact_invitation/models/valid_contact_invitation.dart

127 lines
5.0 KiB
Dart
Raw Normal View History

2024-01-25 20:33:56 -05:00
import 'package:meta/meta.dart';
import 'package:veilid_support/veilid_support.dart';
import '../../account_manager/account_manager.dart';
import '../../conversation/conversation.dart';
2024-01-25 20:33:56 -05:00
import '../../proto/proto.dart' as proto;
import '../../tools/tools.dart';
2024-01-28 21:31:53 -05:00
import 'models.dart';
2024-01-25 20:33:56 -05:00
2024-01-18 19:44:15 -05:00
//////////////////////////////////////////////////
///
class ValidContactInvitation {
2024-01-25 20:33:56 -05:00
@internal
ValidContactInvitation(
2024-06-18 21:20:06 -04:00
{required AccountInfo accountInfo,
2024-01-18 19:44:15 -05:00
required TypedKey contactRequestInboxKey,
required proto.ContactRequestPrivate contactRequestPrivate,
2024-06-07 14:42:04 -04:00
required SuperIdentity contactSuperIdentity,
2024-01-18 19:44:15 -05:00
required KeyPair writer})
2024-06-18 21:20:06 -04:00
: _accountInfo = accountInfo,
2024-01-18 19:44:15 -05:00
_contactRequestInboxKey = contactRequestInboxKey,
_contactRequestPrivate = contactRequestPrivate,
2024-06-07 14:42:04 -04:00
_contactSuperIdentity = contactSuperIdentity,
2024-01-18 19:44:15 -05:00
_writer = writer;
2024-01-30 14:14:11 -05:00
proto.Profile get remoteProfile => _contactRequestPrivate.profile;
2024-06-18 21:20:06 -04:00
Future<AcceptedContact?> accept(proto.Profile profile) async {
2024-01-25 20:33:56 -05:00
final pool = DHTRecordPool.instance;
2024-01-18 19:44:15 -05:00
try {
// Ensure we don't delete this if we're trying to chat to self
2024-02-25 22:52:09 -05:00
// The initiating side will delete the records in deleteInvitation()
2024-06-18 21:20:06 -04:00
final isSelf = _contactSuperIdentity.currentInstance.publicKey ==
_accountInfo.identityPublicKey;
2024-01-18 19:44:15 -05:00
return (await pool.openRecordWrite(_contactRequestInboxKey, _writer,
debugName: 'ValidContactInvitation::accept::'
'ContactRequestInbox',
2024-06-16 22:12:24 -04:00
parent: pool.getParentRecordKey(_contactRequestInboxKey) ??
2024-06-18 21:20:06 -04:00
_accountInfo.accountRecordKey))
2024-01-18 19:44:15 -05:00
// ignore: prefer_expression_function_bodies
.maybeDeleteScope(!isSelf, (contactRequestInbox) async {
// Create local conversation key for this
// contact and send via contact response
2024-02-08 20:35:59 -05:00
final conversation = ConversationCubit(
2024-06-18 21:20:06 -04:00
accountInfo: _accountInfo,
2024-01-18 19:44:15 -05:00
remoteIdentityPublicKey:
2024-06-07 14:42:04 -04:00
_contactSuperIdentity.currentInstance.typedPublicKey);
2024-01-29 22:38:19 -05:00
return conversation.initLocalConversation(
2024-06-18 21:20:06 -04:00
profile: profile,
2024-01-18 19:44:15 -05:00
callback: (localConversation) async {
2024-06-18 21:20:06 -04:00
final contactResponse = proto.ContactResponse()
..accept = true
..remoteConversationRecordKey = localConversation.key.toProto()
..superIdentityRecordKey =
_accountInfo.superIdentityRecordKey.toProto();
final contactResponseBytes = contactResponse.writeToBuffer();
final cs = await _accountInfo.identityCryptoSystem;
final identitySignature = await cs.signWithKeyPair(
_accountInfo.identityWriter, contactResponseBytes);
final signedContactResponse = proto.SignedContactResponse()
..contactResponse = contactResponseBytes
..identitySignature = identitySignature.toProto();
// Write the acceptance to the inbox
await contactRequestInbox
.eventualWriteProtobuf(signedContactResponse, subkey: 1);
return AcceptedContact(
remoteProfile: _contactRequestPrivate.profile,
remoteIdentity: _contactSuperIdentity,
remoteConversationRecordKey:
_contactRequestPrivate.chatRecordKey.toVeilid(),
localConversationRecordKey: localConversation.key,
);
});
2024-01-18 19:44:15 -05:00
});
} on Exception catch (e) {
log.debug('exception: $e', e);
return null;
}
}
Future<bool> reject() async {
2024-01-25 20:33:56 -05:00
final pool = DHTRecordPool.instance;
2024-01-18 19:44:15 -05:00
// Ensure we don't delete this if we're trying to chat to self
2024-06-18 21:20:06 -04:00
final isSelf = _contactSuperIdentity.currentInstance.publicKey ==
_accountInfo.identityPublicKey;
2024-01-18 19:44:15 -05:00
return (await pool.openRecordWrite(_contactRequestInboxKey, _writer,
debugName: 'ValidContactInvitation::reject::'
'ContactRequestInbox',
2024-06-18 21:20:06 -04:00
parent: _accountInfo.accountRecordKey))
2024-01-18 19:44:15 -05:00
.maybeDeleteScope(!isSelf, (contactRequestInbox) async {
final contactResponse = proto.ContactResponse()
..accept = false
2024-06-07 14:42:04 -04:00
..superIdentityRecordKey =
2024-06-18 21:20:06 -04:00
_accountInfo.superIdentityRecordKey.toProto();
2024-01-18 19:44:15 -05:00
final contactResponseBytes = contactResponse.writeToBuffer();
2024-06-18 21:20:06 -04:00
final cs = await _accountInfo.identityCryptoSystem;
final identitySignature = await cs.signWithKeyPair(
_accountInfo.identityWriter, contactResponseBytes);
2024-01-18 19:44:15 -05:00
final signedContactResponse = proto.SignedContactResponse()
..contactResponse = contactResponseBytes
..identitySignature = identitySignature.toProto();
// Write the rejection to the inbox
await contactRequestInbox.eventualWriteProtobuf(signedContactResponse,
subkey: 1);
2024-01-18 19:44:15 -05:00
return true;
});
}
//
2024-06-18 21:20:06 -04:00
final AccountInfo _accountInfo;
2024-01-25 20:33:56 -05:00
final TypedKey _contactRequestInboxKey;
2024-06-07 14:42:04 -04:00
final SuperIdentity _contactSuperIdentity;
2024-01-25 20:33:56 -05:00
final KeyPair _writer;
2024-01-28 21:31:53 -05:00
final proto.ContactRequestPrivate _contactRequestPrivate;
2024-01-18 19:44:15 -05:00
}