veilidchat/lib/contact_invitation/models/valid_contact_invitation.dart

144 lines
5.4 KiB
Dart
Raw Normal View History

2024-01-25 20:33:56 -05:00
import 'package:meta/meta.dart';
2024-06-15 23:29:15 -04:00
import 'package:provider/provider.dart';
2024-01-25 20:33:56 -05:00
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-15 23:29:15 -04:00
{required Locator locator,
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-15 23:29:15 -04:00
: _locator = locator,
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-01-18 19:44:15 -05:00
Future<AcceptedContact?> accept() async {
2024-01-25 20:33:56 -05:00
final pool = DHTRecordPool.instance;
2024-01-18 19:44:15 -05:00
try {
2024-06-15 23:29:15 -04:00
final unlockedAccountInfo =
_locator<ActiveAccountInfoCubit>().state.unlockedAccountInfo!;
final accountRecordKey = unlockedAccountInfo.accountRecordKey;
final identityPublicKey = unlockedAccountInfo.identityPublicKey;
2024-01-18 19:44:15 -05:00
// 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-15 23:29:15 -04:00
final isSelf =
_contactSuperIdentity.currentInstance.publicKey == identityPublicKey;
2024-01-18 19:44:15 -05:00
return (await pool.openRecordWrite(_contactRequestInboxKey, _writer,
debugName: 'ValidContactInvitation::accept::'
'ContactRequestInbox',
2024-01-18 19:44:15 -05:00
parent: accountRecordKey))
// 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-15 23:29:15 -04:00
locator: _locator,
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-01-18 19:44:15 -05:00
callback: (localConversation) async {
2024-06-15 23:29:15 -04:00
final contactResponse = proto.ContactResponse()
..accept = true
..remoteConversationRecordKey = localConversation.key.toProto()
..superIdentityRecordKey =
unlockedAccountInfo.superIdentityRecordKey.toProto();
final contactResponseBytes = contactResponse.writeToBuffer();
final cs =
await pool.veilid.getCryptoSystem(_contactRequestInboxKey.kind);
final identitySignature = await cs.sign(
unlockedAccountInfo.identityWriter.key,
unlockedAccountInfo.identityWriter.secret,
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
2024-06-15 23:29:15 -04:00
final unlockedAccountInfo =
_locator<ActiveAccountInfoCubit>().state.unlockedAccountInfo!;
final accountRecordKey = unlockedAccountInfo.accountRecordKey;
final identityPublicKey = unlockedAccountInfo.identityPublicKey;
2024-01-18 19:44:15 -05:00
// Ensure we don't delete this if we're trying to chat to self
2024-06-15 23:29:15 -04:00
final isSelf =
_contactSuperIdentity.currentInstance.publicKey == identityPublicKey;
2024-01-18 19:44:15 -05:00
return (await pool.openRecordWrite(_contactRequestInboxKey, _writer,
debugName: 'ValidContactInvitation::reject::'
'ContactRequestInbox',
2024-01-18 19:44:15 -05:00
parent: accountRecordKey))
.maybeDeleteScope(!isSelf, (contactRequestInbox) async {
final cs =
await pool.veilid.getCryptoSystem(_contactRequestInboxKey.kind);
final contactResponse = proto.ContactResponse()
..accept = false
2024-06-07 14:42:04 -04:00
..superIdentityRecordKey =
2024-06-15 23:29:15 -04:00
unlockedAccountInfo.superIdentityRecordKey.toProto();
2024-01-18 19:44:15 -05:00
final contactResponseBytes = contactResponse.writeToBuffer();
final identitySignature = await cs.sign(
2024-06-15 23:29:15 -04:00
unlockedAccountInfo.identityWriter.key,
unlockedAccountInfo.identityWriter.secret,
2024-01-18 19:44:15 -05:00
contactResponseBytes);
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-15 23:29:15 -04:00
final Locator _locator;
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
}