Skip to content

Commit f24f8e7

Browse files
loganjLarry
andcommitted
feat(relay): tag every membership notice with its channel type
Every kind:44100/44101 notice now carries `["t", <channel_type>]`. A filter sees a notice only if it knows the type: a filter with `#t` sees the types it names, and a filter without `#t` sees only the frozen legacy types (stream, forum, dm, workflow). A type added later is hidden from old clients by default, so a read path that is not updated fails closed. An untagged notice, stored before this change, counts as a legacy type, so replay across the upgrade stays complete. One filter that names every type reads all notices. The rule lives in buzz-core (`notice_visible`) and in one SQL predicate (`push_notice_visibility`), which defaults to legacy types. The relay reads the channel type in one place, `emit_membership_notification`, and sends no notice when the channel row is missing. Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Signed-off-by: Logan Johnson <[email protected]>
1 parent 66a12c1 commit f24f8e7

8 files changed

Lines changed: 372 additions & 156 deletions

File tree

‎NOSTR.md‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,10 @@ out unless they ask for it:
152152
combine with OR, so `#t:["system", <label>]` does not mean "a system
153153
channel with this label".
154154

155-
Push notifications skip system channels. Their kind:44100/44101
156-
membership notifications carry `["t","system"]`, and the relay returns those
157-
only to a filter with `#t:["system"]` (see NIP-CL).
155+
Push notifications skip system channels. Every kind:44100/44101 membership
156+
notification carries its channel type as `t`, and a filter without `#t` sees
157+
only the legacy types, so system channel notices reach only a filter with
158+
`#t:["system"]` (see NIP-CL).
158159

159160
Events are stored **channel-scoped** so access control applies — private channel member lists are
160161
only visible to members. Discover groups via historical REQ:
@@ -180,8 +181,10 @@ The relay emits relay-signed notifications when members are added or removed:
180181
| **44100** | Member added | `p` = target pubkey, `h` = channel UUID | Community-global |
181182
| **44101** | Member removed | `p` = target pubkey, `h` = channel UUID | Community-global |
182183

183-
A notice about a system channel also has `t` = `system`. Only a filter with
184-
`#t:["system"]` returns it (NIP-CL).
184+
Each notice also has `t` = the channel type (`stream`, `forum`, `dm`,
185+
`workflow`, `system`). A filter with `#t` gets only the types it names. A
186+
filter without `#t` gets only `stream`, `forum`, `dm` and `workflow`, and so
187+
does an untagged notice stored before the tag existed (NIP-CL).
185188

186189
Stored community-globally (`channel_id = None` inside the connected community) so agents and clients can subscribe without knowing channel
187190
UUIDs in advance. Client-submitted kind:44100/44101 events are rejected — only the relay keypair

‎crates/buzz-core/src/filter.rs‎

Lines changed: 119 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -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

7198
fn 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]

‎crates/buzz-db/src/store/channel_members.rs‎

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1288,8 +1288,29 @@ pub async fn get_readable_system_channel_ids(
12881288
.collect()
12891289
}
12901290

1291+
/// The type of channel `channel_id`, deleted or not, or `None` when no such
1292+
/// channel exists. Membership notices carry it as their `t` tag (NIP-CL).
1293+
pub async fn channel_type_of(
1294+
pool: &PgPool,
1295+
community_id: CommunityId,
1296+
channel_id: Uuid,
1297+
) -> Result<Option<String>> {
1298+
let mut connection = crate::observability::acquire_writer(
1299+
pool,
1300+
crate::observability::WriterOperation::Authorization,
1301+
)
1302+
.await?;
1303+
Ok(sqlx::query_scalar(
1304+
"SELECT channel_type::text FROM channels WHERE community_id = $1 AND id = $2",
1305+
)
1306+
.bind(community_id.as_uuid())
1307+
.bind(channel_id)
1308+
.fetch_optional(&mut *connection)
1309+
.await?)
1310+
}
1311+
12911312
/// The IDs in `channel_ids` that name system channels, deleted or not.
1292-
/// Push matching and membership notifications use it to skip them.
1313+
/// Push matching uses it to skip them.
12931314
pub async fn system_channel_ids_among(
12941315
pool: &PgPool,
12951316
community_id: CommunityId,
@@ -1954,6 +1975,17 @@ impl Db {
19541975
get_readable_system_channel_ids(&self.pool, community_id, pubkey).await
19551976
}
19561977

1978+
/// The type of channel `channel_id`, deleted or not, or `None` when no
1979+
/// such channel exists.
1980+
#[datastore_span(name = "channel_type_of", system = "postgresql")]
1981+
pub async fn channel_type_of(
1982+
&self,
1983+
community_id: CommunityId,
1984+
channel_id: Uuid,
1985+
) -> Result<Option<String>> {
1986+
channel_type_of(&self.pool, community_id, channel_id).await
1987+
}
1988+
19571989
/// The IDs in `channel_ids` that name system channels, deleted or not.
19581990
#[datastore_span(name = "system_channel_ids_among", system = "postgresql")]
19591991
pub async fn system_channel_ids_among(

0 commit comments

Comments
 (0)