Skip to content

Commit bff4746

Browse files
loganjLarry
andcommitted
fix(mobile): compare every named Pulse identity and bound Add Members loads
Pulse built its naming context from note authors only, so two notes replying to or mentioning different people named Scout both showed "Replying to Scout". The context now includes reply targets and mentions; these are the identities Pulse already preloads. Add Members sent the whole channel roster through the generic naming helper, which queued a profile read for every uncached member. The sheet now compares choices with the roster's names and bot roles but loads only the shown choices' profiles. The reply composer resolves its opener's context against live naming facts, so a profile that changes while it is open updates the label. Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Signed-off-by: Logan Johnson <[email protected]>
1 parent 8b0b503 commit bff4746

9 files changed

Lines changed: 270 additions & 20 deletions

File tree

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,27 @@ class AddChannelMembersSheet extends HookConsumerWidget {
5858
.toList() ??
5959
const <DirectoryUser>[];
6060
// Compare every shown choice with the channel's members, so a candidate
61-
// who shares a member's name is told apart before being added.
61+
// who shares a member's name is told apart before being added. Members
62+
// are only compared: their names come from the roster the details page
63+
// already loaded, so only the shown choices' profiles are fetched.
6264
final choices = [...availableUsers, ...selectedUsers.value];
65+
final roster =
66+
ref.watch(channelMembersProvider(channelId)).asData?.value ??
67+
const <ChannelMember>[];
6368
final names = watchIdentityNames(
6469
ref,
6570
[...normalizedExisting, for (final user in choices) user.pubkey],
6671
agentPubkeys: {
72+
for (final member in roster)
73+
if (member.isBot) member.pubkey,
6774
for (final user in choices)
6875
if (user.isAgent) user.pubkey,
6976
},
70-
fallbackNames: {for (final user in choices) user.pubkey: user.label},
77+
fallbackNames: {
78+
for (final member in roster) member.pubkey: ?member.displayName,
79+
for (final user in choices) user.pubkey: user.label,
80+
},
81+
shown: [for (final user in choices) user.pubkey],
7182
);
7283
String labelFor(DirectoryUser user) => names.labelFor(user.pubkey);
7384

‎mobile/lib/features/pulse/compose_note_page.dart‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ import 'pulse_models.dart';
2626
class ComposeNotePage extends HookConsumerWidget {
2727
final UserNote? replyTo;
2828

29-
/// Identity labels from the surface that opened the reply, so the reply
30-
/// context names people as they were shown there.
29+
/// The comparison context of the surface that opened the reply, so the
30+
/// reply context names people as they were shown there. Its labels are
31+
/// resolved against live naming facts while this page is open.
3132
final IdentityNames? names;
3233

3334
const ComposeNotePage({super.key, this.replyTo, this.names});
@@ -176,7 +177,8 @@ class _ReplyContext extends ConsumerWidget {
176177
ref.read(userCacheProvider.notifier).get(pubkey);
177178
final mentionPubkeys = mentionedPubkeysFromTags(note.tags);
178179
final labels =
179-
names ?? watchIdentityNames(ref, {pubkey, ...mentionPubkeys});
180+
names?.withSources(ref.watch(identityNameSourcesProvider)) ??
181+
watchIdentityNames(ref, {pubkey, ...mentionPubkeys});
180182
final displayName = labels.labelFor(pubkey);
181183

182184
return Padding(

‎mobile/lib/features/pulse/note_card.dart‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,8 @@ class NoteCard extends HookConsumerWidget {
5252
ref.watch(userCacheProvider.select((cache) => cache[pubkey])) ??
5353
ref.read(userCacheProvider.notifier).get(pubkey);
5454
final mentionPubkeys = mentionedPubkeysFromTags(note.tags);
55-
final replyAuthor = note.replyParentAuthor?.toLowerCase();
5655
final labels =
57-
names ??
58-
watchIdentityNames(ref, {pubkey, ?replyAuthor, ...mentionPubkeys});
56+
names ?? watchIdentityNames(ref, pulseNamedIdentities([note]));
5957
final displayName = labels.labelFor(pubkey);
6058
final mentionLabels = {
6159
for (final key in mentionPubkeys) key: labels.labelFor(key),
@@ -340,6 +338,16 @@ class _ActionButton extends StatelessWidget {
340338
}
341339
}
342340

341+
/// Every identity [notes] name on screen: authors, reply-parent authors and
342+
/// mentions. A Pulse collection compares all of them together.
343+
Set<String> pulseNamedIdentities(Iterable<UserNote> notes) => {
344+
for (final note in notes) ...{
345+
note.pubkey.toLowerCase(),
346+
?note.replyParentAuthor?.toLowerCase(),
347+
...mentionedPubkeysFromTags(note.tags),
348+
},
349+
};
350+
343351
String _shareUri(UserNote note) =>
344352
'nostr:${nostr.Nip19.encodeShareableIdentifiers(prefix: nostr.Nip19Prefix.nevent, data: note.id, author: note.pubkey, kind: 1)}';
345353

‎mobile/lib/features/pulse/pulse_page.dart‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,12 @@ class _PulseBody extends ConsumerWidget {
170170
child: _EmptyState(message: _emptyMessage(tab)),
171171
);
172172
}
173-
// The timeline's authors are the comparison context; reply targets
174-
// and mentions resolve against them plus themselves.
173+
// Everyone the timeline names — authors, reply targets and mentions
174+
// — is one comparison context, so two same-name targets on
175+
// different notes are still told apart.
175176
final names = watchIdentityNames(
176177
ref,
177-
{for (final note in notes) note.pubkey.toLowerCase()},
178+
pulseNamedIdentities(notes),
178179
agentPubkeys: tab == PulseTab.agents
179180
? {for (final note in notes) note.pubkey.toLowerCase()}
180181
: agentPubkeys,

‎mobile/lib/shared/identity_names/identity_names.dart‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,13 +133,16 @@ class IdentityNames {
133133
_ownerPubkeys,
134134
);
135135

136-
/// Owner keys of the context whose profile is not cached yet. Callers load
137-
/// them so readable owner prefixes can appear; none are invented meanwhile.
138-
Set<String> missingOwnerProfiles() => _candidates
139-
.map((key) => _factFor(key)?.ownerPubkey)
140-
.nonNulls
141-
.where((owner) => !_sources.profiles.containsKey(owner))
142-
.toSet();
136+
/// Owner keys of the context (or of [keys] only) whose profile is not
137+
/// cached yet. Callers load them so readable owner prefixes can appear;
138+
/// none are invented meanwhile.
139+
Set<String> missingOwnerProfiles([Iterable<String>? keys]) =>
140+
(keys?.map((key) => key.toLowerCase()) ?? _candidates)
141+
.map(_factFor)
142+
.map((fact) => fact?.ownerPubkey)
143+
.nonNulls
144+
.where((owner) => !_sources.profiles.containsKey(owner))
145+
.toSet();
143146

144147
NamingIdentity? _factFor(String key) => _sources.factFor(
145148
key,

‎mobile/lib/shared/identity_names/identity_names_provider.dart‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,17 @@ void loadIdentityNameOwners(Ref ref, IdentityNames names) {
3939
/// Labels for a displayed collection with no channel context, such as search
4040
/// results, a Pulse timeline, or a picker's choices. [candidates] is that
4141
/// collection; keys outside it resolve against it plus themselves.
42+
///
43+
/// Uncached profiles are loaded for the identities in [shown] (default: all
44+
/// [candidates]). Pass [shown] when the context also holds identities that are
45+
/// only compared, such as a channel roster whose names another owner loads,
46+
/// so opening the view does not queue a profile read for every one of them.
4247
IdentityNames watchIdentityNames(
4348
WidgetRef ref,
4449
Iterable<String> candidates, {
4550
Set<String> agentPubkeys = const {},
4651
Map<String, String> fallbackNames = const {},
52+
Iterable<String>? shown,
4753
}) {
4854
final sources = ref.watch(identityNameSourcesProvider);
4955
final names = sources.scope(
@@ -54,10 +60,16 @@ IdentityNames watchIdentityNames(
5460
// A displayed identity's profile carries its owner hint, so load uncached
5561
// candidates as well as their owners; channels load member profiles
5662
// elsewhere, but a displayed collection has no other loader.
63+
final loadFor = shown == null
64+
? names.candidates
65+
: {
66+
for (final key in shown)
67+
if (names.candidates.contains(key.toLowerCase())) key.toLowerCase(),
68+
};
5769
final missing = {
58-
for (final key in names.candidates)
70+
for (final key in loadFor)
5971
if (!sources.profiles.containsKey(key)) key,
60-
...names.missingOwnerProfiles(),
72+
...names.missingOwnerProfiles(loadFor),
6173
};
6274
if (missing.isNotEmpty) {
6375
Future.microtask(() {
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import 'package:buzz/features/channels/add_members_sheet.dart';
2+
import 'package:buzz/features/channels/channel_management_provider.dart';
3+
import 'package:buzz/shared/profile/user_cache_provider.dart';
4+
import 'package:buzz/shared/profile/user_profile.dart';
5+
import 'package:buzz/shared/theme/theme.dart';
6+
import 'package:flutter/material.dart';
7+
import 'package:flutter_test/flutter_test.dart';
8+
import 'package:hooks_riverpod/hooks_riverpod.dart';
9+
10+
class _RecordingUserCache extends UserCacheNotifier {
11+
final preloaded = <String>{};
12+
13+
@override
14+
Map<String, UserProfile> build() => const {};
15+
16+
@override
17+
UserProfile? get(String pubkey) => null;
18+
19+
@override
20+
Future<bool> preload(List<String> pubkeys) async {
21+
preloaded.addAll(pubkeys);
22+
return true;
23+
}
24+
}
25+
26+
void main() {
27+
testWidgets('compares with every member but loads only shown choices', (
28+
tester,
29+
) async {
30+
const channelId = 'channel-1';
31+
// A large roster with no cached profiles; one human member is Scout.
32+
final roster = [
33+
for (var i = 0; i < 200; i++)
34+
ChannelMember(
35+
pubkey: i.toRadixString(16).padLeft(64, '0'),
36+
role: 'member',
37+
joinedAt: DateTime(2026),
38+
displayName: i == 0 ? 'Scout' : 'Member $i',
39+
),
40+
];
41+
final agent = 'f' * 64;
42+
final cache = _RecordingUserCache();
43+
44+
await tester.pumpWidget(
45+
ProviderScope(
46+
overrides: [
47+
userCacheProvider.overrideWith(() => cache),
48+
channelMembersProvider(channelId).overrideWith((ref) async => roster),
49+
relayDirectoryUsersProvider.overrideWith(
50+
(ref) async => [
51+
DirectoryUser(pubkey: agent, displayName: 'Scout', isAgent: true),
52+
],
53+
),
54+
],
55+
child: MaterialApp(
56+
theme: AppTheme.light(),
57+
home: Scaffold(
58+
body: AddChannelMembersSheet(
59+
channelId: channelId,
60+
existingPubkeys: {for (final member in roster) member.pubkey},
61+
),
62+
),
63+
),
64+
),
65+
);
66+
await tester.pumpAndSettle();
67+
68+
// The shown agent is told apart from the human member named Scout.
69+
expect(find.text('Scout (agent)'), findsOneWidget);
70+
// Only the shown choice's profile is requested, not the whole roster.
71+
expect(cache.preloaded, {agent});
72+
});
73+
}

‎mobile/test/features/pulse/compose_note_page_test.dart‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import 'package:flutter/material.dart';
22
import 'package:flutter_test/flutter_test.dart';
33
import 'package:hooks_riverpod/hooks_riverpod.dart';
4+
import 'package:buzz/shared/identity_names/identity_names.dart';
5+
import 'package:buzz/shared/identity_names/identity_names_provider.dart';
46
import 'package:buzz/shared/profile/user_cache_provider.dart';
57
import 'package:buzz/shared/profile/user_profile.dart';
68
import 'package:buzz/features/pulse/compose_note_page.dart';
@@ -22,6 +24,24 @@ class _FakeUserCacheNotifier extends UserCacheNotifier {
2224
Future<bool> preload(List<String> pubkeys) async => true;
2325
}
2426

27+
class _MutableUserCache extends UserCacheNotifier {
28+
_MutableUserCache(this._initial);
29+
30+
final Map<String, UserProfile> _initial;
31+
32+
@override
33+
Map<String, UserProfile> build() => _initial;
34+
35+
@override
36+
UserProfile? get(String pubkey) => state[pubkey.toLowerCase()];
37+
38+
@override
39+
Future<bool> preload(List<String> pubkeys) async => true;
40+
41+
void replace(UserProfile profile) =>
42+
state = {...state, profile.pubkey: profile};
43+
}
44+
2545
void main() {
2646
final replyNote = UserNote(
2747
id: 'note1',
@@ -77,6 +97,45 @@ void main() {
7797
expect(_replyPreviewAvatarInitial(tester, 'Replying to Alice'), 'A');
7898
});
7999

100+
testWidgets('a supplied context follows profile changes while open', (
101+
tester,
102+
) async {
103+
final author = 'a' * 64;
104+
final cache = _MutableUserCache({
105+
author: UserProfile(pubkey: author, displayName: 'Scout'),
106+
});
107+
final note = UserNote(
108+
id: 'note-live',
109+
pubkey: author,
110+
createdAt: DateTime.now().millisecondsSinceEpoch ~/ 1000 - 120,
111+
content: 'Original',
112+
tags: const [],
113+
);
114+
await tester.pumpWidget(
115+
ProviderScope(
116+
overrides: [userCacheProvider.overrideWith(() => cache)],
117+
child: MaterialApp(
118+
theme: AppTheme.light(),
119+
home: Consumer(
120+
builder: (context, ref, _) {
121+
// The opener's context, as NoteCard passes it to the route.
122+
final IdentityNames opened = ref
123+
.read(identityNameSourcesProvider)
124+
.scope([author]);
125+
return ComposeNotePage(replyTo: note, names: opened);
126+
},
127+
),
128+
),
129+
),
130+
);
131+
await tester.pump();
132+
expect(find.text('Replying to Scout'), findsOneWidget);
133+
134+
cache.replace(UserProfile(pubkey: author, displayName: 'Renamed'));
135+
await tester.pump();
136+
expect(find.text('Replying to Renamed'), findsOneWidget);
137+
});
138+
80139
testWidgets('reply preview constrains its timestamp at large text sizes', (
81140
tester,
82141
) async {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import 'package:buzz/features/pulse/pulse_models.dart';
2+
import 'package:buzz/features/pulse/pulse_page.dart';
3+
import 'package:buzz/features/pulse/pulse_provider.dart';
4+
import 'package:buzz/shared/profile/user_cache_provider.dart';
5+
import 'package:buzz/shared/profile/user_profile.dart';
6+
import 'package:buzz/shared/relay/relay.dart';
7+
import 'package:buzz/shared/theme/theme.dart';
8+
import 'package:flutter/material.dart';
9+
import 'package:flutter_test/flutter_test.dart';
10+
import 'package:hooks_riverpod/hooks_riverpod.dart';
11+
12+
class _UserCache extends UserCacheNotifier {
13+
_UserCache(this._users);
14+
15+
final Map<String, UserProfile> _users;
16+
17+
@override
18+
Map<String, UserProfile> build() => _users;
19+
20+
@override
21+
UserProfile? get(String pubkey) => _users[pubkey.toLowerCase()];
22+
23+
@override
24+
Future<bool> preload(List<String> pubkeys) async => true;
25+
}
26+
27+
void main() {
28+
testWidgets('same-name reply targets on different notes are told apart', (
29+
tester,
30+
) async {
31+
// Two authors reply to two different people who are not authors on the
32+
// timeline but share the name Scout.
33+
final alice = '1' * 64, bob = '2' * 64;
34+
final scoutA = 'a' * 64, scoutB = 'b' * 64;
35+
final users = {
36+
alice: UserProfile(pubkey: alice, displayName: 'Alice'),
37+
bob: UserProfile(pubkey: bob, displayName: 'Bob'),
38+
scoutA: UserProfile(pubkey: scoutA, displayName: 'Scout'),
39+
scoutB: UserProfile(pubkey: scoutB, displayName: 'Scout'),
40+
};
41+
final createdAt =
42+
DateTime.utc(2025, 9, 30, 12).millisecondsSinceEpoch ~/ 1000;
43+
UserNote reply(String id, String author, String target) => UserNote(
44+
id: id,
45+
pubkey: author,
46+
createdAt: createdAt,
47+
content: 'A reply',
48+
tags: [
49+
['e', 'parent-$id', '', 'reply'],
50+
['p', target],
51+
],
52+
);
53+
54+
await tester.pumpWidget(
55+
ProviderScope(
56+
overrides: [
57+
userCacheProvider.overrideWith(() => _UserCache(users)),
58+
myPubkeyProvider.overrideWithValue(null),
59+
agentPubkeysProvider.overrideWith((ref) async => const []),
60+
globalNotesProvider.overrideWith(
61+
(ref) async => [
62+
reply('note-1', alice, scoutA),
63+
reply('note-2', bob, scoutB),
64+
],
65+
),
66+
noteReactionsProvider.overrideWith((ref, key) async => const {}),
67+
],
68+
child: MaterialApp(theme: AppTheme.light(), home: const PulsePage()),
69+
),
70+
);
71+
await tester.pumpAndSettle();
72+
73+
final replyLabels = tester
74+
.widgetList<Text>(find.textContaining('Replying to '))
75+
.map((text) => text.data)
76+
.toList();
77+
expect(replyLabels, hasLength(2));
78+
expect(replyLabels.toSet(), hasLength(2));
79+
expect(replyLabels, isNot(contains('Replying to Scout')));
80+
});
81+
}

0 commit comments

Comments
 (0)