Skip to content

Commit cdc2b12

Browse files
authored
fix(mobile): resolve DM display labels once per sort (#8488)
Home's DM sort resolves participant identity names again for every comparator call. This work appeared in a captured 82.163 ms UI frame after foregrounding the full app (Debug/JIT iOS simulator, shared host). Resolve each lowercase display label once per sort, retaining the same label ordering and channel-ID tie-breaker. Sort keys are invocation-local, so later viewer, profile, owner, participant, and community changes cannot reuse stale labels. No matching open mobile sorting PR or issue found. ### Validation - Added synthetic regression coverage for bounded contextual resolution and ordering parity across identity/name/channel changes, ties, unnamed DMs, and empty/singleton lists. The 80-DM fixture performs 80 contextual resolutions instead of the original comparator’s 982; restoring that comparator makes the regression fail. - 133 focused tests passed, including existing Home scrolling/reconnect widget tests and identity-name tests. - Mobile static analysis passed; 3,697 tests passed with 16 existing skips, plus four no-gateway configuration tests. - No new full-app or hardware capture. The existing trace establishes repeated synchronous work, not its release-build magnitude or the cause of every scrolling stall. Signed-off-by: Tom Brow <[email protected]>
1 parent fcdabcf commit cdc2b12

2 files changed

Lines changed: 187 additions & 16 deletions

File tree

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

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,28 +128,29 @@ String dmAvatarInitial(Channel channel, {String? currentPubkey}) {
128128
return label.isNotEmpty ? label[0].toUpperCase() : '?';
129129
}
130130

131+
/// Sorts by the visible label, resolving each label only once per invocation.
131132
List<Channel> sortDmChannelsByDisplayLabel(
132133
Iterable<Channel> channels, {
133134
String? currentPubkey,
134135
IdentityNameSources? names,
135136
}) {
136-
final sorted = channels.toList();
137+
// Keep sort keys local: the next call must reflect current viewer, channel
138+
// participants, and community profile/owner names.
139+
final sorted = [
140+
for (final channel in channels)
141+
(
142+
channel: channel,
143+
label: resolveDmChannelDisplayLabel(
144+
channel,
145+
currentPubkey: currentPubkey,
146+
names: names,
147+
).toLowerCase(),
148+
),
149+
];
137150
sorted.sort((left, right) {
138-
final leftLabel = resolveDmChannelDisplayLabel(
139-
left,
140-
currentPubkey: currentPubkey,
141-
names: names,
142-
);
143-
final rightLabel = resolveDmChannelDisplayLabel(
144-
right,
145-
currentPubkey: currentPubkey,
146-
names: names,
147-
);
148-
final labelCompare = leftLabel.toLowerCase().compareTo(
149-
rightLabel.toLowerCase(),
150-
);
151+
final labelCompare = left.label.compareTo(right.label);
151152
if (labelCompare != 0) return labelCompare;
152-
return left.id.compareTo(right.id);
153+
return left.channel.id.compareTo(right.channel.id);
153154
});
154-
return sorted;
155+
return [for (final entry in sorted) entry.channel];
155156
}
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
import 'package:buzz/features/channels/channel.dart';
2+
import 'package:buzz/features/channels/dm_channel_labels.dart';
3+
import 'package:buzz/shared/identity_names/identity_names.dart';
4+
import 'package:buzz/shared/profile/user_profile.dart';
5+
import 'package:flutter_test/flutter_test.dart';
6+
7+
String _key(int number) => number.toRadixString(16).padLeft(64, '0');
8+
9+
Channel _dm(
10+
String id, {
11+
String name = 'DM',
12+
List<String> keys = const [],
13+
List<String> labels = const [],
14+
}) => Channel(
15+
id: id,
16+
name: name,
17+
channelType: 'dm',
18+
visibility: 'private',
19+
description: '',
20+
createdBy: _key(1),
21+
createdAt: DateTime(2026),
22+
memberCount: keys.length,
23+
participantPubkeys: keys,
24+
participants: labels,
25+
isMember: true,
26+
);
27+
28+
// Count real contextual resolutions, without replacing the naming policy.
29+
class _CountingSources extends IdentityNameSources {
30+
final List<List<String>> scopes = [];
31+
32+
_CountingSources({required super.profiles});
33+
34+
@override
35+
IdentityNames scope(
36+
Iterable<String> candidates, {
37+
Set<String> agentPubkeys = const {},
38+
Map<String, String> fallbackNames = const {},
39+
Map<String, String> ownerPubkeys = const {},
40+
}) {
41+
scopes.add(candidates.toList());
42+
return super.scope(
43+
candidates,
44+
agentPubkeys: agentPubkeys,
45+
fallbackNames: fallbackNames,
46+
ownerPubkeys: ownerPubkeys,
47+
);
48+
}
49+
}
50+
51+
void main() {
52+
test('sorting resolves each DM context once, independent of comparisons', () {
53+
final channels = [
54+
for (var index = 0; index < 80; index++)
55+
_dm('dm-$index', keys: [_key(1), _key(index + 2)]),
56+
];
57+
final names = _CountingSources(
58+
profiles: {
59+
for (var index = 0; index < channels.length; index++)
60+
_key(index + 2): UserProfile(
61+
pubkey: _key(index + 2),
62+
displayName: 'Person ${(79 - index).toString().padLeft(2, '0')}',
63+
),
64+
},
65+
);
66+
67+
final sorted = sortDmChannelsByDisplayLabel(
68+
channels,
69+
currentPubkey: _key(1),
70+
names: names,
71+
);
72+
73+
expect(sorted, channels.reversed.toList());
74+
expect(names.scopes, hasLength(channels.length));
75+
expect(
76+
names.scopes,
77+
unorderedEquals(channels.map((channel) => channel.participantPubkeys)),
78+
);
79+
expect(channels.first.id, 'dm-0', reason: 'Do not mutate the source list');
80+
});
81+
82+
test(
83+
'each sort uses current labels, identity, participants and community',
84+
() {
85+
final me = _key(1);
86+
final alice = _key(2);
87+
final agent = _key(3);
88+
final otherAgent = _key(4);
89+
final owner = _key(5);
90+
final channels = [
91+
_dm('direct', keys: [me, alice], labels: ['Zoe', 'Alice']),
92+
_dm(
93+
'group',
94+
name: 'Group DM (4)',
95+
keys: [me, alice, agent, otherAgent],
96+
labels: ['Zoe', 'Alice', 'Scout', 'Scout'],
97+
),
98+
_dm('self', keys: [me], labels: ['Zoe']),
99+
_dm('empty'),
100+
_dm('legacy', keys: ['legacy-key'], labels: ['Bob']),
101+
_dm('tie-b', name: 'ALICE'),
102+
_dm('tie-a', name: 'alice'),
103+
];
104+
final updatedNames = IdentityNameSources(
105+
viewer: me,
106+
profiles: {
107+
me: UserProfile(pubkey: me, displayName: 'Zoe'),
108+
alice: UserProfile(pubkey: alice, displayName: 'Yvonne'),
109+
agent: UserProfile(
110+
pubkey: agent,
111+
displayName: 'Scout',
112+
ownerPubkey: me,
113+
),
114+
otherAgent: UserProfile(
115+
pubkey: otherAgent,
116+
displayName: 'Scout',
117+
ownerPubkey: owner,
118+
),
119+
owner: UserProfile(pubkey: owner, displayName: 'Aaron'),
120+
},
121+
);
122+
final cases = [
123+
(channels: channels, viewer: me, names: null),
124+
(channels: channels, viewer: me, names: updatedNames),
125+
(channels: channels, viewer: alice, names: updatedNames),
126+
(
127+
channels: [
128+
channels.first.copyWith(name: 'Renamed'),
129+
_dm('group', keys: [agent, owner], labels: ['Scout', 'Aaron']),
130+
...channels.skip(2),
131+
],
132+
viewer: me,
133+
names: updatedNames,
134+
),
135+
(channels: channels, viewer: me, names: const IdentityNameSources()),
136+
(channels: <Channel>[], viewer: me, names: updatedNames),
137+
(channels: [channels.first], viewer: me, names: updatedNames),
138+
];
139+
140+
for (final scenario in cases) {
141+
// Independent reference: the previous comparator's ordering contract.
142+
final expected = scenario.channels.toList()
143+
..sort((left, right) {
144+
String label(Channel channel) => resolveDmChannelDisplayLabel(
145+
channel,
146+
currentPubkey: scenario.viewer,
147+
names: scenario.names,
148+
).toLowerCase();
149+
final order = label(left).compareTo(label(right));
150+
return order != 0 ? order : left.id.compareTo(right.id);
151+
});
152+
expect(
153+
sortDmChannelsByDisplayLabel(
154+
scenario.channels,
155+
currentPubkey: scenario.viewer,
156+
names: scenario.names,
157+
),
158+
expected,
159+
);
160+
}
161+
expect(
162+
sortDmChannelsByDisplayLabel(
163+
channels,
164+
currentPubkey: me,
165+
).map((channel) => channel.id),
166+
['direct', 'tie-a', 'tie-b', 'group', 'legacy', 'empty', 'self'],
167+
);
168+
},
169+
);
170+
}

0 commit comments

Comments
 (0)