Skip to content

Commit 2fd0d0b

Browse files
committed
fix(mobile): preserve send outcome and scope cancellation evidence
Signed-off-by: Logan Johnson <[email protected]>
1 parent 4caa1eb commit 2fd0d0b

3 files changed

Lines changed: 88 additions & 19 deletions

File tree

‎mobile/lib/features/channels/compose_bar/compose_bar_widget.dart‎

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ class ComposeBar extends HookConsumerWidget {
476476
final attempt = Object();
477477
authorizationAttempt.value = attempt;
478478
isSending.value = true;
479+
void Function()? checkPreparationCurrent;
479480
try {
480481
final submittedDraftRevision = draftRevision.value;
481482
var authorizationRevision = submittedDraftRevision;
@@ -498,6 +499,8 @@ class ComposeBar extends HookConsumerWidget {
498499
}
499500
}
500501

502+
checkPreparationCurrent = ensureAuthorizationCurrent;
503+
501504
Future<void> authorize(Set<String> keys, {bool prepare = false}) async {
502505
if (keys.isEmpty) return;
503506
ensureAuthorizationCurrent();
@@ -539,7 +542,7 @@ class ComposeBar extends HookConsumerWidget {
539542
currentPubkey: currentPubkey,
540543
);
541544

542-
if (intendedAgentKeys.isNotEmpty && !isAuthorizationCurrent()) return;
545+
if (intendedAgentKeys.isNotEmpty) ensureAuthorizationCurrent();
543546
// Mentioning humans outside the channel prompts "Invite" / "Do
544547
// nothing" (send without inviting) — mirrors desktop's
545548
// NonMemberMentionDialog. Agents keep the existing silent auto-add.
@@ -550,6 +553,7 @@ class ComposeBar extends HookConsumerWidget {
550553
names: [for (final candidate in scan.humans) candidate.label],
551554
canInvite: scan.canAddMembers,
552555
);
556+
if (intendedAgentKeys.isNotEmpty) ensureAuthorizationCurrent();
553557
if (choice == null) {
554558
return; // Dismissed — keep the draft, send nothing.
555559
}
@@ -601,7 +605,7 @@ class ComposeBar extends HookConsumerWidget {
601605
return;
602606
}
603607

604-
if (intendedAgentKeys.isNotEmpty && !isAuthorizationCurrent()) return;
608+
if (intendedAgentKeys.isNotEmpty) ensureAuthorizationCurrent();
605609
final draftText = controller.value;
606610
final draftAttachments = List<_PendingAttachment>.of(attachments.value);
607611
final draftMentions = Map<String, MentionCandidate>.of(
@@ -652,7 +656,7 @@ class ComposeBar extends HookConsumerWidget {
652656
await addMentionedNonMembers();
653657
if (queueGeneration != uploadGeneration.value) return;
654658
if (preparingAgents) {
655-
if (!isAuthorizationCurrent()) return;
659+
ensureAuthorizationCurrent();
656660
clearComposer();
657661
authorizationRevision = draftRevision.value;
658662
}
@@ -700,10 +704,25 @@ class ComposeBar extends HookConsumerWidget {
700704
}
701705
}());
702706
} catch (error) {
707+
var communityChanged = false;
708+
// Failed awaits need the same scope/edit classification as success.
709+
try {
710+
checkPreparationCurrent?.call();
711+
if (error is _ComposeAuthorizationCancelled) return;
712+
} on _ComposeAuthorizationCancelled {
713+
return;
714+
} on StateError {
715+
communityChanged = true;
716+
}
703717
if (context.mounted) {
704-
ScaffoldMessenger.maybeOf(context)?.showSnackBar(
705-
SnackBar(content: Text(_composeSendErrorMessage(error))),
706-
);
718+
final messenger = ScaffoldMessenger.maybeOf(context);
719+
if (communityChanged) {
720+
_reportSendCancelledByCommunitySwitch(messenger);
721+
} else {
722+
messenger?.showSnackBar(
723+
SnackBar(content: Text(_composeSendErrorMessage(error))),
724+
);
725+
}
707726
}
708727
} finally {
709728
if (context.mounted && authorizationAttempt.value == attempt) {

‎mobile/test/features/channels/compose_bar_test.dart‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ Widget _buildComposeBar({
176176
required ComposeBarOnSend onSend,
177177
List<ChannelMember> members = const <ChannelMember>[],
178178
Future<List<ChannelMember>>? membersFuture,
179+
Future<List<ChannelMember>> Function()? membersLoader,
179180
AgentAuthorizationReader? authorizationReader,
180181
List<AgentDirectoryEntry> relayAgents = const <AgentDirectoryEntry>[],
181182
List<Channel> channels = const <Channel>[],
@@ -210,9 +211,10 @@ Widget _buildComposeBar({
210211
),
211212
photoLibraryProvider.overrideWithValue(photoLibrary),
212213
currentPubkeyProvider.overrideWith((ref) => currentPubkey),
213-
channelMembersProvider(
214-
'channel-1',
215-
).overrideWith((ref) => membersFuture ?? Future.value(members)),
214+
channelMembersProvider('channel-1').overrideWith(
215+
(ref) =>
216+
membersLoader?.call() ?? membersFuture ?? Future.value(members),
217+
),
216218
agentAuthorizationReaderProvider.overrideWithValue(
217219
authorizationReader ??
218220
(keys, viewer, channel, current) async => [

‎mobile/test/features/channels/compose_bar_test/publication_tests.dart‎

Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,18 @@ void _publicationTests() {
7070
});
7171
}
7272

73-
for (final switchCommunity in [false, true]) {
73+
for (final (boundary, switchCommunity) in [
74+
for (final boundary in ['reader', 'scan', 'scan error', 'prompt'])
75+
for (final change in [false, true]) (boundary, change),
76+
]) {
7477
testWidgets(
75-
'authorization cancellation reports scope change only: $switchCommunity',
78+
'authorization cancellation $boundary reports scope change only: $switchCommunity',
7679
(tester) async {
7780
final key = 'd' * 64;
7881
final signer = nostr.Keys.generate();
7982
final pending = Completer<List<AgentDirectoryEntry>>();
83+
final members = Completer<List<ChannelMember>>();
84+
var scanPending = false;
8085
var reads = 0;
8186
var sent = 0;
8287
await tester.pumpWidget(
@@ -86,9 +91,27 @@ void _publicationTests() {
8691
relayConfig: () => _SwitchableRelayConfigNotifier(
8792
RelayConfig(baseUrl: 'https://relay.example', nsec: signer.nsec),
8893
),
94+
membersLoader: () => boundary == 'prompt'
95+
? Future.value(
96+
scanPending
97+
? []
98+
: [
99+
ChannelMember(
100+
pubkey: 'a' * 64,
101+
displayName: 'Alice',
102+
role: 'member',
103+
joinedAt: DateTime(2025),
104+
),
105+
],
106+
)
107+
: scanPending
108+
? members.future
109+
: Future.value([]),
89110
relayAgents: [_testAgent(key)],
90111
channels: [
91-
_makeCurrentChannel(channelType: 'dm'),
112+
_makeCurrentChannel(
113+
channelType: boundary == 'reader' ? 'dm' : 'stream',
114+
),
92115
_makeSharedMemberChannel(),
93116
],
94117
authorizationReader: (_, _, _, _) {
@@ -105,17 +128,45 @@ void _publicationTests() {
105128
await tester.pumpAndSettle();
106129
await tester.tap(find.text('Helper Bot'));
107130
await tester.pumpAndSettle();
108-
await tester.enterText(find.byType(TextField), 'hello @Helper Bot');
131+
if (boundary == 'prompt') {
132+
await tester.enterText(find.byType(TextField), '@Helper Bot @ali');
133+
await tester.pumpAndSettle();
134+
await tester.tap(find.text('Alice'));
135+
await tester.pumpAndSettle();
136+
} else {
137+
await tester.enterText(find.byType(TextField), 'hello @Helper Bot');
138+
}
139+
final controller = tester
140+
.widget<TextField>(find.byType(TextField))
141+
.controller!;
142+
if (boundary != 'reader') {
143+
scanPending = true;
144+
ProviderScope.containerOf(
145+
tester.element(find.byType(ComposeBar)),
146+
).invalidate(channelMembersProvider('channel-1'));
147+
await tester.pump();
148+
}
109149
await tester.tap(find.byIcon(LucideIcons.arrowUp));
110150
await tester.pump();
111-
expect(reads, 1);
151+
expect(reads, boundary == 'reader' ? 1 : 0);
152+
if (boundary == 'prompt') {
153+
await tester.pump(const Duration(milliseconds: 300));
154+
}
112155
if (switchCommunity) {
113156
ProviderScope.containerOf(tester.element(find.byType(ComposeBar)))
114157
.read(relayConfigProvider.notifier)
115158
.update(baseUrl: 'https://other.example', nsec: signer.nsec);
116159
await tester.pump();
117160
} else {
118-
await tester.enterText(find.byType(TextField), 'new intent');
161+
controller.text = 'new intent';
162+
}
163+
if (boundary == 'prompt') {
164+
await tester.tap(find.text('Invite'));
165+
}
166+
if (boundary == 'scan error') {
167+
members.completeError(StateError('scan unavailable'));
168+
} else if (boundary == 'scan') {
169+
members.complete([]);
119170
}
120171
pending.complete([
121172
AgentDirectoryEntry(
@@ -131,10 +182,7 @@ void _publicationTests() {
131182
switchCommunity ? findsOneWidget : findsNothing,
132183
);
133184
expect(find.textContaining('Could not authorize'), findsNothing);
134-
expect(
135-
tester.widget<TextField>(find.byType(TextField)).controller!.text,
136-
switchCommunity ? '' : 'new intent',
137-
);
185+
expect(controller.text, switchCommunity ? '' : 'new intent');
138186
},
139187
);
140188
}

0 commit comments

Comments
 (0)