@@ -485,50 +485,60 @@ class ComposeBar extends HookConsumerWidget {
485485 var authorizationRevision = submittedDraftRevision;
486486 final visit = authorizationVisit.value;
487487 final config = ref.read (relayConfigProvider);
488- final readAuthorization = ref.read (agentAuthorizationReaderProvider);
488+ // Equivalent refreshes retain scope; destination/credentials do not.
489+ bool isConfigScopeCurrent () {
490+ final current = ref.read (relayConfigProvider);
491+ return current.baseUrl == config.baseUrl &&
492+ current.nsec == config.nsec;
493+ }
494+
495+ final readSelected = ref.read (
496+ selectedMentionAuthorizationReaderProvider,
497+ );
498+ final session = ref.read (relaySessionProvider.notifier);
499+ final observedProfiles = < String , NostrEvent > {};
500+ final observedKeys = < String > {};
501+ bool profilesCurrent () => observedProfiles.entries.every ((entry) {
502+ final order = ref
503+ .read (userCacheProvider.notifier)
504+ .profileEventOrder (entry.key);
505+ final event = entry.value;
506+ return order == null ||
507+ order.createdAt < event.createdAt ||
508+ (order.createdAt == event.createdAt &&
509+ order.eventId.compareTo (event.id) >= 0 );
510+ });
489511 bool ownsSource () =>
490512 context.mounted &&
491513 visit == authorizationVisit.value &&
492- identical (config, ref. read (relayConfigProvider) );
514+ isConfigScopeCurrent ( );
493515 bool isAuthorizationCurrent () =>
494516 ownsSource () &&
517+ identical (session, ref.read (relaySessionProvider.notifier)) &&
518+ currentPubkey == ref.read (currentPubkeyProvider) &&
519+ profilesCurrent () &&
495520 submittedUploadGeneration == uploadGeneration.value &&
496521 authorizationRevision == draftRevision.value &&
497- identical (config, ref. read (relayConfigProvider) );
522+ isConfigScopeCurrent ( );
498523 void ensureAuthorizationCurrent () {
499524 if (! context.mounted) throw const _ComposeAuthorizationCancelled ();
500- if (! identical (config, ref. read (relayConfigProvider) )) {
501- throw StateError ( 'Community changed during authorization' );
525+ if (! isConfigScopeCurrent ( )) {
526+ throw const _ComposeCommunityChanged ( );
502527 }
503528 if (submittedUploadGeneration != uploadGeneration.value ||
504529 visit != authorizationVisit.value ||
505530 authorizationRevision != draftRevision.value) {
506531 throw const _ComposeAuthorizationCancelled ();
507532 }
533+ if (! identical (session, ref.read (relaySessionProvider.notifier)) ||
534+ currentPubkey != ref.read (currentPubkeyProvider) ||
535+ ! profilesCurrent ()) {
536+ throw Exception ('Mention evidence changed; retry the draft' );
537+ }
508538 }
509539
510540 checkPreparationCurrent = ensureAuthorizationCurrent;
511541
512- Future <void > authorize (Set <String > keys, {bool prepare = false }) async {
513- if (keys.isEmpty) return ;
514- ensureAuthorizationCurrent ();
515- try {
516- await authorizeAgentMentions (
517- readAuthorization,
518- keys,
519- currentPubkey,
520- channelId,
521- isAuthorizationCurrent,
522- prepare: prepare,
523- );
524- } catch (_) {
525- // A stale request is cancellation, not an access decision.
526- ensureAuthorizationCurrent ();
527- rethrow ;
528- }
529- ensureAuthorizationCurrent ();
530- }
531-
532542 // Resolved before any await: see
533543 // `_reportSendCancelledByCommunitySwitch`.
534544 final messenger = ScaffoldMessenger .maybeOf (context);
@@ -542,15 +552,34 @@ class ComposeBar extends HookConsumerWidget {
542552 selectedMentions,
543553 '${ref .read (relayConfigProvider ).baseUrl } / $channelId ' ,
544554 );
545- final intendedAgentKeys = {
555+ final selectedKeys = outgoing.pubkeys.toSet ();
556+ final priorAgentKeys = {
546557 for (final mention in selectedMentions)
547558 if (mention.isAgent) mention.pubkey.toLowerCase (),
548559 };
560+ Future <Map <String , SelectedMentionAuthorization >> authorize (
561+ Set <String > keys, {
562+ bool prepare = false ,
563+ }) => _authorizeSelectedMentions (
564+ keys,
565+ readSelected: readSelected,
566+ priorAgentKeys: priorAgentKeys,
567+ observedKeys: observedKeys,
568+ observedProfiles: observedProfiles,
569+ currentPubkey: currentPubkey,
570+ channelId: channelId,
571+ ensureAuthorizationCurrent: ensureAuthorizationCurrent,
572+ isAuthorizationCurrent: isAuthorizationCurrent,
573+ prepare: prepare,
574+ );
575+
576+ final initialEvidence = await authorize (selectedKeys, prepare: true );
549577 final scan = await _scanNonMemberMentions (
550578 ref,
551579 channelId: channelId,
552580 selectedMentions: selectedMentions,
553581 currentPubkey: currentPubkey,
582+ evidence: initialEvidence,
554583 );
555584
556585 ensureAuthorizationCurrent ();
@@ -583,7 +612,18 @@ class ComposeBar extends HookConsumerWidget {
583612
584613 // Agent failures stop publication; the original draft keeps its keys.
585614 Future <void > addMentionedNonMembers () async {
586- final keys = intendedAgentKeys.intersection (outgoing.pubkeys.toSet ());
615+ final keys = outgoing.pubkeys.toSet ();
616+ Future <bool > authorizeWrite (String key, String role) async {
617+ final evidence = await authorize (keys, prepare: true );
618+ final fresh = evidence[key]! ;
619+ if (fresh.invitationRole != role) {
620+ throw Exception (
621+ 'Mention classification changed; retry invitation consent' ,
622+ );
623+ }
624+ return ! fresh.isMember;
625+ }
626+
587627 await authorize (keys, prepare: true );
588628 ensureAuthorizationCurrent ();
589629 invitationStarted.value = true ;
@@ -592,6 +632,7 @@ class ComposeBar extends HookConsumerWidget {
592632 scan: scan,
593633 messenger: messenger,
594634 ensureCurrent: ensureAuthorizationCurrent,
635+ authorizeWrite: authorizeWrite,
595636 );
596637 if (! outgoing.pubkeys.toSet ().containsAll (keys)) {
597638 throw Exception (
@@ -600,7 +641,7 @@ class ComposeBar extends HookConsumerWidget {
600641 }
601642 await authorize (keys);
602643 if (queuedAttachments.isEmpty ||
603- (intendedAgentKeys .isNotEmpty ||
644+ (selectedKeys .isNotEmpty ||
604645 scan.humans.isNotEmpty ||
605646 scan.agentPubkeys.isNotEmpty)) {
606647 ensureAuthorizationCurrent ();
@@ -639,7 +680,7 @@ class ComposeBar extends HookConsumerWidget {
639680 );
640681 // Agent authorization is preparation, not a detached background send.
641682 final preparingAgents =
642- intendedAgentKeys .isNotEmpty || scan.humans.isNotEmpty;
683+ selectedKeys .isNotEmpty || scan.humans.isNotEmpty;
643684 if (! preparingAgents) clearComposer ();
644685 final clearedDraftRevision = draftRevision.value;
645686 authorizationRevision = clearedDraftRevision;
@@ -697,8 +738,11 @@ class ComposeBar extends HookConsumerWidget {
697738 } on _ComposeAuthorizationCancelled {
698739 // Keep the newer draft without displaying a false access error.
699740 } catch (error) {
700- if (cancellation.isCancelled) return ;
701- if (error is StateError ) {
741+ if (cancellation.isCancelled &&
742+ error is ! _ComposeCommunityChanged ) {
743+ return ;
744+ }
745+ if (error is _ComposeCommunityChanged ) {
702746 _reportSendCancelledByCommunitySwitch (messenger);
703747 } else if (context.mounted) {
704748 uploadError.value = _formatUploadError (error);
@@ -735,25 +779,8 @@ class ComposeBar extends HookConsumerWidget {
735779 }
736780 }());
737781 } catch (error) {
738- var communityChanged = false ;
739- // Failed awaits need the same scope/edit classification as success.
740- try {
741- checkPreparationCurrent? .call ();
742- if (error is _ComposeAuthorizationCancelled ) return ;
743- } on _ComposeAuthorizationCancelled {
744- return ;
745- } on StateError {
746- communityChanged = true ;
747- }
748782 if (context.mounted) {
749- final messenger = ScaffoldMessenger .maybeOf (context);
750- if (communityChanged) {
751- _reportSendCancelledByCommunitySwitch (messenger);
752- } else {
753- messenger? .showSnackBar (
754- SnackBar (content: Text (_composeSendErrorMessage (error))),
755- );
756- }
783+ _reportComposeSendError (context, error, checkPreparationCurrent);
757784 }
758785 } finally {
759786 if (context.mounted && authorizationAttempt.value == attempt) {
0 commit comments