Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 36 additions & 6 deletions NOSTR.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ PGPASSWORD=buzz_dev psql -h localhost -U buzz -d buzz -c \
| **Reactions (kind:7)** | ✅ | Standard NIP-25; channel derived from target event's `#e` tag (client `#h` ignored) |
| **Deletions (kind:5)** | ✅ | Standard NIP-09; self-authored only. `#h` optional, `#e` required |
| **User profiles (kind:0)** | ✅ | NIP-01 metadata; synced to users table (display_name, avatar, about, NIP-05). NIP-05 handles must canonicalize to this relay's domain — off-domain or invalid handles are silently cleared. If a NIP-05 handle collides with another user's (UNIQUE constraint), the handle is skipped but other profile fields (display_name, avatar, about) are still synced. |
| **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`. |
| **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. A stream may start View only with `["visibility", "view-only"]` and optional Viewer grants. 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. 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. |
Expand Down Expand Up @@ -103,27 +103,57 @@ relay to specific external Nostr identities without granting full access.

### Announce channels

Roles have the same permissions in every channel. A channel's settings
decide only who can join and which role a new person gets:
A channel's settings decide who can join, which role a new person gets,
and whether guests may contribute within existing conversations:

- `visibility` (`open` | `private`) decides who can find, join and read.
- `join_role` (`member` | `guest`) is the role a person gets when nobody
picks one: a self-join (kind:9021, or a kind:9000 self-add), or a kind:9000
add with no `role` tag. The default, `member`, keeps today's behavior.

An open channel with `join_role = guest` is an **announce channel**: anyone
can find, read and join it, but people who join are guests and cannot write.
can find, read and join it, but people who join are guests and cannot start
conversations. By default they cannot reply or react either.

What each role can do:

| Action | Guest | Member or bot | Admin / owner |
|--------|-------|---------------|---------------|
| Read, leave | Yes | Yes | Yes |
| Post, reply, edit, delete own, react, artifacts, canvas, forum, pins, typing, workflow messages, huddles | No | Yes | Yes |
| Start conversations, artifacts, canvas, forum posts, pins, typing, workflow messages, huddles | No | Yes | Yes |
| Reply, edit/delete own reply | When `guest_replies = true` | Yes | Yes |
| Add reactions | When `guest_reactions = true` | Yes | Yes |
| Remove own reactions | Yes | Yes | Yes |
| Topic, purpose (kind:9002) | No | Yes | Yes |
| Add people (kind:9000) | No | Yes, as member, guest or bot | Yes; only owners can grant owner |
| Push to the channel's git repositories | No | Yes | Yes |
| Name, about, visibility, TTL, `join_role`, `unlisted` | No | No | Yes |
| Name, about, visibility, TTL, `join_role`, `guest_replies`, `guest_reactions`, `unlisted` | No | No | Yes |

Owners and admins may set `["guest_replies", "true" | "false"]` and
`["guest_reactions", "true" | "false"]` with kind:9002. Both default to false;
omitted tags preserve the setting, and duplicate or malformed values are refused.
Kind:39000 always publishes both flags. Older clients omitting them do not reset
them. These grants apply only to active Guest members of streams and forums;
non-members receive no additional authority.

To create a View only stream, send kind:9007 with exactly one
`["visibility", "view-only"]` tag and optional `guest_replies` and
`guest_reactions` boolean tags in the same event. Omitted grants start as false.
The channel commits as open with `join_role = guest` and the requested grants;
the creator is still its owner. The first self-join therefore yields a Viewer.
Signed kind:39000 reports `public`, `restricted`, `join_role = guest`, and both
boolean flags. The creation-only visibility is rejected by older Buzz relays;
clients must surface the refusal and must never retry with `visibility = open`.

The reply grant covers stream message kinds 9 and 40002, including their threads
in forum channels. Forum-specific comments (45003), votes and new posts retain
their existing writer-only gate. A Guest reply must resolve to an existing message root and parent in the same
channel through NIP-10. Unknown, inaccessible, cross-channel and auxiliary-event
parents are refused. A lone root marker is not a reply. `["broadcast", "1"]`
and separate root copies remain forbidden. Reply edits/deletions check the
original target and its author. Removing one's own reaction remains possible
after additions are disabled; other deletion authority is unchanged. Archive,
moderation, token scope and visibility checks still apply.

A non-member may write only where joining would give a role that can write:
an open channel with `join_role = member`. A refused write gets
Expand Down
6 changes: 5 additions & 1 deletion crates/buzz-db/src/runtime/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -879,7 +879,7 @@ mod postgres_tests {
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
migrations.sort_by_key(|migration| migration.version);

assert_eq!(migrations.len(), 101);
assert_eq!(migrations.len(), 102);
assert_eq!(migrations[100].version, 101);
assert!(strip_sql_comments(migrations[100].sql.as_str())
.contains("DROP CONSTRAINT IF EXISTS push_gateway_delegations_installation_id_fkey"));
Expand Down Expand Up @@ -970,6 +970,10 @@ mod postgres_tests {
"0095 must run {statement}"
);
}
assert_eq!(migrations[101].version, 102);
let guest_grants = migrations[101].sql.as_str();
assert!(guest_grants.contains("ADD COLUMN guest_replies BOOLEAN NOT NULL DEFAULT false"));
assert!(guest_grants.contains("ADD COLUMN guest_reactions BOOLEAN NOT NULL DEFAULT false"));
assert!(migrations[70]
.sql
.as_str()
Expand Down
84 changes: 76 additions & 8 deletions crates/buzz-db/src/store/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ pub struct ChannelRecord {
/// The role a new person gets (`"member"` or `"guest"`). Parse with
/// [`buzz_core::channel::ChannelJoinRole::parse_fail_closed`].
pub join_role: String,
/// Whether active guests may reply within existing conversation threads.
pub guest_replies: bool,
/// Whether active guests may add reactions.
pub guest_reactions: bool,
/// Whether the channel is left out of channel listings for non-members.
/// It matters only while the channel is open: anyone can still read and
/// join an unlisted open channel by naming it, and private channels are
Expand Down Expand Up @@ -188,7 +192,7 @@ pub async fn create_channel(
nip29_group_id, topic_required, max_members,
topic, topic_set_by, topic_set_at,
purpose, purpose_set_by, purpose_set_at,
ttl_seconds, ttl_deadline, labels, join_role, unlisted, auto_join
ttl_seconds, ttl_deadline, labels, join_role, guest_replies, guest_reactions, unlisted, auto_join
FROM channels WHERE community_id = $1 AND id = $2
"#,
)
Expand Down Expand Up @@ -238,6 +242,7 @@ pub async fn create_channel_with_id(
ttl_seconds,
labels,
unlisted: false,
guest_contributions: None,
members: &[],
},
)
Expand All @@ -251,7 +256,7 @@ pub async fn create_channel_with_id(
nip29_group_id, topic_required, max_members,
topic, topic_set_by, topic_set_at,
purpose, purpose_set_by, purpose_set_at,
ttl_seconds, ttl_deadline, labels, join_role, unlisted, auto_join
ttl_seconds, ttl_deadline, labels, join_role, guest_replies, guest_reactions, unlisted, auto_join
FROM channels WHERE community_id = $1 AND id = $2
"#,
)
Expand All @@ -265,6 +270,15 @@ pub async fn create_channel_with_id(
Ok((record, was_created))
}

/// Contributions enabled at creation for a channel that admits viewers.
#[derive(Clone, Copy, Debug, Default)]
pub struct GuestContributions {
/// Viewers may send thread replies.
pub replies: bool,
/// Viewers may add message reactions.
pub reactions: bool,
}

/// A channel to create: the fields a kind:9007 sets.
pub struct NewChannel<'a> {
/// Display name, canonicalized before storage.
Expand All @@ -283,6 +297,8 @@ pub struct NewChannel<'a> {
pub labels: &'a [String],
/// Leave the channel out of listings for non-members from the start.
pub unlisted: bool,
/// When present, self-joins start as viewers with these grants.
pub guest_contributions: Option<GuestContributions>,
/// 32-byte pubkeys added as ordinary members invited by the creator. The
/// caller has authorized each addition.
pub members: &'a [Vec<u8>],
Expand Down Expand Up @@ -365,6 +381,7 @@ async fn insert_channel_rows(
ttl_seconds,
labels,
unlisted,
guest_contributions,
members,
} = channel;
let community_id = tx.community();
Expand All @@ -386,11 +403,17 @@ async fn insert_channel_rows(
return Err(DbError::InvalidData("channel name is required".into()));
}

let join_role = if guest_contributions.is_some() {
ChannelJoinRole::Guest
} else {
ChannelJoinRole::Member
};
let grants = guest_contributions.unwrap_or_default();
let rows_affected = sqlx::query(
r#"
INSERT INTO channels (id, community_id, name, channel_type, visibility, description, created_by, ttl_seconds, ttl_deadline, labels, unlisted)
INSERT INTO channels (id, community_id, name, channel_type, visibility, description, created_by, ttl_seconds, ttl_deadline, labels, unlisted, join_role, guest_replies, guest_reactions)
VALUES ($1, $2, $3, $4::channel_type, $5::channel_visibility, $6, $7, $8,
CASE WHEN $8 IS NOT NULL THEN NOW() + ($8 || ' seconds')::interval ELSE NULL END, $9, $10)
CASE WHEN $8 IS NOT NULL THEN NOW() + ($8 || ' seconds')::interval ELSE NULL END, $9, $10, $11, $12, $13)
ON CONFLICT (community_id, id) DO NOTHING
"#,
)
Expand All @@ -404,6 +427,9 @@ async fn insert_channel_rows(
.bind(ttl_seconds)
.bind(labels)
.bind(unlisted)
.bind(join_role.as_str())
.bind(grants.replies)
.bind(grants.reactions)
.execute(tx.conn())
.await
.map_err(|e| listed_name_taken(e, Some(name)))?
Expand Down Expand Up @@ -541,7 +567,7 @@ pub(crate) async fn select_channel(
nip29_group_id, topic_required, max_members,
topic, topic_set_by, topic_set_at,
purpose, purpose_set_by, purpose_set_at,
ttl_seconds, ttl_deadline, labels, join_role, unlisted, auto_join
ttl_seconds, ttl_deadline, labels, join_role, guest_replies, guest_reactions, unlisted, auto_join
FROM channels WHERE community_id = $1 AND id = $2 AND deleted_at IS NULL
"#,
)
Expand Down Expand Up @@ -630,7 +656,7 @@ async fn list_channels_with_operation(
nip29_group_id, topic_required, max_members,
topic, topic_set_by, topic_set_at,
purpose, purpose_set_by, purpose_set_at,
ttl_seconds, ttl_deadline, labels, join_role, unlisted, auto_join
ttl_seconds, ttl_deadline, labels, join_role, guest_replies, guest_reactions, unlisted, auto_join
FROM channels
WHERE community_id = $1 AND deleted_at IS NULL AND visibility::text = $2
ORDER BY created_at DESC
Expand All @@ -650,7 +676,7 @@ async fn list_channels_with_operation(
nip29_group_id, topic_required, max_members,
topic, topic_set_by, topic_set_at,
purpose, purpose_set_by, purpose_set_at,
ttl_seconds, ttl_deadline, labels, join_role, unlisted, auto_join
ttl_seconds, ttl_deadline, labels, join_role, guest_replies, guest_reactions, unlisted, auto_join
FROM channels
WHERE community_id = $1 AND deleted_at IS NULL
ORDER BY created_at DESC
Expand Down Expand Up @@ -691,7 +717,7 @@ pub async fn list_auto_join_channels(
nip29_group_id, topic_required, max_members,
topic, topic_set_by, topic_set_at,
purpose, purpose_set_by, purpose_set_at,
ttl_seconds, ttl_deadline, labels, join_role, unlisted, auto_join
ttl_seconds, ttl_deadline, labels, join_role, guest_replies, guest_reactions, unlisted, auto_join
FROM channels
WHERE community_id = $1 AND auto_join AND channel_type = 'stream'
AND visibility = 'open' AND archived_at IS NULL AND deleted_at IS NULL
Expand Down Expand Up @@ -795,6 +821,8 @@ pub(crate) fn row_to_channel_record(row: sqlx::postgres::PgRow) -> Result<Channe
// Selected by every channel-record query. A missing column is an
// error, never a silent `member`: the write gate depends on it.
join_role: row.try_get("join_role")?,
guest_replies: row.try_get("guest_replies")?,
guest_reactions: row.try_get("guest_reactions")?,
// Selected by every channel-record query, like `join_role`.
unlisted: row.try_get("unlisted")?,
auto_join: row.try_get("auto_join")?,
Expand Down Expand Up @@ -987,6 +1015,22 @@ pub fn group_metadata_tags(channel: &ChannelRecord, members: &[MemberRecord]) ->
&channel.created_at.timestamp().to_string(),
]));
tags.push(tag(&["join_role", join_role.as_str()]));
tags.push(tag(&[
"guest_replies",
if channel.guest_replies {
"true"
} else {
"false"
},
]));
tags.push(tag(&[
"guest_reactions",
if channel.guest_reactions {
"true"
} else {
"false"
},
]));
if channel.auto_join && open && channel.archived_at.is_none() {
tags.push(tag(&["auto_join"]));
}
Expand Down Expand Up @@ -1163,6 +1207,10 @@ pub struct ChannelUpdate {
pub labels: Option<Vec<String>>,
/// New join role (`"member"`/`"guest"`), or `None` to leave unchanged.
pub join_role: Option<String>,
/// Whether guests may reply; omission preserves the setting.
pub guest_replies: Option<bool>,
/// Whether guests may react; omission preserves the setting.
pub guest_reactions: Option<bool>,
/// New unlisted flag, or `None` to leave unchanged. It decides listing,
/// not admission, so it does not take the membership lock.
pub unlisted: Option<bool>,
Expand All @@ -1181,6 +1229,8 @@ impl ChannelUpdate {
&& self.ttl_seconds.is_none()
&& self.labels.is_none()
&& self.join_role.is_none()
&& self.guest_replies.is_none()
&& self.guest_reactions.is_none()
&& self.unlisted.is_none()
&& self.auto_join.is_none()
}
Expand Down Expand Up @@ -1449,6 +1499,15 @@ async fn update_channel_on(
set_parts.push(format!("join_role = ${param_idx}"));
param_idx += 1;
}
for (name, value) in [
("guest_replies", updates.guest_replies),
("guest_reactions", updates.guest_reactions),
] {
if value.is_some() {
set_parts.push(format!("{name} = ${param_idx}"));
param_idx += 1;
}
}
if updates.unlisted.is_some() {
set_parts.push(format!("unlisted = ${param_idx}"));
param_idx += 1;
Expand Down Expand Up @@ -1492,6 +1551,12 @@ async fn update_channel_on(
if let Some(ref join_role) = updates.join_role {
q = q.bind(join_role);
}
for value in [updates.guest_replies, updates.guest_reactions]
.into_iter()
.flatten()
{
q = q.bind(value);
}
if let Some(unlisted) = updates.unlisted {
q = q.bind(unlisted);
}
Expand Down Expand Up @@ -2551,6 +2616,8 @@ mod group_metadata_tag_tests {
ttl_deadline: None,
labels: Vec::new(),
join_role: join_role.into(),
guest_replies: false,
guest_reactions: false,
unlisted: false,
auto_join: false,
}
Expand Down Expand Up @@ -3204,6 +3271,7 @@ mod postgres_tests {
ttl_seconds: None,
labels: &[],
unlisted: false,
guest_contributions: None,
members,
}
}
Expand Down
Loading
Loading