@@ -2,6 +2,7 @@ import 'dart:async';
22
33import 'package:flutter_test/flutter_test.dart' ;
44import 'package:hooks_riverpod/hooks_riverpod.dart' ;
5+ import 'package:buzz/features/channels/agent_activity/working_bots_provider.dart' ;
56import 'package:buzz/features/channels/channel_management_provider.dart' ;
67import 'package:buzz/shared/mentions/agent_identity_provider.dart' ;
78import 'package:buzz/shared/relay/relay.dart' ;
@@ -28,7 +29,7 @@ void main() {
2829 });
2930 await relaySession.subscribed;
3031 expect (relaySession.liveFilters.single.kinds, const [39002 ]);
31- expect (relaySession.liveFilters.single.tags['#h ' ], [_channelId]);
32+ expect (relaySession.liveFilters.single.tags['#d ' ], [_channelId]);
3233
3334 relaySession.emit (_membershipEvent (role: 'member' ));
3435 await _pumpEventQueue ();
@@ -95,6 +96,44 @@ void main() {
9596
9697 expect (relaySession.unsubscribeCount, 1 );
9798 });
99+
100+ test (
101+ 'does not retain a live role subscription through working bots' ,
102+ () async {
103+ final relaySession = _MembershipRelaySessionNotifier ([
104+ _membershipEvent (role: 'bot' ),
105+ ]);
106+ final container = ProviderContainer (
107+ overrides: [relaySessionProvider.overrideWith (() => relaySession)],
108+ );
109+ addTearDown (container.dispose);
110+ final keepAlive = container.listen (
111+ workingBotPubkeysProvider (_channelId),
112+ (_, _) {},
113+ fireImmediately: true ,
114+ );
115+
116+ await relaySession.subscribed;
117+ keepAlive.close ();
118+ await container.pump ();
119+
120+ expect (relaySession.unsubscribeCount, 1 );
121+ },
122+ );
123+
124+ test ('blank profile labels defer to the directory label' , () {
125+ const pubkey = 'deadbeef0123456789' ;
126+
127+ expect (
128+ mentionNamesWithDirectoryLabels (
129+ mentionPubkeys: const [pubkey],
130+ profileMentionNames: const {pubkey: ' ' },
131+ directoryDisplayNames: const {pubkey: 'Directory bot' },
132+ agentMentionPubkeys: const {pubkey},
133+ ),
134+ const {pubkey: 'Directory bot' },
135+ );
136+ });
98137}
99138
100139const _channelId = '11111111-1111-4111-8111-111111111111' ;
@@ -107,7 +146,7 @@ NostrEvent _membershipEvent({required String role}) => NostrEvent(
107146 createdAt: 1 ,
108147 kind: 39002 ,
109148 tags: [
110- ['h ' , _channelId],
149+ ['d ' , _channelId],
111150 ['p' , _agentPubkey, 'wss://relay.example' , role],
112151 ],
113152 content: '' ,
@@ -122,7 +161,7 @@ Future<void> _pumpEventQueue() async {
122161class _MembershipRelaySessionNotifier extends RelaySessionNotifier {
123162 final List <NostrEvent > _memberships;
124163 final List <NostrFilter > liveFilters = [];
125- final List <void Function ( NostrEvent )> _listeners = [];
164+ final List <_LiveSubscription > _subscriptions = [];
126165 final Completer <void > _subscribed = Completer <void >();
127166 var unsubscribeCount = 0 ;
128167 var _membershipIndex = 0 ;
@@ -149,17 +188,40 @@ class _MembershipRelaySessionNotifier extends RelaySessionNotifier {
149188 void Function (String message)? onClosed,
150189 }) async {
151190 liveFilters.add (filter);
152- _listeners.add (onEvent);
191+ final subscription = _LiveSubscription (filter, onEvent);
192+ _subscriptions.add (subscription);
153193 if (! _subscribed.isCompleted) _subscribed.complete ();
154194 return () {
155195 unsubscribeCount++ ;
156- _listeners .remove (onEvent );
196+ _subscriptions .remove (subscription );
157197 };
158198 }
159199
160200 void emit (NostrEvent event) {
161- for (final listener in List .of (_listeners)) {
162- listener (event);
201+ for (final subscription in List .of (_subscriptions)) {
202+ if (_matches (subscription.filter, event)) {
203+ subscription.onEvent (event);
204+ }
163205 }
164206 }
165207}
208+
209+ class _LiveSubscription {
210+ final NostrFilter filter;
211+ final void Function (NostrEvent ) onEvent;
212+
213+ const _LiveSubscription (this .filter, this .onEvent);
214+ }
215+
216+ bool _matches (NostrFilter filter, NostrEvent event) {
217+ if (! filter.kinds.contains (event.kind)) return false ;
218+ return filter.tags.entries.every ((entry) {
219+ final tagName = entry.key.substring (1 );
220+ return event.tags.any (
221+ (tag) =>
222+ tag.isNotEmpty &&
223+ tag.first == tagName &&
224+ tag.skip (1 ).any (entry.value.contains),
225+ );
226+ });
227+ }
0 commit comments