2024-02-20 17:57:05 -05:00
|
|
|
import 'dart:typed_data';
|
|
|
|
|
|
|
|
import 'package:async_tools/async_tools.dart';
|
2024-05-01 20:58:25 -04:00
|
|
|
import 'package:bloc_advanced_tools/bloc_advanced_tools.dart';
|
2024-02-20 17:57:05 -05:00
|
|
|
import 'package:equatable/equatable.dart';
|
|
|
|
import 'package:meta/meta.dart';
|
|
|
|
import 'package:veilid_support/veilid_support.dart';
|
|
|
|
|
|
|
|
import '../../account_manager/account_manager.dart';
|
|
|
|
import '../../contacts/contacts.dart';
|
|
|
|
import '../../proto/proto.dart' as proto;
|
|
|
|
import '../../tools/tools.dart';
|
|
|
|
import '../models/accepted_contact.dart';
|
|
|
|
import 'contact_request_inbox_cubit.dart';
|
|
|
|
|
|
|
|
@immutable
|
|
|
|
class InvitationStatus extends Equatable {
|
|
|
|
const InvitationStatus({required this.acceptedContact});
|
|
|
|
final AcceptedContact? acceptedContact;
|
|
|
|
|
|
|
|
@override
|
|
|
|
List<Object?> get props => [acceptedContact];
|
|
|
|
}
|
|
|
|
|
|
|
|
class WaitingInvitationCubit extends AsyncTransformerCubit<InvitationStatus,
|
2024-04-04 22:31:09 -04:00
|
|
|
proto.SignedContactResponse?> {
|
2024-02-20 17:57:05 -05:00
|
|
|
WaitingInvitationCubit(ContactRequestInboxCubit super.input,
|
|
|
|
{required ActiveAccountInfo activeAccountInfo,
|
|
|
|
required proto.Account account,
|
|
|
|
required proto.ContactInvitationRecord contactInvitationRecord})
|
|
|
|
: super(
|
|
|
|
transform: (signedContactResponse) => _transform(
|
|
|
|
signedContactResponse,
|
|
|
|
activeAccountInfo: activeAccountInfo,
|
|
|
|
account: account,
|
|
|
|
contactInvitationRecord: contactInvitationRecord));
|
|
|
|
|
|
|
|
static Future<AsyncValue<InvitationStatus>> _transform(
|
2024-04-04 22:31:09 -04:00
|
|
|
proto.SignedContactResponse? signedContactResponse,
|
2024-02-20 17:57:05 -05:00
|
|
|
{required ActiveAccountInfo activeAccountInfo,
|
|
|
|
required proto.Account account,
|
|
|
|
required proto.ContactInvitationRecord contactInvitationRecord}) async {
|
2024-04-04 22:31:09 -04:00
|
|
|
if (signedContactResponse == null) {
|
|
|
|
return const AsyncValue.loading();
|
|
|
|
}
|
2024-02-20 17:57:05 -05:00
|
|
|
final contactResponseBytes =
|
|
|
|
Uint8List.fromList(signedContactResponse.contactResponse);
|
|
|
|
final contactResponse =
|
|
|
|
proto.ContactResponse.fromBuffer(contactResponseBytes);
|
|
|
|
final contactIdentityMasterRecordKey =
|
2024-06-07 14:42:04 -04:00
|
|
|
contactResponse.superIdentityRecordKey.toVeilid();
|
2024-02-20 17:57:05 -05:00
|
|
|
|
|
|
|
// Fetch the remote contact's account master
|
2024-06-07 14:42:04 -04:00
|
|
|
final contactSuperIdentity = await SuperIdentity.open(
|
|
|
|
superRecordKey: contactIdentityMasterRecordKey);
|
2024-02-20 17:57:05 -05:00
|
|
|
|
|
|
|
// Verify
|
2024-06-07 14:42:04 -04:00
|
|
|
final idcs = await contactSuperIdentity.currentInstance.cryptoSystem;
|
2024-02-20 17:57:05 -05:00
|
|
|
final signature = signedContactResponse.identitySignature.toVeilid();
|
2024-06-07 14:42:04 -04:00
|
|
|
await idcs.verify(contactSuperIdentity.currentInstance.publicKey,
|
2024-02-20 17:57:05 -05:00
|
|
|
contactResponseBytes, signature);
|
|
|
|
|
|
|
|
// Check for rejection
|
|
|
|
if (!contactResponse.accept) {
|
|
|
|
// Rejection
|
|
|
|
return const AsyncValue.data(InvitationStatus(acceptedContact: null));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pull profile from remote conversation key
|
|
|
|
final remoteConversationRecordKey =
|
|
|
|
contactResponse.remoteConversationRecordKey.toVeilid();
|
|
|
|
|
|
|
|
final conversation = ConversationCubit(
|
|
|
|
activeAccountInfo: activeAccountInfo,
|
2024-06-07 14:42:04 -04:00
|
|
|
remoteIdentityPublicKey:
|
|
|
|
contactSuperIdentity.currentInstance.typedPublicKey,
|
2024-02-20 17:57:05 -05:00
|
|
|
remoteConversationRecordKey: remoteConversationRecordKey);
|
|
|
|
|
|
|
|
// wait for remote conversation for up to 20 seconds
|
|
|
|
proto.Conversation? remoteConversation;
|
|
|
|
var retryCount = 20;
|
|
|
|
do {
|
|
|
|
await conversation.refresh();
|
2024-04-05 22:03:04 -04:00
|
|
|
remoteConversation = conversation.state.asData?.value.remoteConversation;
|
2024-02-20 17:57:05 -05:00
|
|
|
if (remoteConversation != null) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
log.info('Remote conversation could not be read. Waiting...');
|
|
|
|
await Future<void>.delayed(const Duration(seconds: 1));
|
|
|
|
retryCount--;
|
|
|
|
} while (retryCount > 0);
|
|
|
|
if (remoteConversation == null) {
|
|
|
|
return AsyncValue.error('Invitation accept timed out.');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Complete the local conversation now that we have the remote profile
|
|
|
|
final remoteProfile = remoteConversation.profile;
|
|
|
|
final localConversationRecordKey =
|
|
|
|
contactInvitationRecord.localConversationRecordKey.toVeilid();
|
|
|
|
return conversation.initLocalConversation(
|
|
|
|
existingConversationRecordKey: localConversationRecordKey,
|
|
|
|
profile: account.profile,
|
|
|
|
// ignore: prefer_expression_function_bodies
|
|
|
|
callback: (localConversation) async {
|
|
|
|
return AsyncValue.data(InvitationStatus(
|
|
|
|
acceptedContact: AcceptedContact(
|
|
|
|
remoteProfile: remoteProfile,
|
2024-06-07 14:42:04 -04:00
|
|
|
remoteIdentity: contactSuperIdentity,
|
2024-02-20 17:57:05 -05:00
|
|
|
remoteConversationRecordKey: remoteConversationRecordKey,
|
|
|
|
localConversationRecordKey: localConversationRecordKey)));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|