Skip to content

Commit ea4fade

Browse files
committed
fix(mobile): check mentions against the route's channel
A DM opened on the fast path is on screen before the channel list has it, but the composer's mention check looked the destination up in that list, so a selected mention failed or waited on the full reload. The channel page now hands the composer the channel it resolved, and the check uses it before falling back to the list. Signed-off-by: Tom Brow <[email protected]>
1 parent 84444d2 commit ea4fade

4 files changed

Lines changed: 83 additions & 7 deletions

File tree

‎mobile/lib/features/channels/channel_detail_page.dart‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -888,6 +888,7 @@ class _ResolvedChannelDetailPage extends HookConsumerWidget {
888888
?assistant.pendingNotice,
889889
ComposeBar(
890890
channelId: channel.id,
891+
channel: resolvedChannel,
891892
focusNode: composerFocusNode,
892893
onFocusRestorerChanged: (restoreFocus) =>
893894
restoreComposerFocus.value = restoreFocus,

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,14 @@ class ComposeBar extends HookConsumerWidget {
1818
/// Optional thread IDs for thread-scoped typing indicators.
1919
final String? threadHeadId;
2020
final String? rootId;
21+
22+
/// The destination as the route resolved it. Sends check mentions against
23+
/// it, so a DM opened before the channel list reloads can still send them.
24+
final Channel? channel;
2125
const ComposeBar({
2226
super.key,
2327
required this.channelId,
28+
this.channel,
2429
this.channelName = '',
2530
this.hintText,
2631
this.threadHeadId,
@@ -575,6 +580,7 @@ class ComposeBar extends HookConsumerWidget {
575580
scan = await _scanNonMemberMentions(
576581
ref,
577582
channelId: channelId,
583+
routeChannel: channel,
578584
selectedMentions: selectedMentions,
579585
currentPubkey: currentPubkey,
580586
);

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,13 @@ class _NonMemberMentionScan {
510510

511511
/// Resolves which mentioned identities are non-members, and whether this
512512
/// identity may add them (see [Channel.canAddMembers]). A DM's participant set
513-
/// is fixed at creation, so nobody outside it can be added.
513+
/// is fixed at creation, so nobody outside it can be added. [routeChannel],
514+
/// when given, stands in for the channel list, which may not have a just
515+
/// opened DM yet.
514516
Future<_NonMemberMentionScan> _scanNonMemberMentions(
515517
WidgetRef ref, {
516518
required String channelId,
519+
Channel? routeChannel,
517520
required List<MentionCandidate> selectedMentions,
518521
required String? currentPubkey,
519522
}) async {
@@ -525,9 +528,11 @@ Future<_NonMemberMentionScan> _scanNonMemberMentions(
525528
);
526529
if (selectedMentions.isEmpty) return none;
527530

528-
final channel = (await ref.read(
529-
channelsProvider.future,
530-
)).firstWhere((candidate) => candidate.id == channelId);
531+
final channel =
532+
routeChannel ??
533+
(await ref.read(
534+
channelsProvider.future,
535+
)).firstWhere((candidate) => candidate.id == channelId);
531536

532537
// A DM decides who is inside it the way its send does: by current
533538
// membership, or by the channel metadata when membership is unavailable.

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

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ Widget _buildComposeBar({
187187
Future<List<ChannelMember>> Function()? loadMembers,
188188
List<AgentDirectoryEntry> relayAgents = const <AgentDirectoryEntry>[],
189189
List<Channel> channels = const <Channel>[],
190+
bool stallChannels = false,
191+
Channel? channel,
190192
List<ChannelMember> cachedMembers = const <ChannelMember>[],
191193
String? currentPubkey,
192194
bool? supportsShowingSystemContextMenu,
@@ -240,7 +242,11 @@ Widget _buildComposeBar({
240242
if (appLifecycle != null) appLifecycleProvider.overrideWith(appLifecycle),
241243
savedPrefsProvider.overrideWithValue(_testPrefs),
242244
channelsProvider.overrideWith(
243-
() => _FakeChannelsNotifier(channels, cachedMembers: cachedMembers),
245+
() => _FakeChannelsNotifier(
246+
channels,
247+
cachedMembers: cachedMembers,
248+
stall: stallChannels,
249+
),
244250
),
245251
],
246252
child: MaterialApp(
@@ -272,6 +278,7 @@ Widget _buildComposeBar({
272278
final composeBar = ComposeBar(
273279
key: ValueKey(composeBarKey),
274280
channelId: 'channel-1',
281+
channel: channel,
275282
threadHeadId: threadHeadId,
276283
focusNode: focusNode,
277284
onFocusRestorerChanged: onFocusRestorerChanged,
@@ -638,17 +645,23 @@ class _FakeChannelsNotifier extends ChannelsNotifier {
638645
final List<Channel> _channels;
639646
final List<ChannelMember> _cachedMembers;
640647

648+
/// Keeps the channel list loading, like a slow full reload.
649+
final bool _stall;
650+
641651
_FakeChannelsNotifier(
642652
this._channels, {
643653
List<ChannelMember> cachedMembers = const [],
644-
}) : _cachedMembers = cachedMembers;
654+
bool stall = false,
655+
}) : _cachedMembers = cachedMembers,
656+
_stall = stall;
645657

646658
@override
647659
List<ChannelMember> cachedMembersForChannel(String channelId) =>
648660
channelId == 'channel-1' ? _cachedMembers : const [];
649661

650662
@override
651-
Future<List<Channel>> build() async => _channels;
663+
Future<List<Channel>> build() =>
664+
_stall ? Completer<List<Channel>>().future : Future.value(_channels);
652665

653666
@override
654667
Future<void> refresh({bool fetchDirectory = false}) async {
@@ -4698,6 +4711,57 @@ void main() {
46984711
expect(sentTags, contains(orderedEquals(['mention', maryJane.pubkey])));
46994712
});
47004713

4714+
testWidgets('a DM opened before the channel list loads sends mentions', (
4715+
tester,
4716+
) async {
4717+
final signer = nostr.Keys.generate();
4718+
final pat = '1' * 64;
4719+
List<String>? sentRecipients;
4720+
List<List<String>>? sentTags;
4721+
await tester.pumpWidget(
4722+
_buildComposeBar(
4723+
uploadService: _testUploadService(signer.nsec),
4724+
currentPubkey: signer.public,
4725+
members: [
4726+
member(signer.public),
4727+
ChannelMember(
4728+
pubkey: pat,
4729+
role: 'member',
4730+
joinedAt: DateTime.fromMillisecondsSinceEpoch(1000),
4731+
displayName: 'Pat Peer',
4732+
),
4733+
],
4734+
// The route opened the DM from its own metadata and roster while
4735+
// the full channel list is still loading.
4736+
stallChannels: true,
4737+
channel: _makeCurrentChannel(
4738+
channelType: 'dm',
4739+
participantPubkeys: [signer.public, pat],
4740+
),
4741+
onSend:
4742+
(_, recipients, {mediaTags = const <List<String>>[]}) async {
4743+
sentRecipients = recipients;
4744+
sentTags = mediaTags;
4745+
},
4746+
),
4747+
);
4748+
await _expandComposer(tester);
4749+
await tester.enterText(find.byType(TextField), '@Pat');
4750+
await tester.pump();
4751+
await tester.pumpAndSettle();
4752+
await tester.tap(find.text('Pat Peer'));
4753+
await tester.pumpAndSettle();
4754+
await tester.tap(find.byIcon(BuzzIcons.arrowUp));
4755+
await tester.pumpAndSettle();
4756+
4757+
expect(
4758+
find.text('Message not sent: could not check who is in this channel'),
4759+
findsNothing,
4760+
);
4761+
expect(sentRecipients, contains(pat));
4762+
expect(sentTags, isNot(contains(orderedEquals(['mention', pat]))));
4763+
});
4764+
47014765
testWidgets('a failed member check keeps the draft and Retry sends', (
47024766
tester,
47034767
) async {

0 commit comments

Comments
 (0)