Skip to content

Commit 25a93bb

Browse files
loganjLarry
andcommitted
fix(mobile): align DM avatar and blank-name fallbacks with npub identity
PR-M1 review follow-up (#7493): both findings from the exact-head review. ## Summary - dm_channel_labels.dart: dmAvatarInitial now mirrors resolveDmChannelDisplayLabel's participant selection — the first participant that is not the current user, falling back to the first participant when every participant is the current user — instead of blindly reading participants.first. Member order does not guarantee the counterpart is listed first, so a [self, unnamedOther] DM rendered the counterpart's compact npub label beside an avatar initial derived from the current user's key. Both call sites (DM tile, DM header) pass currentPubkey, so the avatar initial now identifies the same counterpart the label names; unnamed counterparts keep the hex-key-derived initial and named ones keep their authored initial. - user_profile.dart: UserProfile.label trims the display name and falls back to the compact npub when it is empty or whitespace-only, mirroring `initial` (which already rejected blank names). Relay profiles can carry a blank display_name through the cache unchanged, which previously rendered an empty author label on every surface using profile?.label. - Regressions at the production seams: self-first [self, unnamedOther] widget tests for the DM tile and DM header asserting that label and avatar both identify the counterpart (each fails with the current user's initial against the old first-participant selection), blank and whitespace-only display-name scenarios in the pulse author-identity table, and a UserProfile label/initial contract table. Full suite: 2095 passing. flutter analyze clean, dart format clean. Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Signed-off-by: Logan Johnson <[email protected]>
1 parent ed503d3 commit 25a93bb

8 files changed

Lines changed: 204 additions & 12 deletions

File tree

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,12 @@ class _DmAppBarTitle extends ConsumerWidget {
240240
final animatedAvatar = parseAnimatedAvatarUrl(avatarUrl);
241241
// Keyed to the hex public key when the participant is unnamed and the
242242
// profile isn't cached — the compact-npub participant label would
243-
// otherwise render `N` for every unnamed DM counterpart.
244-
final initial = profile?.initial ?? dmAvatarInitial(channel);
243+
// otherwise render `N` for every unnamed DM counterpart. Selection skips
244+
// the current user like the header label does, so the initial always
245+
// identifies the same counterpart the label names.
246+
final initial =
247+
profile?.initial ??
248+
dmAvatarInitial(channel, currentPubkey: currentPubkey);
245249
final presenceLabel = switch (presence) {
246250
'online' => 'Online',
247251
'away' => 'Away',

‎mobile/lib/features/channels/channels_page/channel_tile.dart‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,12 @@ class _DmAvatar extends ConsumerWidget {
193193
final avatarUrl = profile?.avatarUrl;
194194
// Keyed to the hex public key when the counterpart is unnamed and the
195195
// profile isn't cached — the compact-npub participant label would
196-
// otherwise render `N` for every unnamed DM counterpart.
197-
final initial = profile?.initial ?? dmAvatarInitial(channel);
196+
// otherwise render `N` for every unnamed DM counterpart. Selection skips
197+
// the current user like the row label does, so the initial always
198+
// identifies the same counterpart the label names.
199+
final initial =
200+
profile?.initial ??
201+
dmAvatarInitial(channel, currentPubkey: currentPubkey);
198202
return SizedBox(
199203
width: _kDmAvatarSize,
200204
height: _kDmAvatarSize,

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,42 @@ String resolveDmChannelDisplayLabel(Channel channel, {String? currentPubkey}) {
5454
: channel.name;
5555
}
5656

57-
/// Avatar initial for the channel's first DM participant label.
57+
/// Avatar initial for the DM's visible counterpart.
58+
///
59+
/// Mirrors [resolveDmChannelDisplayLabel]'s participant selection: the
60+
/// first participant that is not [currentPubkey] (falling back to the first
61+
/// participant when every participant is the current user), since member
62+
/// order does not guarantee the counterpart is listed first — otherwise the
63+
/// avatar could identify the current user while the label beside it
64+
/// identifies the counterpart.
5865
///
5966
/// A resolved display name keeps its name-derived initial. A label that
6067
/// fell back to the compact npub form of the participant's key is keyed to
6168
/// the hex public key instead — the npub form starts with `npub1`, so every
6269
/// unnamed participant would otherwise render `N`.
63-
String dmAvatarInitial(Channel channel) {
64-
if (channel.participants.isEmpty) return '?';
65-
final label = channel.participants.first;
66-
final pubkey = channel.participantPubkeys.isNotEmpty
67-
? channel.participantPubkeys.first
68-
: '';
70+
String dmAvatarInitial(Channel channel, {String? currentPubkey}) {
71+
final normalizedCurrent = currentPubkey?.toLowerCase();
72+
var index = 0;
73+
while (normalizedCurrent != null &&
74+
index < channel.participantPubkeys.length &&
75+
channel.participantPubkeys[index].toLowerCase() == normalizedCurrent) {
76+
index++;
77+
}
78+
79+
// No participant pubkeys (labels only) or every pubkey is the current
80+
// user: fall back to the first participant label, like the channel label
81+
// does when the non-self list is empty.
82+
if (index >= channel.participantPubkeys.length) {
83+
final label = channel.participants.isNotEmpty
84+
? channel.participants.first
85+
: '';
86+
return label.isNotEmpty ? label[0].toUpperCase() : '?';
87+
}
88+
89+
final pubkey = channel.participantPubkeys[index];
90+
final label = index < channel.participants.length
91+
? channel.participants[index]
92+
: shortPubkey(pubkey);
6993
if (pubkey.isNotEmpty && label == shortPubkey(pubkey)) {
7094
return pubkey[0].toUpperCase();
7195
}

‎mobile/lib/shared/profile/user_profile.dart‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,15 @@ class UserProfile {
3434
);
3535

3636
/// Short label: display name, or the compact npub form of the public key.
37-
String get label => displayName ?? shortPubkey(pubkey);
37+
///
38+
/// Blank display names (empty or whitespace-only) fall back to the compact
39+
/// npub too — relay profiles can carry them — so a valid identity never
40+
/// renders an empty label. Mirrors [initial], which already rejects blank
41+
/// names.
42+
String get label {
43+
final name = displayName?.trim();
44+
return name != null && name.isNotEmpty ? name : shortPubkey(pubkey);
45+
}
3846

3947
/// First letter for fallback avatar.
4048
String get initial {

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,44 @@ void main() {
619619
expect(_dmHeaderAvatarInitial(tester), 'A');
620620
});
621621

622+
testWidgets('keys DM header fallback avatars to the non-self counterpart', (
623+
tester,
624+
) async {
625+
// Member order does not guarantee the counterpart is listed first:
626+
// the current user (self, from the fake profile) comes FIRST, so an
627+
// avatar keyed to the first participant would render the current
628+
// user's initial while the header label names the counterpart.
629+
const b0b =
630+
'b0b0000000000000000000000000000000000000000000000000000000000000';
631+
final dmChannel = Channel(
632+
id: _channelId,
633+
name: 'DM',
634+
channelType: 'dm',
635+
visibility: 'private',
636+
description: 'Direct message',
637+
createdBy: 'self',
638+
createdAt: DateTime(2025),
639+
memberCount: 2,
640+
participants: ['Self', shortPubkey(b0b)],
641+
participantPubkeys: const ['self', b0b],
642+
isMember: true,
643+
);
644+
645+
await tester.pumpWidget(
646+
_buildTestable(messages: const [], channel: dmChannel),
647+
);
648+
await tester.pumpAndSettle();
649+
650+
// Label and avatar agree on the counterpart's key: the compact npub
651+
// names the unnamed counterpart, and the avatar initial is keyed to
652+
// that same hex key — never the current user's `S`.
653+
expect(
654+
tester.widget<Text>(find.byKey(const ValueKey('dm-header-name'))).data,
655+
shortPubkey(b0b),
656+
);
657+
expect(_dmHeaderAvatarInitial(tester), 'B');
658+
});
659+
622660
testWidgets('uses a fallback squircle for bot-role DM participants', (
623661
tester,
624662
) async {

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,63 @@ void main() {
250250
expect(sectionTitle.style?.fontWeight, FontWeight.w600);
251251
});
252252

253+
testWidgets('keys DM tile fallback avatars to the non-self counterpart', (
254+
tester,
255+
) async {
256+
// Valid fixture keys whose npub encodings were verified against the
257+
// NIP-19 codec independently of the code under test. The current user
258+
// (aabb, from the fake profile) is listed FIRST — member order does
259+
// not guarantee the counterpart is first — so an avatar that keys off
260+
// the first participant would identify the current user while the
261+
// label beside it identifies the counterpart.
262+
const b0b =
263+
'b0b0000000000000000000000000000000000000000000000000000000000000';
264+
final selfFirstUnnamedDm = Channel(
265+
id: 'dm-self-first-unnamed',
266+
name: 'DM',
267+
channelType: 'dm',
268+
visibility: 'open',
269+
description: 'Direct message',
270+
createdBy: 'aabb',
271+
createdAt: DateTime(2025),
272+
memberCount: 2,
273+
participants: ['Test', shortPubkey(b0b)],
274+
participantPubkeys: const ['aabb', b0b],
275+
isMember: true,
276+
);
277+
final selfFirstNamedDm = Channel(
278+
id: 'dm-self-first-named',
279+
name: 'DM',
280+
channelType: 'dm',
281+
visibility: 'open',
282+
description: 'Direct message',
283+
createdBy: 'aabb',
284+
createdAt: DateTime(2025),
285+
memberCount: 2,
286+
participants: const ['Test', 'Bob'],
287+
participantPubkeys: const ['aabb', b0b],
288+
isMember: true,
289+
);
290+
await tester.pumpWidget(
291+
buildTestable(
292+
overrides: [
293+
channelsProvider.overrideWith(
294+
() => _FakeNotifier([selfFirstUnnamedDm, selfFirstNamedDm]),
295+
),
296+
],
297+
),
298+
);
299+
await tester.pumpAndSettle();
300+
301+
// Label and avatar agree on the counterpart's key: the compact npub
302+
// names the unnamed counterpart, and the avatar initial is keyed to
303+
// that same hex key — never the current user's `A`.
304+
expect(find.text(shortPubkey(b0b)), findsOneWidget);
305+
expect(_dmTileAvatarInitial(tester, shortPubkey(b0b)), 'B');
306+
// A named counterpart listed second keeps its authored initial too.
307+
expect(_dmTileAvatarInitial(tester, 'Bob'), 'B');
308+
});
309+
253310
testWidgets('sizes the community header for accessible text', (tester) async {
254311
await tester.pumpWidget(
255312
buildTestable(

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,27 @@ void main() {
201201
npubLabelCount: 0,
202202
avatarInitial: 'C',
203203
),
204+
// Cached blank display names (empty or whitespace-only — relay
205+
// profiles can carry both) must fall back to the compact npub
206+
// for the label instead of rendering an empty author row.
207+
(
208+
id: 'cached-blank-name-author',
209+
tags: const [],
210+
users: {b0b: const UserProfile(pubkey: b0b, displayName: '')},
211+
authorLabel: 'npub1kzc\u2026uyv8',
212+
replyLabel: null,
213+
npubLabelCount: 1,
214+
avatarInitial: 'B',
215+
),
216+
(
217+
id: 'cached-whitespace-name-author',
218+
tags: const [],
219+
users: {b0b: const UserProfile(pubkey: b0b, displayName: ' ')},
220+
authorLabel: 'npub1kzc\u2026uyv8',
221+
replyLabel: null,
222+
npubLabelCount: 1,
223+
avatarInitial: 'B',
224+
),
204225
];
205226

206227
for (final scenario in scenarios) {
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import 'package:buzz/shared/profile/user_profile.dart';
2+
import 'package:buzz/shared/utils/string_utils.dart';
3+
import 'package:flutter_test/flutter_test.dart';
4+
5+
void main() {
6+
group('UserProfile label and initial', () {
7+
// Valid fixture key whose npub encoding was verified against the NIP-19
8+
// codec independently of the code under test.
9+
const b0b =
10+
'b0b0000000000000000000000000000000000000000000000000000000000000';
11+
12+
// Relay profiles can carry a blank `display_name` (empty or
13+
// whitespace-only) and reach the cache unchanged, so both accessors must
14+
// reject blank names and fall back to the key instead of rendering an
15+
// empty label. Resolved names keep their authored label and initial
16+
// (whitespace-only padding is trimmed, like `initial` already did).
17+
final cases = <({String? displayName, String label, String initial})>[
18+
(displayName: null, label: shortPubkey(b0b), initial: 'B'),
19+
(displayName: '', label: shortPubkey(b0b), initial: 'B'),
20+
(displayName: ' ', label: shortPubkey(b0b), initial: 'B'),
21+
(displayName: 'Carol', label: 'Carol', initial: 'C'),
22+
(displayName: ' Carol ', label: 'Carol', initial: 'C'),
23+
];
24+
25+
for (final testCase in cases) {
26+
test('displayName ${testCase.displayName ?? '(null)'}', () {
27+
final profile = UserProfile(
28+
pubkey: b0b,
29+
displayName: testCase.displayName,
30+
);
31+
expect(profile.label, testCase.label);
32+
expect(profile.initial, testCase.initial);
33+
});
34+
}
35+
});
36+
}

0 commit comments

Comments
 (0)