Skip to content

Commit 99e65b6

Browse files
browsquare-tombstudio
authored
perf(mobile): read channel bot roles from the member list (#9451)
Every channel view read the channel's member roster (kind 39002) twice: once for the member list (`channelMembersProvider`) and once for bot roles (`channelBotPubkeysProvider`, which feeds agent mention styling). Both sent the same `kinds:[39002] #d:[channel] limit:1` query, and both re-ran it on every roster update. Screens that show messages from several channels did this per channel: opening Activity read 10 rosters twice each. Bot roles now come from `channelMembersProvider`, so each channel's roster is fetched once. While the relay is disconnected, bot roles now come from the member list's last loaded roster instead of going empty, so agent mentions keep their styling offline. ### Validation New test: watching both the member list and bot roles for a channel fetches the roster once and both report the bot. It fails without this change (two fetches). The existing bot-role tests (live updates, subscription cleanup, working bots) pass unchanged. The channel page test for a bot-role replay used a fixed member list alongside live bot roles, which can no longer differ; it now reads both from its fake relay. Full mobile test suite and `flutter analyze` pass. On an iPhone 12 mini signed in to Block's Builderlab (instrumented profile build), opening Activity right after launch read 10 channel rosters once each (25.7 KB) instead of twice (51.5 KB), and opening a channel read its roster once instead of twice. Agent messages kept their agent styling. The same automated run covered cold start, opening and scrolling Activity, scrolling the home list, opening buzz-mobile-tomb-review (a channel with agent members) and returning home. ### Regressions None expected. Bot roles now refresh when the member list does (on roster updates and after a reconnect) rather than on every relay session state change, which only drops redundant refetches. Signed-off-by: Tom Brow <[email protected]> Co-authored-by: Tom Brow <[email protected]> Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@buzz.block.builderlab.xyz>
1 parent 3dcdf1c commit 99e65b6

3 files changed

Lines changed: 45 additions & 28 deletions

File tree

‎mobile/lib/shared/mentions/agent_identity_provider.dart‎

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:convert';
44
import 'package:flutter/foundation.dart';
55
import 'package:hooks_riverpod/hooks_riverpod.dart';
66

7+
import '../../features/channels/channel_management_provider.dart';
78
import '../../shared/crypto/nip_oa.dart';
89
import '../../shared/relay/relay.dart';
910
import '../../shared/utils/string_utils.dart';
@@ -321,22 +322,14 @@ final channelMembershipUpdateProvider = NotifierProvider.autoDispose
321322
);
322323

323324
/// Bot pubkeys currently assigned a channel bot role.
325+
///
326+
/// Read from [channelMembersProvider] so a channel's roster is fetched once
327+
/// for both its member list and its bot roles.
324328
final channelBotPubkeysProvider = FutureProvider.autoDispose
325329
.family<Set<String>, String>((ref, channelId) async {
326-
ref.watch(
327-
channelMembershipUpdateProvider(
328-
channelId,
329-
).select((update) => update.version),
330-
);
331-
final sessionState = ref.watch(relaySessionProvider);
332-
if (sessionState.status != SessionStatus.connected) return const {};
333-
final session = ref.read(relaySessionProvider.notifier);
334-
final events = await session.fetchHistory(
335-
NostrFilters.channelMembers(channelId),
336-
);
337-
if (events.isEmpty) return const {};
330+
final members = await ref.watch(channelMembersProvider(channelId).future);
338331
return _AgentPubkeySet({
339-
for (final member in membersFromEvent(events.first))
332+
for (final member in members)
340333
if (member.role == 'bot') member.pubkey.toLowerCase(),
341334
});
342335
});

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

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -422,9 +422,10 @@ Widget _buildTestable({
422422
authorPubkey: null,
423423
),
424424
),
425-
channelMembersProvider(_channelId).overrideWith(
426-
(ref) async => loadMembers != null ? loadMembers() : members,
427-
),
425+
if (!watchChannelMembershipUpdates)
426+
channelMembersProvider(_channelId).overrideWith(
427+
(ref) async => loadMembers != null ? loadMembers() : members,
428+
),
428429
channelMembersProvider(_huddleChannelId).overrideWith(
429430
(ref) async => huddleMembersNotifier == null
430431
? huddleMembers
@@ -2665,18 +2666,6 @@ void main() {
26652666
channel: dmChannel,
26662667
relaySessionNotifier: relaySession,
26672668
watchChannelMembershipUpdates: true,
2668-
members: [
2669-
ChannelMember(
2670-
pubkey: 'self',
2671-
role: 'member',
2672-
joinedAt: DateTime(2025),
2673-
),
2674-
ChannelMember(
2675-
pubkey: 'alice',
2676-
role: 'member',
2677-
joinedAt: DateTime(2025),
2678-
),
2679-
],
26802669
),
26812670
);
26822671
await tester.pumpAndSettle();

‎mobile/test/shared/mentions/agent_identity_provider_test.dart‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,39 @@ void main() {
4141
);
4242
});
4343

44+
test('reads bot roles from the member list without a second fetch', () async {
45+
final relaySession = _MembershipRelaySessionNotifier([
46+
_membershipEvent(role: 'bot'),
47+
]);
48+
final container = ProviderContainer(
49+
overrides: [relaySessionProvider.overrideWith(() => relaySession)],
50+
);
51+
addTearDown(container.dispose);
52+
final members = container.listen(
53+
channelMembersProvider(_channelId),
54+
(_, _) {},
55+
fireImmediately: true,
56+
);
57+
addTearDown(members.close);
58+
final bots = container.listen(
59+
channelBotPubkeysProvider(_channelId),
60+
(_, _) {},
61+
fireImmediately: true,
62+
);
63+
addTearDown(bots.close);
64+
65+
expect(await container.read(channelBotPubkeysProvider(_channelId).future), {
66+
_agentPubkey,
67+
});
68+
expect(
69+
(await container.read(
70+
channelMembersProvider(_channelId).future,
71+
)).single.role,
72+
'bot',
73+
);
74+
expect(relaySession.fetchCount, 1);
75+
});
76+
4477
test('refreshes channel members from live membership updates', () async {
4578
final relaySession = _MembershipRelaySessionNotifier([
4679
_membershipEvent(role: 'bot'),
@@ -305,6 +338,7 @@ class _MembershipRelaySessionNotifier extends RelaySessionNotifier {
305338
final List<_LiveSubscription> _subscriptions = [];
306339
final Completer<void> _subscribed = Completer<void>();
307340
var unsubscribeCount = 0;
341+
var fetchCount = 0;
308342
var _membershipIndex = 0;
309343

310344
_MembershipRelaySessionNotifier(this._memberships, {this.subscribeError});
@@ -319,6 +353,7 @@ class _MembershipRelaySessionNotifier extends RelaySessionNotifier {
319353
NostrFilter filter, {
320354
Duration timeout = const Duration(seconds: 8),
321355
}) async {
356+
fetchCount++;
322357
return [_memberships[_membershipIndex++]];
323358
}
324359

0 commit comments

Comments
 (0)