Skip to content
29 changes: 26 additions & 3 deletions NOSTR.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@ PGPASSWORD=buzz_dev psql -h localhost -U buzz -d buzz -c \
| **Group creation (kind:9007)** | ✅ | NIP-29; include an `h` tag with the new channel id and a `name` tag, optional `visibility`, `channel_type`, label `t` and `unlisted` tags. The relay refuses a 9007 without `h`. It refuses a listed channel (open, not unlisted, stream or forum) whose name another listed channel already has, ignoring case and including archived ones: `invalid: an open channel named "<name>" already exists`. |
| **Add user (kind:9000)** | ✅ | Open: any user, subject to target's `channel_add_policy` (`owner_only`/`nobody` can block). Private: owner/admin only. Self-add bypasses agent policy but not private-channel auth. A guest cannot add anyone. A new member with no role tag gets the channel's `join_role`; a non-member adding themselves may ask for `join_role` or a lower role. One event may name up to 100 people: one `p` tag each, with an optional role as the `p` tag's third element (NIP-29), falling back to the event's `role` tag. Each person is checked and added on their own. When the event names several people, the OK message is `members: ` followed by JSON listing the non-empty `added`, `updated` (role changed), `already_member` and `rejected` (`[{"reason", "pubkeys"}]`) results. The event is refused only when it is malformed or nobody it names may be added; then one person keeps the single-add error, and several get `invalid: members: {…}`. The event and its membership changes are stored together, so an `error:` rejection stores nothing and the same event can be resent; a resent stored event returns `duplicate:` without results. Re-adding someone at their current role posts no second `member_joined` notice. |
| **Remove user (kind:9001)** | ✅ | Self-remove allowed (with last-owner guard). Removing others: owner/admin, or an active member removing their own agent. In a DM, any person in it may remove any agent; nobody may remove a person. Only a channel owner may remove another channel owner. |
| **Edit group metadata (kind:9002)** | ✅ | `name`/`about`/label `t`/`visibility`/`ttl`/`archived`/`join_role`/`unlisted` tags: owner/admin. `topic`/`purpose` tags: any member except a guest. `auto_join` tag: relay owner/admin (see [Default channels](#default-channels)). A relay operator may also send a 9002 that changes only `name` on a listed channel, archived ones included, whatever its channel role, so that duplicate names can be repaired; the channel stays archived. The relay refuses a `name`, `visibility` or `unlisted` change that leaves a listed channel with a name another listed channel already has: `invalid: an open channel named "<name>" already exists`. The relay refuses a private-to-open `visibility` change while the channel has, or once had, a member who is neither the sender nor an agent the sender owns (NIP-OA); removed members count, because their posts stay in its history: `invalid: a private channel with other members cannot be made open; create a new open channel instead`. Buzz applies 9002 as a partial update: an absent tag leaves that setting unchanged. |
| **Edit group metadata (kind:9002)** | ✅ | `name`/`about`/label `t`/`visibility`/`ttl`/`archived`/`join_role`/`unlisted` tags: owner/admin. `topic`/`purpose` tags: any member except a guest. `auto_join` tag: relay owner/admin (see [Default channels](#default-channels)). `official` tag: community owner/admin/maintainer (see [Official channels](#official-channels)). A relay operator may also send a 9002 that changes only `name` on a listed channel, archived ones included, whatever its channel role, so that duplicate names can be repaired; the channel stays archived. The relay refuses a `name`, `visibility` or `unlisted` change that leaves a listed channel with a name another listed channel already has: `invalid: an open channel named "<name>" already exists`. The relay refuses a private-to-open `visibility` change while the channel has, or once had, a member who is neither the sender nor an agent the sender owns (NIP-OA); removed members count, because their posts stay in its history: `invalid: a private channel with other members cannot be made open; create a new open channel instead`. Buzz applies 9002 as a partial update: an absent tag leaves that setting unchanged. |
| **Admin delete event (kind:9005)** | ✅ | Event author can always delete own. Otherwise owner/admin required. Target must be in same channel. |
| **Group deletion (kind:9008)** | ✅ | Owner only. |
| **Leave group (kind:9022)** | ✅ | Any member. Last-owner guard prevents orphaned groups. |
| **Group metadata (kind:39000)** | ✅ | Relay-signed; always `d`, `name`, `t`, `P`, `created_at`, `join_role`; `public` or `private`; `restricted` and `closed` per the access flags below; `unlisted` when the channel is unlisted; `about` only if description non-empty; `hidden` for DM channels and private system channels; `auto_join` while the channel is a default channel |
| **Group metadata (kind:39000)** | ✅ | Relay-signed; always `d`, `name`, `t`, `P`, `created_at`, `join_role`; `public` or `private`; `restricted` and `closed` per the access flags below; `unlisted` when the channel is unlisted; `about` only if description non-empty; `hidden` for DM channels and private system channels; `auto_join` while the channel is a default channel; NIP-32 labels `["L", "buzz.community"]` and `["l", "official", "buzz.community"]` while the channel is official, open, listed and unarchived (see [Official channels](#official-channels)) |
| **Unlisted channels** | ✅ | kind:9002 `["unlisted", "true"\|"false"]`, owner/admin, or at creation on kind:9007. An unlisted open channel is left out of every read that does not name it, for non-members: channel lists, kind:39000 without `#d`, messages, COUNT, NIP-50 search and artifact queries. Naming it by `#h` (or `#d` on 39000–39003) reads it, and kind:9021 joins it, as for any open channel. See [Unlisted channels](#unlisted-channels). |
| **Group admins (kind:39001)** | ✅ | Relay-signed; `d`, channel type `t` and creator `P` tags + `p` tags with roles (`owner`, `admin`) |
| **Group members (kind:39002)** | ✅ | Relay-signed; `d`, channel type `t` and creator `P` tags + `p` tags for all members |
Expand Down Expand Up @@ -277,6 +277,29 @@ A relay older than this feature that rebuilds the roster leaves the `grant`
tags out until the next change. The relay records grant changes in its
membership table and publishes them in the roster.

### Official channels

Owners, admins and maintainers mark an open stream or
forum channel official with kind:9002 `["official", "true"|"false"]`. Relays
advertise this capability as `buzz-official-channels` in NIP-11
`supported_extensions`. Like
`auto_join`, it takes community authority, not a channel role, and the actor
need not be a channel member; other tags in the same event still need their
own authority. Just before commit, the edit takes the kind:13534 membership-list
lock and reads the committed role and grants. If a roster change takes the
lock first, the edit sees the change once it commits. If the edit takes the
lock first, the roster change waits for the edit to finish. `true` needs a
channel the event leaves open, listed and unarchived; either value needs it
open. kind:39000 carries the NIP-32 labels
`["L", "buzz.community"]` and `["l", "official", "buzz.community"]` only while
the channel is official, open, listed and unarchived. Designation joins nobody
and changes no access, posting rights or notifications; each change is
announced by one kind:40099 `official_changed` system message. Clients find
every official channel a member can read, joined or not, with
`{"kinds":[39000], "#l":["official"]}`, which the relay matches in SQL before
`limit`. Treat a complete result as the whole set and a result that reaches
`limit` as partial.

### Group Discovery

The relay emits NIP-29 group state events when channels are created, updated, or membership changes.
Expand All @@ -286,7 +309,7 @@ All discovery events include a `d` tag set to the channel UUID (NIP-29 addressab

| Kind | Tags | Content |
|------|------|---------|
| **39000** | `d=<uuid>`, `name`, `t=<channel_type>`, `P=<creator>`, `created_at=<unix seconds>`, `join_role` (always); `public` or `private`; `restricted` (private channels, and `join_role=guest`); `closed` (private channels only); `unlisted` (unlisted channels); `about` (if description non-empty); `hidden` (DM channels and private system channels); `auto_join` (open, unarchived channels with the setting on, see [Default channels](#default-channels)) | Group metadata. The access flags follow the channel settings (see [Announce channels](#announce-channels)). |
| **39000** | `d=<uuid>`, `name`, `t=<channel_type>`, `P=<creator>`, `created_at=<unix seconds>`, `join_role` (always); `public` or `private`; `restricted` (private channels, and `join_role=guest`); `closed` (private channels only); `unlisted` (unlisted channels); `about` (if description non-empty); `hidden` (DM channels and private system channels); `auto_join` (open, unarchived channels with the setting on, see [Default channels](#default-channels)); `L=buzz.community` and `l=official` (official channels, see [Community maintainers](#community-maintainers)) | Group metadata. The access flags follow the channel settings (see [Announce channels](#announce-channels)). |
| **39001** | `d=<uuid>`, `t=<channel_type>`, `P=<creator>`, `p` tags with role label (`owner`, `admin`) | Admin list |
| **39002** | `d=<uuid>`, `t=<channel_type>`, `P=<creator>`, `p` tags for all members | Member list |

Expand Down
83 changes: 71 additions & 12 deletions crates/buzz-cli/src/commands/channels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -701,14 +701,7 @@ const UNLISTED_EXTENSION: &str = "buzz-unlisted-channels";
/// ignores `unlisted`, so an open channel would end up listed. Call it before
/// anything is written.
async fn require_unlisted_support(client: &BuzzClient) -> Result<(), CliError> {
let raw = client.get_public("/info").await?;
let info: serde_json::Value = serde_json::from_str(&raw)
.map_err(|e| CliError::Other(format!("invalid NIP-11 response: {e}")))?;
let supported = info
.get("supported_extensions")
.and_then(|v| v.as_array())
.is_some_and(|exts| exts.iter().any(|e| e.as_str() == Some(UNLISTED_EXTENSION)));
if supported {
if relay_supports_extension(client, UNLISTED_EXTENSION).await? {
Ok(())
} else {
Err(CliError::Other(format!(
Expand All @@ -717,6 +710,18 @@ async fn require_unlisted_support(client: &BuzzClient) -> Result<(), CliError> {
}
}

const OFFICIAL_EXTENSION: &str = "buzz-official-channels";

async fn relay_supports_extension(client: &BuzzClient, extension: &str) -> Result<bool, CliError> {
let raw = client.get_public("/info").await?;
let info: serde_json::Value = serde_json::from_str(&raw)
.map_err(|e| CliError::Other(format!("invalid NIP-11 response: {e}")))?;
Ok(info
.get("supported_extensions")
.and_then(|v| v.as_array())
.is_some_and(|exts| exts.iter().any(|e| e.as_str() == Some(extension))))
}

pub async fn cmd_create_channel(
client: &BuzzClient,
name: &str,
Expand Down Expand Up @@ -1671,6 +1676,7 @@ fn validate_ttl_seconds(secs: i64) -> Result<i32, CliError> {
.map_err(|_| CliError::Usage(format!("--ttl is too large (max {} seconds)", i32::MAX)))
}

#[allow(clippy::too_many_arguments)]
fn validate_update_channel_fields(
name: Option<&str>,
description: Option<&str>,
Expand All @@ -1679,6 +1685,7 @@ fn validate_update_channel_fields(
join_role: Option<buzz_sdk::ChannelJoinRole>,
unlisted: Option<bool>,
auto_join: Option<bool>,
official: Option<bool>,
) -> Result<(), CliError> {
if name.is_none()
&& description.is_none()
Expand All @@ -1687,9 +1694,10 @@ fn validate_update_channel_fields(
&& join_role.is_none()
&& unlisted.is_none()
&& auto_join.is_none()
&& official.is_none()
{
return Err(CliError::Usage(
"at least one field required (--name, --description, --visibility, --ttl, --no-ttl, --join-role, --unlisted, --auto-join)"
"at least one field required (--name, --description, --visibility, --ttl, --no-ttl, --join-role, --unlisted, --auto-join, --official)"
.into(),
));
}
Expand All @@ -1708,6 +1716,7 @@ pub async fn cmd_update_channel(
join_role: Option<buzz_sdk::ChannelJoinRole>,
unlisted: Option<bool>,
auto_join: Option<bool>,
official: Option<bool>,
) -> Result<(), CliError> {
// Outer Option: None leaves TTL unchanged. Inner: Some(secs) sets it,
// None (from --no-ttl) clears it, making the channel permanent.
Expand All @@ -1725,6 +1734,7 @@ pub async fn cmd_update_channel(
join_role,
unlisted,
auto_join,
official,
)?;
let channel_uuid = parse_uuid(channel_id)?;
// `--unlisted false` needs no check: a relay without the extension lists
Expand All @@ -1733,7 +1743,13 @@ pub async fn cmd_update_channel(
require_unlisted_support(client).await?;
}

let builder = buzz_sdk::build_update_channel(
if official.is_some() && !relay_supports_extension(client, OFFICIAL_EXTENSION).await? {
return Err(CliError::Other(format!(
"this relay does not support Official channels (missing \"{OFFICIAL_EXTENSION}\" in supported_extensions)"
)));
}

let builder = buzz_sdk::build_update_channel_with_official(
channel_uuid,
name,
description,
Expand All @@ -1742,6 +1758,7 @@ pub async fn cmd_update_channel(
join_role,
unlisted,
auto_join,
official,
)
.map_err(|e| CliError::Other(format!("build_update_channel failed: {e}")))?;

Expand Down Expand Up @@ -2295,6 +2312,7 @@ pub async fn dispatch(
join_role,
unlisted,
auto_join,
official,
} => {
let visibility = visibility.as_ref().map(|v| v.to_string());
cmd_update_channel(
Expand All @@ -2308,6 +2326,7 @@ pub async fn dispatch(
join_role.map(Into::into),
unlisted,
auto_join,
official,
)
.await
}
Expand Down Expand Up @@ -2655,7 +2674,7 @@ mod tests {

#[test]
fn update_channel_fields_rejects_empty_update() {
let result = validate_update_channel_fields(None, None, None, None, None, None, None);
let result = validate_update_channel_fields(None, None, None, None, None, None, None, None);
assert!(matches!(result, Err(CliError::Usage(_))));
let msg = result.unwrap_err().to_string();
assert!(msg.contains("at least one field required"));
Expand All @@ -2665,7 +2684,7 @@ mod tests {
#[test]
fn update_channel_fields_accepts_visibility_only_update() {
let result =
validate_update_channel_fields(None, None, Some("open"), None, None, None, None);
validate_update_channel_fields(None, None, Some("open"), None, None, None, None, None);
assert!(result.is_ok(), "visibility-only update should be accepted");
}

Expand Down Expand Up @@ -5357,6 +5376,7 @@ mod unlisted_support_tests {
None,
Some(true),
None,
None,
)
.await
.unwrap_err();
Expand Down Expand Up @@ -5384,6 +5404,7 @@ mod unlisted_support_tests {
None,
Some(true),
None,
None,
)
.await
.unwrap();
Expand All @@ -5393,6 +5414,43 @@ mod unlisted_support_tests {
assert!(submitted.iter().all(has_unlisted_true));
}

#[tokio::test]
async fn official_updates_require_relay_support() {
for (extensions, supported) in [(r#"[]"#, false), (r#"["buzz-official-channels"]"#, true)] {
let (url, submitted) = relay(extensions).await;
let result = cmd_update_channel(
&client(&url),
CHANNEL,
None,
None,
None,
None,
false,
None,
None,
None,
Some(true),
)
.await;
let submitted = submitted.lock().unwrap();
if supported {
result.expect("official update");
assert_eq!(submitted.len(), 1);
assert!(submitted[0]["tags"]
.as_array()
.unwrap()
.iter()
.any(|tag| tag[0] == "official" && tag[1] == "true"));
} else {
assert!(result
.unwrap_err()
.to_string()
.contains("buzz-official-channels"));
assert!(submitted.is_empty());
}
}
}

#[tokio::test]
async fn listed_writes_skip_the_check() {
// The relay advertises no extensions, so a check would fail the command.
Expand All @@ -5413,6 +5471,7 @@ mod unlisted_support_tests {
None,
Some(false),
None,
None,
)
.await
.unwrap();
Expand Down
4 changes: 4 additions & 0 deletions crates/buzz-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,10 @@ pub enum ChannelsCmd {
/// on an open, unarchived stream channel.
#[arg(long, value_name = "true|false")]
auto_join: Option<bool>,
/// Designate or clear an Official channel. Requires an open, listed
/// stream or forum and community owner, admin or maintainer authority.
#[arg(long, value_name = "true|false")]
official: Option<bool>,
},
/// Set the channel topic
Topic {
Expand Down
4 changes: 3 additions & 1 deletion crates/buzz-db/src/runtime/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,9 @@ mod postgres_tests {
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
migrations.sort_by_key(|migration| migration.version);

assert_eq!(migrations.len(), 114);
assert_eq!(migrations.len(), 115);
assert_eq!(migrations[114].version, 115);
assert!(strip_sql_comments(migrations[114].sql.as_str()).contains("ADD COLUMN official"));
assert_eq!(migrations[113].version, 114);
assert!(strip_sql_comments(migrations[113].sql.as_str()).contains("ADD COLUMN grants"));
assert_eq!(migrations[112].version, 113);
Expand Down
Loading
Loading