@@ -32,44 +32,74 @@ pub fn reader_authorized_for_event(event: &nostr::Event, reader_pubkey_hex: &str
3232 . any ( |t| t. content ( ) == Some ( reader_pubkey_hex) )
3333}
3434
35- /// The tag that marks a kind:44100/44101 membership notice about a system
36- /// channel (NIP-CL): `["t", "system"]`. Only the relay signs these kinds, and
37- /// it adds the tag only when the channel is a system channel.
38- pub const SYSTEM_NOTICE_TAG : [ & str ; 2 ] = [ "t" , "system" ] ;
39-
40- /// The kinds that [`SYSTEM_NOTICE_TAG`] can mark.
41- pub const SYSTEM_NOTICE_KINDS : [ u32 ; 2 ] = [
35+ /// The membership notice kinds (44100 added, 44101 removed). Only the relay
36+ /// signs them, and it tags each one with its channel's type:
37+ /// `["t", <channel_type>]` (NIP-CL).
38+ pub const MEMBERSHIP_NOTICE_KINDS : [ u32 ; 2 ] = [
4239 crate :: kind:: KIND_MEMBER_ADDED_NOTIFICATION ,
4340 crate :: kind:: KIND_MEMBER_REMOVED_NOTIFICATION ,
4441] ;
4542
46- /// Whether `event` is a membership notice about a system channel.
43+ /// The tag name that carries a membership notice's channel type.
44+ pub const NOTICE_CHANNEL_TYPE_TAG : & str = "t" ;
45+
46+ /// The channel types that clients knew before NIP-CL typed membership
47+ /// notices. A filter without `#t` sees notices only about these types, and a
48+ /// notice without a type tag (stored before NIP-CL) counts as all of them.
4749///
48- /// Such a notice is hidden by default: it matches only a filter that asks
49- /// for it ([`filter_asks_for_system_notices`]). Clients that do not know
50- /// system channels never ask, so they never see one, on any read or live
51- /// path that matches through [`filters_match`] or applies the same rule in
52- /// SQL (`EventQuery::include_system_notices`, which defaults to off).
53- pub fn is_system_notice ( event : & nostr:: Event ) -> bool {
54- SYSTEM_NOTICE_KINDS . contains ( & crate :: kind:: event_kind_u32 ( event) )
55- && event. tags . iter ( ) . any ( |tag| {
56- let tag = tag. as_slice ( ) ;
57- tag. first ( ) . map ( String :: as_str) == Some ( SYSTEM_NOTICE_TAG [ 0 ] )
58- && tag. get ( 1 ) . map ( String :: as_str) == Some ( SYSTEM_NOTICE_TAG [ 1 ] )
59- } )
50+ /// Frozen. Do not add a type here: a type added later stays hidden from every
51+ /// client that does not name it in `#t`.
52+ pub const LEGACY_NOTICE_CHANNEL_TYPES : [ & str ; 4 ] = [ "stream" , "forum" , "dm" , "workflow" ] ;
53+
54+ /// Whether `event` is a membership notice (kind 44100/44101).
55+ pub fn is_membership_notice ( event : & nostr:: Event ) -> bool {
56+ MEMBERSHIP_NOTICE_KINDS . contains ( & crate :: kind:: event_kind_u32 ( event) )
6057}
6158
62- /// Whether `filter` asks for system-channel membership notices: its `#t`
63- /// values include `"system"`.
64- pub fn filter_asks_for_system_notices ( filter : & Filter ) -> bool {
59+ /// The channel types whose membership notices `filter` may see: its `#t`
60+ /// values, or [`LEGACY_NOTICE_CHANNEL_TYPES`] when it has no `#t`.
61+ ///
62+ /// This is the one rule for notice visibility (NIP-CL). [`filters_match`]
63+ /// applies it, and `EventQuery::notice_channel_types` applies it in SQL.
64+ pub fn notice_channel_types ( filter : & Filter ) -> Option < Vec < String > > {
6565 filter
6666 . generic_tags
6767 . get ( & nostr:: SingleLetterTag :: lowercase ( nostr:: Alphabet :: T ) )
68- . is_some_and ( |values| values. iter ( ) . any ( |value| value == SYSTEM_NOTICE_TAG [ 1 ] ) )
68+ . map ( |values| values. iter ( ) . map ( ToString :: to_string) . collect ( ) )
69+ }
70+
71+ /// Whether a filter whose visible types are `visible` (see
72+ /// [`notice_channel_types`]) may see membership notice `event`.
73+ ///
74+ /// A tagged notice matches when one of its types is visible. An untagged
75+ /// notice is a legacy type, so it matches when a legacy type is visible.
76+ pub fn notice_visible ( visible : Option < & [ String ] > , event : & nostr:: Event ) -> bool {
77+ let is_visible = |channel_type : & str | match visible {
78+ Some ( types) => types. iter ( ) . any ( |t| t == channel_type) ,
79+ None => LEGACY_NOTICE_CHANNEL_TYPES . contains ( & channel_type) ,
80+ } ;
81+ let mut tagged = false ;
82+ for tag in event. tags . iter ( ) {
83+ let tag = tag. as_slice ( ) ;
84+ if tag. first ( ) . map ( String :: as_str) != Some ( NOTICE_CHANNEL_TYPE_TAG ) {
85+ continue ;
86+ }
87+ tagged = true ;
88+ if tag
89+ . get ( 1 )
90+ . is_some_and ( |channel_type| is_visible ( channel_type) )
91+ {
92+ return true ;
93+ }
94+ }
95+ !tagged && LEGACY_NOTICE_CHANNEL_TYPES . iter ( ) . any ( |t| is_visible ( t) )
6996}
7097
7198fn filter_match_one ( f : & Filter , ev : & StoredEvent ) -> bool {
72- if is_system_notice ( & ev. event ) && !filter_asks_for_system_notices ( f) {
99+ // A membership notice's `#t` is its channel type, matched by the notice
100+ // rule above instead of the plain tag match below.
101+ let notice = is_membership_notice ( & ev. event ) ;
102+ if notice && !notice_visible ( notice_channel_types ( f) . as_deref ( ) , & ev. event ) {
73103 return false ;
74104 }
75105
@@ -107,6 +137,9 @@ fn filter_match_one(f: &Filter, ev: &StoredEvent) -> bool {
107137
108138 for ( tag_key, tag_values) in f. generic_tags . iter ( ) {
109139 let tag_key_str = tag_key. to_string ( ) ;
140+ if notice && tag_key_str == NOTICE_CHANNEL_TYPE_TAG {
141+ continue ;
142+ }
110143 let has_match = tag_values. iter ( ) . any ( |filter_val| {
111144 ev. event
112145 . tags
@@ -160,16 +193,16 @@ mod tests {
160193 }
161194
162195 #[ test]
163- fn a_system_notice_matches_only_a_filter_that_asks ( ) {
196+ fn a_notice_matches_only_filters_that_know_its_channel_type ( ) {
164197 let relay = Keys :: generate ( ) ;
165198 let member = Keys :: generate ( ) . public_key ( ) ;
166- let notice = |kind : u32 , system : bool | {
199+ let notice = |kind : u32 , channel_type : Option < & str > | {
167200 let mut tags = vec ! [
168201 Tag :: parse( [ "p" , & member. to_hex( ) ] ) . unwrap( ) ,
169202 Tag :: parse( [ "h" , "c" ] ) . unwrap( ) ,
170203 ] ;
171- if system {
172- tags. push ( Tag :: parse ( SYSTEM_NOTICE_TAG ) . unwrap ( ) ) ;
204+ if let Some ( channel_type ) = channel_type {
205+ tags. push ( Tag :: parse ( [ NOTICE_CHANNEL_TYPE_TAG , channel_type ] ) . unwrap ( ) ) ;
173206 }
174207 let event = EventBuilder :: new ( Kind :: Custom ( kind as u16 ) , "" )
175208 . tags ( tags)
@@ -179,36 +212,66 @@ mod tests {
179212 } ;
180213 let filter = |json : serde_json:: Value | -> Filter { serde_json:: from_value ( json) . unwrap ( ) } ;
181214 let p = member. to_hex ( ) ;
182- let old = [
183- filter ( serde_json:: json!( { "kinds" : [ 44100 , 44101 ] , "#p" : [ p] } ) ) ,
184- filter ( serde_json:: json!( { "#p" : [ p] } ) ) ,
185- filter ( serde_json:: json!( { } ) ) ,
186- filter ( serde_json:: json!( { "#t" : [ "other" ] } ) ) ,
187- ] ;
188- let asks = [
189- filter ( serde_json:: json!( { "kinds" : [ 44100 , 44101 ] , "#p" : [ p] , "#t" : [ "system" ] } ) ) ,
190- filter ( serde_json:: json!( { "#t" : [ "other" , "system" ] } ) ) ,
215+ let all = [ "stream" , "forum" , "dm" , "workflow" , "system" ] ;
216+ // (filter, sees untagged, stream, system, a type added later)
217+ let cases = [
218+ (
219+ serde_json:: json!( { "kinds" : [ 44100 , 44101 ] , "#p" : [ p] } ) ,
220+ [ true , true , false , false ] ,
221+ ) ,
222+ ( serde_json:: json!( { "#p" : [ p] } ) , [ true , true , false , false ] ) ,
223+ ( serde_json:: json!( { } ) , [ true , true , false , false ] ) ,
224+ (
225+ serde_json:: json!( { "#t" : [ "other" ] } ) ,
226+ [ false , false , false , false ] ,
227+ ) ,
228+ (
229+ serde_json:: json!( { "kinds" : [ 44100 , 44101 ] , "#p" : [ p] , "#t" : all} ) ,
230+ [ true , true , true , false ] ,
231+ ) ,
232+ (
233+ serde_json:: json!( { "#t" : [ "system" ] } ) ,
234+ [ false , false , true , false ] ,
235+ ) ,
236+ (
237+ serde_json:: json!( { "#t" : [ "dm" ] } ) ,
238+ [ true , false , false , false ] ,
239+ ) ,
240+ (
241+ serde_json:: json!( { "#t" : [ "future" ] } ) ,
242+ [ false , false , false , true ] ,
243+ ) ,
191244 ] ;
192- for kind in SYSTEM_NOTICE_KINDS {
193- let system = notice ( kind, true ) ;
194- assert ! ( is_system_notice( & system. event) ) ;
195- for f in & old {
196- assert ! ( !filters_match( std:: slice:: from_ref( f) , & system) , "{f:?}" ) ;
197- }
198- for f in & asks {
199- assert ! ( filters_match( std:: slice:: from_ref( f) , & system) , "{f:?}" ) ;
245+ for kind in MEMBERSHIP_NOTICE_KINDS {
246+ let notices = [
247+ notice ( kind, None ) ,
248+ notice ( kind, Some ( "stream" ) ) ,
249+ notice ( kind, Some ( "system" ) ) ,
250+ notice ( kind, Some ( "future" ) ) ,
251+ ] ;
252+ for ( json, expected) in & cases {
253+ let f = filter ( json. clone ( ) ) ;
254+ for ( ev, expected) in notices. iter ( ) . zip ( expected) {
255+ assert_eq ! (
256+ filters_match( std:: slice:: from_ref( & f) , ev) ,
257+ * expected,
258+ "kind {kind}, filter {json}, notice {:?}" ,
259+ ev. event. tags
260+ ) ;
261+ }
200262 }
201- // An ordinary notice is unchanged: old filters see it, and the
202- // asking filter's `#t` does not match it.
203- let ordinary = notice ( kind, false ) ;
204- assert ! ( !is_system_notice( & ordinary. event) ) ;
205- assert ! ( filters_match( & old[ ..1 ] , & ordinary) ) ;
206- assert ! ( !filters_match( & asks[ ..1 ] , & ordinary) ) ;
207263 }
208- // The tag hides nothing on other kinds.
209- let other = notice ( 44200 , true ) ;
210- assert ! ( !is_system_notice( & other. event) ) ;
211- assert ! ( filters_match( & old[ 2 ..3 ] , & other) ) ;
264+ // Other kinds keep the plain `#t` match.
265+ let other = notice ( 44200 , Some ( "system" ) ) ;
266+ assert ! ( filters_match( & [ filter( serde_json:: json!( { } ) ) ] , & other) ) ;
267+ assert ! ( filters_match(
268+ & [ filter( serde_json:: json!( { "#t" : [ "system" ] } ) ) ] ,
269+ & other
270+ ) ) ;
271+ assert ! ( !filters_match(
272+ & [ filter( serde_json:: json!( { "#t" : [ "stream" ] } ) ) ] ,
273+ & other
274+ ) ) ;
212275 }
213276
214277 #[ test]
0 commit comments