Skip to content
Draft
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
5 changes: 3 additions & 2 deletions NOSTR.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,10 @@ of **grants**. Grants are independent of community and channel roles.

- `maintainer` — identifies a community channel curator. Individual channel
operations document when they consult this grant.
- `moderator` — reserved for community moderation.

Owners and admins appoint them with **kind:9034**, `["p", <member>]` and
`["grant", "maintainer", "true"|"false"]`. The target must already
`["grant", "maintainer"|"moderator", "true"|"false"]`. The target must already
be a member of the community. An owner may change the grants of admins and
members; an admin only of members; nobody changes an owner's. The relay
decides the sender's and the target's roles with both roster rows locked, so
Expand Down Expand Up @@ -561,7 +562,7 @@ the sender to be authenticated (NIP-42) as the relay owner or an admin.
| 9031 | Remove member | `["p", "<hex-pubkey>"]`, optional `["role", "member\|admin"]` |
| 9032 | Change role | `["p", "<hex-pubkey>"]`, `["role", "member\|admin"]` |
| 9033 | Set workspace profile (icon) | `["icon", "<https-url or data:image/* URL>"]` (empty clears) |
| 9034 | Set grant | `["p", "<hex-pubkey>"]`, `["grant", "maintainer", "true\|false"]` (see [Community maintainers](#community-maintainers)) |
| 9034 | Set grant | `["p", "<hex-pubkey>"]`, `["grant", "maintainer\|moderator", "true\|false"]` (see [Community maintainers](#community-maintainers)) |

Example using `nak`:

Expand Down
9 changes: 8 additions & 1 deletion crates/buzz-core/src/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ use std::str::FromStr;
pub enum CommunityGrant {
/// Curates the community's channels.
Maintainer,
/// Reserved for community moderation.
Moderator,
}

impl CommunityGrant {
/// Every grant, in roster order.
pub const ALL: [Self; 1] = [Self::Maintainer];
pub const ALL: [Self; 2] = [Self::Maintainer, Self::Moderator];

/// Canonical string representation (matches the DB value and Nostr tags).
pub fn as_str(&self) -> &'static str {
match self {
Self::Maintainer => "maintainer",
Self::Moderator => "moderator",
}
}
}
Expand Down Expand Up @@ -100,6 +103,10 @@ mod tests {
let maintainer = CommunityAuthority::new(Some("member"), &grants(&["maintainer"]));
assert!(maintainer.may_maintain_channels());
assert!(!maintainer.is_steward());
assert!(
!CommunityAuthority::new(Some("member"), &grants(&["moderator"]))
.may_maintain_channels()
);
assert!(!CommunityAuthority::new(Some("member"), &[]).may_maintain_channels());
assert!(!CommunityAuthority::new(None, &grants(&["maintainer"])).is_steward());
}
Expand Down
65 changes: 46 additions & 19 deletions crates/buzz-relay/src/handlers/community_grants_postgres_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::sync::Arc;

use buzz_core::community::CommunityGrant;
use buzz_core::TenantContext;
use buzz_db::event::EventQuery;
use nostr::{Event, EventBuilder, Keys, Kind, Tag};
Expand Down Expand Up @@ -73,6 +74,28 @@ pub(crate) async fn grants(state: &AppState, tenant: &TenantContext, keys: &Keys
.unwrap_or_default()
}

async fn roster_tags(state: &AppState, tenant: &TenantContext) -> Vec<Vec<String>> {
let roster = state
.db
.query_events(
buzz_db::Scope::Home(tenant.community()),
&EventQuery {
kinds: Some(vec![buzz_core::kind::KIND_NIP43_MEMBERSHIP_LIST as i32]),
limit: Some(1),
global_only: true,
..EventQuery::for_community(tenant.community())
},
)
.await
.expect("roster");
roster[0]
.event
.tags
.iter()
.map(|t| t.as_slice().to_vec())
.collect()
}

/// Owners and admins appoint maintainers; an admin cannot touch another
/// admin's grants; the target must be a member of the same community; and the
/// relay-signed roster lists the grant. A grant in one community gives
Expand Down Expand Up @@ -135,25 +158,7 @@ async fn owners_and_admins_appoint_maintainers_within_their_community() {
.expect("read")
.is_none());

let roster = state
.db
.query_events(
buzz_db::Scope::Home(a.community()),
&EventQuery {
kinds: Some(vec![buzz_core::kind::KIND_NIP43_MEMBERSHIP_LIST as i32]),
limit: Some(1),
global_only: true,
..EventQuery::for_community(a.community())
},
)
.await
.expect("roster");
let tags: Vec<Vec<String>> = roster[0]
.event
.tags
.iter()
.map(|t| t.as_slice().to_vec())
.collect();
let tags = roster_tags(&state, &a).await;
assert!(tags.contains(&strs(&[
"grant",
&member.public_key().to_hex(),
Expand All @@ -165,6 +170,28 @@ async fn owners_and_admins_appoint_maintainers_within_their_community() {
.await
.expect("admin revokes");
assert!(grants(&state, &a, &member).await.is_empty());
set_grant(
&state,
&a,
&admin,
&member,
CommunityGrant::Moderator.as_str(),
true,
)
.await
.expect("moderator is independent");
assert_eq!(grants(&state, &a, &member).await, vec!["moderator"]);
let tags = roster_tags(&state, &a).await;
assert!(tags.contains(&strs(&[
"grant",
&member.public_key().to_hex(),
"moderator"
])));
assert!(!tags.contains(&strs(&[
"grant",
&member.public_key().to_hex(),
"maintainer"
])));
}

/// kind:9034 decides the signer's role again inside its transaction: a
Expand Down
Loading