Skip to content

Commit cf91a19

Browse files
ngoodger-blockBuildy
andcommitted
fix(relay): advertise and filter Official channel discovery
Co-authored-by: Buildy <9c2fd8f72fad905d31cdf563c3920011399bd334523d6b3c84c0dc124bcfb380@sprout-oss.stage.blox.sqprod.co> Signed-off-by: Nikolaj Goodger <ngoodger@squareup.com>
1 parent 4bb7fd0 commit cf91a19

7 files changed

Lines changed: 133 additions & 16 deletions

File tree

‎NOSTR.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,9 @@ tags out until the next change; the relay enforces grants from its own table
279279
either way.
280280

281281
**Official channels.** Owners, admins and maintainers mark an open stream or
282-
forum channel official with kind:9002 `["official", "true"|"false"]`. Like
282+
forum channel official with kind:9002 `["official", "true"|"false"]`. Relays
283+
advertise this capability as `buzz-official-channels` in NIP-11
284+
`supported_extensions`. Like
283285
`auto_join`, it takes community authority, not a channel role, and the actor
284286
need not be a channel member; other tags in the same event still need their
285287
own authority. Just before commit, the edit takes the kind:13534 membership-list

‎crates/buzz-cli/src/commands/channels.rs‎

Lines changed: 71 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -701,14 +701,7 @@ const UNLISTED_EXTENSION: &str = "buzz-unlisted-channels";
701701
/// ignores `unlisted`, so an open channel would end up listed. Call it before
702702
/// anything is written.
703703
async fn require_unlisted_support(client: &BuzzClient) -> Result<(), CliError> {
704-
let raw = client.get_public("/info").await?;
705-
let info: serde_json::Value = serde_json::from_str(&raw)
706-
.map_err(|e| CliError::Other(format!("invalid NIP-11 response: {e}")))?;
707-
let supported = info
708-
.get("supported_extensions")
709-
.and_then(|v| v.as_array())
710-
.is_some_and(|exts| exts.iter().any(|e| e.as_str() == Some(UNLISTED_EXTENSION)));
711-
if supported {
704+
if relay_supports_extension(client, UNLISTED_EXTENSION).await? {
712705
Ok(())
713706
} else {
714707
Err(CliError::Other(format!(
@@ -717,6 +710,18 @@ async fn require_unlisted_support(client: &BuzzClient) -> Result<(), CliError> {
717710
}
718711
}
719712

713+
const OFFICIAL_EXTENSION: &str = "buzz-official-channels";
714+
715+
async fn relay_supports_extension(client: &BuzzClient, extension: &str) -> Result<bool, CliError> {
716+
let raw = client.get_public("/info").await?;
717+
let info: serde_json::Value = serde_json::from_str(&raw)
718+
.map_err(|e| CliError::Other(format!("invalid NIP-11 response: {e}")))?;
719+
Ok(info
720+
.get("supported_extensions")
721+
.and_then(|v| v.as_array())
722+
.is_some_and(|exts| exts.iter().any(|e| e.as_str() == Some(extension))))
723+
}
724+
720725
pub async fn cmd_create_channel(
721726
client: &BuzzClient,
722727
name: &str,
@@ -1671,6 +1676,7 @@ fn validate_ttl_seconds(secs: i64) -> Result<i32, CliError> {
16711676
.map_err(|_| CliError::Usage(format!("--ttl is too large (max {} seconds)", i32::MAX)))
16721677
}
16731678

1679+
#[allow(clippy::too_many_arguments)]
16741680
fn validate_update_channel_fields(
16751681
name: Option<&str>,
16761682
description: Option<&str>,
@@ -1679,6 +1685,7 @@ fn validate_update_channel_fields(
16791685
join_role: Option<buzz_sdk::ChannelJoinRole>,
16801686
unlisted: Option<bool>,
16811687
auto_join: Option<bool>,
1688+
official: Option<bool>,
16821689
) -> Result<(), CliError> {
16831690
if name.is_none()
16841691
&& description.is_none()
@@ -1687,9 +1694,10 @@ fn validate_update_channel_fields(
16871694
&& join_role.is_none()
16881695
&& unlisted.is_none()
16891696
&& auto_join.is_none()
1697+
&& official.is_none()
16901698
{
16911699
return Err(CliError::Usage(
1692-
"at least one field required (--name, --description, --visibility, --ttl, --no-ttl, --join-role, --unlisted, --auto-join)"
1700+
"at least one field required (--name, --description, --visibility, --ttl, --no-ttl, --join-role, --unlisted, --auto-join, --official)"
16931701
.into(),
16941702
));
16951703
}
@@ -1708,6 +1716,7 @@ pub async fn cmd_update_channel(
17081716
join_role: Option<buzz_sdk::ChannelJoinRole>,
17091717
unlisted: Option<bool>,
17101718
auto_join: Option<bool>,
1719+
official: Option<bool>,
17111720
) -> Result<(), CliError> {
17121721
// Outer Option: None leaves TTL unchanged. Inner: Some(secs) sets it,
17131722
// None (from --no-ttl) clears it, making the channel permanent.
@@ -1725,6 +1734,7 @@ pub async fn cmd_update_channel(
17251734
join_role,
17261735
unlisted,
17271736
auto_join,
1737+
official,
17281738
)?;
17291739
let channel_uuid = parse_uuid(channel_id)?;
17301740
// `--unlisted false` needs no check: a relay without the extension lists
@@ -1733,7 +1743,13 @@ pub async fn cmd_update_channel(
17331743
require_unlisted_support(client).await?;
17341744
}
17351745

1736-
let builder = buzz_sdk::build_update_channel(
1746+
if official.is_some() && !relay_supports_extension(client, OFFICIAL_EXTENSION).await? {
1747+
return Err(CliError::Other(format!(
1748+
"this relay does not support Official channels (missing \"{OFFICIAL_EXTENSION}\" in supported_extensions)"
1749+
)));
1750+
}
1751+
1752+
let builder = buzz_sdk::build_update_channel_with_official(
17371753
channel_uuid,
17381754
name,
17391755
description,
@@ -1742,6 +1758,7 @@ pub async fn cmd_update_channel(
17421758
join_role,
17431759
unlisted,
17441760
auto_join,
1761+
official,
17451762
)
17461763
.map_err(|e| CliError::Other(format!("build_update_channel failed: {e}")))?;
17471764

@@ -2295,6 +2312,7 @@ pub async fn dispatch(
22952312
join_role,
22962313
unlisted,
22972314
auto_join,
2315+
official,
22982316
} => {
22992317
let visibility = visibility.as_ref().map(|v| v.to_string());
23002318
cmd_update_channel(
@@ -2308,6 +2326,7 @@ pub async fn dispatch(
23082326
join_role.map(Into::into),
23092327
unlisted,
23102328
auto_join,
2329+
official,
23112330
)
23122331
.await
23132332
}
@@ -2655,7 +2674,7 @@ mod tests {
26552674

26562675
#[test]
26572676
fn update_channel_fields_rejects_empty_update() {
2658-
let result = validate_update_channel_fields(None, None, None, None, None, None, None);
2677+
let result = validate_update_channel_fields(None, None, None, None, None, None, None, None);
26592678
assert!(matches!(result, Err(CliError::Usage(_))));
26602679
let msg = result.unwrap_err().to_string();
26612680
assert!(msg.contains("at least one field required"));
@@ -2665,7 +2684,7 @@ mod tests {
26652684
#[test]
26662685
fn update_channel_fields_accepts_visibility_only_update() {
26672686
let result =
2668-
validate_update_channel_fields(None, None, Some("open"), None, None, None, None);
2687+
validate_update_channel_fields(None, None, Some("open"), None, None, None, None, None);
26692688
assert!(result.is_ok(), "visibility-only update should be accepted");
26702689
}
26712690

@@ -5357,6 +5376,7 @@ mod unlisted_support_tests {
53575376
None,
53585377
Some(true),
53595378
None,
5379+
None,
53605380
)
53615381
.await
53625382
.unwrap_err();
@@ -5384,6 +5404,7 @@ mod unlisted_support_tests {
53845404
None,
53855405
Some(true),
53865406
None,
5407+
None,
53875408
)
53885409
.await
53895410
.unwrap();
@@ -5393,6 +5414,43 @@ mod unlisted_support_tests {
53935414
assert!(submitted.iter().all(has_unlisted_true));
53945415
}
53955416

5417+
#[tokio::test]
5418+
async fn official_updates_require_relay_support() {
5419+
for (extensions, supported) in [(r#"[]"#, false), (r#"["buzz-official-channels"]"#, true)] {
5420+
let (url, submitted) = relay(extensions).await;
5421+
let result = cmd_update_channel(
5422+
&client(&url),
5423+
CHANNEL,
5424+
None,
5425+
None,
5426+
None,
5427+
None,
5428+
false,
5429+
None,
5430+
None,
5431+
None,
5432+
Some(true),
5433+
)
5434+
.await;
5435+
let submitted = submitted.lock().unwrap();
5436+
if supported {
5437+
result.expect("official update");
5438+
assert_eq!(submitted.len(), 1);
5439+
assert!(submitted[0]["tags"]
5440+
.as_array()
5441+
.unwrap()
5442+
.iter()
5443+
.any(|tag| tag[0] == "official" && tag[1] == "true"));
5444+
} else {
5445+
assert!(result
5446+
.unwrap_err()
5447+
.to_string()
5448+
.contains("buzz-official-channels"));
5449+
assert!(submitted.is_empty());
5450+
}
5451+
}
5452+
}
5453+
53965454
#[tokio::test]
53975455
async fn listed_writes_skip_the_check() {
53985456
// The relay advertises no extensions, so a check would fail the command.
@@ -5413,6 +5471,7 @@ mod unlisted_support_tests {
54135471
None,
54145472
Some(false),
54155473
None,
5474+
None,
54165475
)
54175476
.await
54185477
.unwrap();

‎crates/buzz-cli/src/lib.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,6 +1009,10 @@ pub enum ChannelsCmd {
10091009
/// on an open, unarchived stream channel.
10101010
#[arg(long, value_name = "true|false")]
10111011
auto_join: Option<bool>,
1012+
/// Designate or clear an Official channel. Requires an open, listed
1013+
/// stream or forum and community owner, admin or maintainer authority.
1014+
#[arg(long, value_name = "true|false")]
1015+
official: Option<bool>,
10121016
},
10131017
/// Set the channel topic
10141018
Topic {

‎crates/buzz-relay/src/handlers/community_official_channels_postgres_tests.rs‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,12 @@ async fn maintainers_designate_official_channels_members_can_find() {
132132
req_event_ids(&state, &a, &newcomer, official_read.clone()).await,
133133
expected
134134
);
135+
let namespace_read =
136+
serde_json::json!({"kinds": [39000], "#L": ["buzz.community"], "limit": 1});
137+
assert_eq!(
138+
req_event_ids(&state, &a, &newcomer, namespace_read).await,
139+
expected
140+
);
135141
assert!(req_event_ids(&state, &b, &newcomer, official_read.clone())
136142
.await
137143
.is_empty());

‎crates/buzz-relay/src/handlers/req.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1499,8 +1499,8 @@ fn filter_to_query_params(
14991499
}
15001500
}
15011501

1502-
/// `#t` (channel type and labels), `#P` (creator) and `#l` (community labels
1503-
/// such as `official`) values to match in SQL
1502+
/// `#t` (channel type and labels), `#P` (creator), `#L` (label namespace),
1503+
/// and `#l` (labels such as `official`) values to match in SQL
15041504
/// before `LIMIT`, when every filter kind is a relay-signed group-state kind
15051505
/// (39000–39003). Without this, a page of the newest group-state events is
15061506
/// read first and the tag match runs after it, so a selective
@@ -1517,6 +1517,7 @@ fn group_state_tag_values(filter: &Filter) -> Vec<(String, Vec<String>)> {
15171517
[
15181518
nostr::SingleLetterTag::lowercase(nostr::Alphabet::T),
15191519
nostr::SingleLetterTag::uppercase(nostr::Alphabet::P),
1520+
nostr::SingleLetterTag::uppercase(nostr::Alphabet::L),
15201521
nostr::SingleLetterTag::lowercase(nostr::Alphabet::L),
15211522
]
15221523
.into_iter()

‎crates/buzz-relay/src/nip11.rs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ impl RelayInfo {
285285
"buzz-solo-member-role".to_string(),
286286
// kind:9034 community grants (NOSTR.md, Community maintainers).
287287
"buzz-community-maintainers".to_string(),
288+
"buzz-official-channels".to_string(),
288289
];
289290
let gif = gif_provider.map(|provider| {
290291
supported_extensions.push("buzz-gif".to_string());
@@ -893,6 +894,7 @@ mod tests {
893894
assert!(extensions.contains(&"buzz-search-sort".to_string()));
894895
assert!(extensions.contains(&"buzz-search-from".to_string()));
895896
assert!(extensions.contains(&"buzz-unlisted-channels".to_string()));
897+
assert!(extensions.contains(&"buzz-official-channels".to_string()));
896898
assert!(extensions.contains(&"buzz-solo-member-role".to_string()));
897899
}
898900

‎crates/buzz-sdk/src/builders.rs‎

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,24 @@ pub fn build_update_channel(
953953
join_role: Option<buzz_core::channel::ChannelJoinRole>,
954954
unlisted: Option<bool>,
955955
auto_join: Option<bool>,
956+
) -> Result<EventBuilder, SdkError> {
957+
build_update_channel_with_official(
958+
channel_id, name, about, visibility, ttl, join_role, unlisted, auto_join, None,
959+
)
960+
}
961+
962+
/// Build a kind:9002 metadata update with an optional Official designation.
963+
#[allow(clippy::too_many_arguments)]
964+
pub fn build_update_channel_with_official(
965+
channel_id: Uuid,
966+
name: Option<&str>,
967+
about: Option<&str>,
968+
visibility: Option<&str>,
969+
ttl: Option<Option<i32>>,
970+
join_role: Option<buzz_core::channel::ChannelJoinRole>,
971+
unlisted: Option<bool>,
972+
auto_join: Option<bool>,
973+
official: Option<bool>,
956974
) -> Result<EventBuilder, SdkError> {
957975
if name.is_none()
958976
&& about.is_none()
@@ -961,9 +979,10 @@ pub fn build_update_channel(
961979
&& join_role.is_none()
962980
&& unlisted.is_none()
963981
&& auto_join.is_none()
982+
&& official.is_none()
964983
{
965984
return Err(SdkError::InvalidTag(
966-
"at least one of name, about, visibility, ttl, join_role, unlisted, or auto_join must be provided"
985+
"at least one of name, about, visibility, ttl, join_role, unlisted, auto_join, or official must be provided"
967986
.into(),
968987
));
969988
}
@@ -1013,6 +1032,9 @@ pub fn build_update_channel(
10131032
if auto_join { "true" } else { "false" },
10141033
])?);
10151034
}
1035+
if let Some(official) = official {
1036+
tags.push(tag(&["official", if official { "true" } else { "false" }])?);
1037+
}
10161038
Ok(EventBuilder::new(Kind::Custom(9002), "").tags(tags))
10171039
}
10181040

@@ -4279,6 +4301,27 @@ mod tests {
42794301
.any(|t| t.kind().to_string() == "auto_join"));
42804302
}
42814303

4304+
#[test]
4305+
fn update_channel_can_designate_and_clear_official() {
4306+
for (enabled, value) in [(true, "true"), (false, "false")] {
4307+
let event = sign(
4308+
build_update_channel_with_official(
4309+
uuid(),
4310+
None,
4311+
None,
4312+
None,
4313+
None,
4314+
None,
4315+
None,
4316+
None,
4317+
Some(enabled),
4318+
)
4319+
.unwrap(),
4320+
);
4321+
assert!(has_tag(&event, "official", value));
4322+
}
4323+
}
4324+
42824325
#[test]
42834326
fn update_channel_no_fields_rejected() {
42844327
let cid = uuid();

0 commit comments

Comments
 (0)