Files
buzz/schema/tables/public/channels.sql
Matt Toohey da502cd222 feat(relay): add a channel auto_join setting for default channels
Add `channels.auto_join` (migration 0076, constant default false) so a
community's relay owners and admins can mark the channels the relay should
join new community members into. This is the contract half (R1): the
setting, who may change it, and how it is published. The auto-add at the
join sites lands separately (R2).

- kind:9002 accepts `["auto_join", "true"|"false"]`. A new authorization
  tier in the 9002 validator takes the actor's relay roster role
  (owner|admin) rather than a channel role, so a relay admin who is not a
  channel member can set it, and an event carrying only that tag needs
  neither channel-owner nor member authority. Only stream channels
  qualify. API tokens need `AdminChannels` for it, like `archived`.
- `apply_channel_metadata_event` refuses `auto_join=true` unless the state
  the event leaves behind is open and unarchived, decided under the
  channel row lock: one event may open and flag a channel, and a
  concurrent archive cannot slip past a pre-lock check. Every change is
  announced by one `auto_join_changed` kind:40099 notice.
- kind:39000 carries a bare `auto_join` tag only while the setting is
  effective (set, open, unarchived), so what the event says is what the
  relay will do, with no cascade logic on visibility flips or archive.
- `Db::list_auto_join_channels` (effective channels, oldest first, capped
  at 32) is the loader R2 will call.
- SDK `build_update_channel(.., auto_join)`, `buzz channels update
  --auto-join <true|false>`, a NOSTR.md "Default channels" section and
  the NIP-CL tag table row.

Tests: a Postgres-lane journey in edit_metadata_postgres_tests covering
the relay-role tier, the 40099 notice, the effective-state tag across a
flip to private, the combined open+flag event, and the private, archived
and forum refusals; group_metadata_tags unit rows; SDK and CLI builder
cases; and auto_join assertions in the announce-channel e2e wire test.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: Matt Toohey <contact@matttoohey.com>
2026-10-30 16:58:49 +11:00

65 lines
3.5 KiB
SQL

-- ── Channels ──────────────────────────────────────────────────────────────────
-- Conformance: "Channels and channel membership". `community_id` immutable.
-- Channel UUIDs stay valid wire identifiers, but they are NOT globally unique:
-- the PK is `(community_id, id)`, so the same UUID may legitimately exist in two
-- communities (conformance lists "same channel UUID collision in two
-- communities" as a required isolation test). Handlers always carry `ctx`, so
-- `(ctx.community, h)` names exactly one channel; a client-supplied `h` can
-- never reach another community's channel.
CREATE TABLE channels (
id UUID NOT NULL DEFAULT gen_random_uuid(),
community_id UUID NOT NULL REFERENCES communities(id),
name VARCHAR(255) NOT NULL,
channel_type channel_type NOT NULL DEFAULT 'stream',
visibility channel_visibility NOT NULL DEFAULT 'open',
description TEXT,
canvas TEXT,
created_by BYTEA NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
archived_at TIMESTAMPTZ,
deleted_at TIMESTAMPTZ,
nip29_group_id VARCHAR(255),
topic_required BOOLEAN NOT NULL DEFAULT FALSE,
max_members INT,
topic TEXT,
topic_set_by BYTEA,
topic_set_at TIMESTAMPTZ,
purpose TEXT,
purpose_set_by BYTEA,
purpose_set_at TIMESTAMPTZ,
participant_hash BYTEA,
ttl_seconds INT,
ttl_deadline TIMESTAMPTZ,
-- Labels, published as ["t", <label>] after the channel type tag on the
-- relay-signed group-state events. The relay validates their shape.
labels TEXT[] NOT NULL DEFAULT '{}'
CONSTRAINT channels_labels_bounded CHECK (cardinality(labels) <= 8),
join_role TEXT NOT NULL DEFAULT 'member'
CHECK (join_role IN ('member', 'guest')),
-- An unlisted open channel is readable and joinable by id but left out of
-- channel listings for non-members. Matters only while the channel is open.
unlisted BOOLEAN NOT NULL DEFAULT FALSE,
auto_join BOOLEAN NOT NULL DEFAULT false,
PRIMARY KEY (community_id, id),
CONSTRAINT chk_channels_id_not_nil CHECK (id <> '00000000-0000-0000-0000-000000000000'::uuid)
);
-- nip29 group id and DM participant hash are unique WITHIN a community, not globally.
CREATE UNIQUE INDEX idx_channels_nip29_group ON channels (community_id, nip29_group_id)
WHERE nip29_group_id IS NOT NULL;
CREATE UNIQUE INDEX idx_channels_dm_hash ON channels (community_id, participant_hash)
WHERE participant_hash IS NOT NULL;
CREATE INDEX idx_channels_community_type ON channels (community_id, channel_type);
CREATE INDEX idx_channels_community_visibility ON channels (community_id, visibility);
CREATE INDEX idx_channels_created_by ON channels (community_id, created_by);
CREATE INDEX idx_channels_ttl_expiry ON channels (ttl_deadline)
WHERE ttl_seconds IS NOT NULL AND archived_at IS NULL AND deleted_at IS NULL;
-- Tenant-independent channel-id → community lookups (Db::communities_of_channels,
-- Db::community_of_channel) carry no community_id predicate, so no
-- community_id-leading index can serve them. Covering + partial: index-only scan.
-- Not UNIQUE — the same channel id may exist under more than one community.
CREATE INDEX idx_channels_id_live ON channels (id) INCLUDE (community_id)
WHERE deleted_at IS NULL;