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
54 changes: 54 additions & 0 deletions NOSTR.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,60 @@ the child, without joining it.
`parent_access` tag beside `parent` and files the channel unnarrowed, so a
client checks for it before filing a narrowed channel.

### Artifact conversations

A NIP-AR artifact, such as a task, can have a **conversation**: a channel
for its comments. It belongs to the artifact, not to anyone who posts in it.

- **Address.** The conversation's channel ID is derived from the artifact's
`d` alone, so every client computes the same one: take the first 16 bytes
of `SHA-256("nip-ar-conversation" || 0x00 || UTF-8(d))`, then set the
RFC 9562 version (8) and variant bits (byte 6 = `(b & 0x0f) | 0x80`,
byte 8 = `(b & 0x3f) | 0x80`). It does not change when the artifact moves.
Fixture: `d` `8c1f3d2e-4b5a-4c6d-9e7f-0a1b2c3d4e5f` gives
`3f3e3cd7-3b56-81d9-8dd2-8d8b5b1f6d57`. Version-8 channel IDs are
reserved for conversations: an ordinary kind:9007 at one is refused.
- **Opening.** kind:9007 with `["h", <derived id>]` and
`["artifact", <d>]`. Anyone who may post in the artifact's home may send
it; it needs the `messages:write` scope. The relay checks that `h` is the
derived address and that the artifact exists, is not deleted and lives in
that home. It then creates a private channel of type `artifact`, filed
under the home (`parent`), with no member rows: nobody owns it. Other
tags are ignored. Opening one that already exists is accepted and writes
nothing. The home must be a live, unarchived stream or forum with at most
one parent, so the conversation is at most the third level; otherwise the
relay refuses with `invalid: comments aren't available here`. An
artifact cannot itself live in a conversation.
- **Comments** are kind:9 events with the conversation's `h`. Who reads and
posts comes only from the home, through [Channel parents](#channel-parents):
the home's readers read, a view-only (`guest`) home's people read but do
not post, and a home's `parent_access` applies as it does to any child.
Comments close to new posts while the home is archived. Only the home
counts: an archived project above it still grants access and does not
archive its channels, so it does not close their comments either, just as
it does not close posting in those channels.
- **Only comments.** A conversation accepts only kind:9 messages, kind:40003
edits, kind:7 reactions, kind:5 and kind:9005 deletions, and the opening
kind:9007. Every other kind with its `h`, including people (9000, 9001,
9021, 9022), settings (9002), deletion (9008), a canvas, and any kind added
later, is refused with `invalid: an artifact's conversation only holds
comments`. The home's direct owners and admins may delete comments with
kind:9005; authors may delete their own.
- **Moves.** When the artifact moves, the conversation moves with it in the
same transaction: its parent becomes the new home, the old home's readers
lose it at once, and their live subscriptions close. A move to a home that
could not hold a conversation is refused with
`invalid: this artifact's comments can't move there`.
- **Delete and restore.** Deleting the artifact closes its conversation to
everyone: nothing in it is readable and nothing can be posted, and opening
one is refused. Restoring the artifact reopens it.
- **Hidden.** `artifact` is not a legacy channel type, so conversations are
left out of reads that name no channel ([NIP-CL](docs/nips/NIP-CL.md)).
Read them by naming the channel.
- Relays that support conversations advertise `buzz-artifact-conversations`
in NIP-11 `supported_extensions`. They also include artifacts in
auto-archiving channels in incremental artifact sync.

### Default channels

A community's owners and admins can mark a channel as a **default channel**:
Expand Down
27 changes: 27 additions & 0 deletions crates/buzz-core/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,36 @@ pub fn validate(event: &Event) -> Result<ArtifactEnvelope, &'static str> {
root,
})
}
/// Domain separator for [`conversation_id`].
pub const CONVERSATION_ID_DOMAIN: &[u8] = b"nip-ar-conversation\0";

/// The channel ID of an artifact's conversation, derived from its `d` alone:
/// the first 16 bytes of `SHA-256("nip-ar-conversation\0" || UTF-8(d))`, with
/// the RFC 9562 version (8) and variant bits set. Anyone holding the artifact
/// can compute it, and it does not change when the artifact moves.
pub fn conversation_id(d: &str) -> Uuid {
use sha2::{Digest, Sha256};
let mut hasher = Sha256::new();
hasher.update(CONVERSATION_ID_DOMAIN);
hasher.update(d.as_bytes());
let digest = hasher.finalize();
let mut bytes = [0u8; 16];
bytes.copy_from_slice(&digest[..16]);
uuid::Builder::from_custom_bytes(bytes).into_uuid()
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn conversation_id_matches_the_documented_fixture() {
let id = conversation_id("8c1f3d2e-4b5a-4c6d-9e7f-0a1b2c3d4e5f");
assert_eq!(id.get_version_num(), 8);
assert_eq!(id.get_variant(), uuid::Variant::RFC4122);
assert_eq!(id.to_string(), "3f3e3cd7-3b56-81d9-8dd2-8d8b5b1f6d57");
assert_ne!(id, conversation_id("8c1f3d2e-4b5a-4c6d-9e7f-0a1b2c3d4e50"));
}
use nostr::{EventBuilder, Keys, Kind, Tag};
fn event(extra: Vec<Vec<String>>, op: &str, content: &str) -> Event {
let mut tags = vec![
Expand Down
19 changes: 15 additions & 4 deletions crates/buzz-core/src/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ pub const MAX_CHANNEL_LABEL_LEN: usize = 64;
/// Names a label may not take, because the channel type uses the same `t`
/// tag. A label equal to one of these could make a channel look like
/// another type. The list includes types that a later relay may add.
pub const RESERVED_CHANNEL_LABELS: &[&str] = &["stream", "forum", "dm", "workflow", "system"];
pub const RESERVED_CHANNEL_LABELS: &[&str] =
&["stream", "forum", "dm", "workflow", "system", "artifact"];

/// Why a set of channel labels was refused.
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
Expand Down Expand Up @@ -389,6 +390,11 @@ pub enum ChannelType {
/// filter names it, or asks for system channels with `#t:["system"]` on
/// kinds 39000-39003.
System,
/// A NIP-AR artifact's conversation, such as a task's comments. Its ID is
/// derived from the artifact ([`crate::artifact::conversation_id`]), its
/// parent is the artifact's home, and it has no members or settings of
/// its own: who reads and posts comes from the home.
Artifact,
}

/// The channel types that every client knew before NIP-CL added new ones.
Expand All @@ -413,6 +419,7 @@ impl ChannelType {
Self::Dm => "dm",
Self::Workflow => "workflow",
Self::System => "system",
Self::Artifact => "artifact",
}
}

Expand All @@ -421,7 +428,7 @@ impl ChannelType {
/// private unless its creator asks for open.
pub fn default_visibility(&self) -> ChannelVisibility {
match self {
Self::System => ChannelVisibility::Private,
Self::System | Self::Artifact => ChannelVisibility::Private,
Self::Stream | Self::Forum | Self::Dm | Self::Workflow => ChannelVisibility::Open,
}
}
Expand All @@ -443,6 +450,7 @@ impl FromStr for ChannelType {
"dm" => Ok(Self::Dm),
"workflow" => Ok(Self::Workflow),
"system" => Ok(Self::System),
"artifact" => Ok(Self::Artifact),
other => Err(format!("unknown channel type: {other:?}")),
}
}
Expand Down Expand Up @@ -585,6 +593,7 @@ mod tests {
let _: ChannelType = name.parse().expect("legacy type parses");
}
assert!(!LEGACY_CHANNEL_TYPES.contains(&ChannelType::System.as_str()));
assert!(!LEGACY_CHANNEL_TYPES.contains(&ChannelType::Artifact.as_str()));
}

/// Every channel type name is reserved, so a label cannot pose as a type.
Expand All @@ -593,20 +602,22 @@ mod tests {
/// until its name is in `RESERVED_CHANNEL_LABELS`.
#[test]
fn every_channel_type_name_is_a_reserved_label() {
const ALL: [ChannelType; 5] = [
const ALL: [ChannelType; 6] = [
ChannelType::Stream,
ChannelType::Forum,
ChannelType::Dm,
ChannelType::Workflow,
ChannelType::System,
ChannelType::Artifact,
];
for channel_type in ALL {
match channel_type {
ChannelType::Stream
| ChannelType::Forum
| ChannelType::Dm
| ChannelType::Workflow
| ChannelType::System => {}
| ChannelType::System
| ChannelType::Artifact => {}
}
assert!(
RESERVED_CHANNEL_LABELS.contains(&channel_type.as_str()),
Expand Down
8 changes: 7 additions & 1 deletion crates/buzz-db/src/runtime/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -890,7 +890,7 @@ mod postgres_tests {
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
migrations.sort_by_key(|migration| migration.version);

assert_eq!(migrations.len(), 94);
assert_eq!(migrations.len(), 95);
assert_eq!(migrations[89].version, 90);
assert!(migrations[89]
.sql
Expand Down Expand Up @@ -938,6 +938,12 @@ mod postgres_tests {
assert!(parent_access
.contains("ADD COLUMN IF NOT EXISTS parent_access TEXT NOT NULL DEFAULT 'member'"));
assert!(!parent_access.to_ascii_uppercase().contains("UPDATE "));
// Artifact conversations (0095): one additive enum value.
let artifact_type = strip_sql_comments(migrations[94].sql.as_str());
assert_eq!(
artifact_type.trim(),
"ALTER TYPE channel_type ADD VALUE IF NOT EXISTS 'artifact';"
);
for (index, migration) in migrations.iter().enumerate() {
assert_eq!(migration.version, (index + 1) as i64);
}
Expand Down
Loading
Loading