-- The forum (services/forum, docs/kozosseg.md): pseudonymous members, topics, -- threads, posts, reactions, read marks, followed threads and the moderation -- log, in a schema of its own. CREATE SCHEMA forum; -- 'moderator': appointed by an admin; may delete posts and threads and ban members. ALTER TABLE users DROP CONSTRAINT users_roles_valid; ALTER TABLE users ADD CONSTRAINT users_roles_valid CHECK (cardinality(roles) > 0 AND roles <@ ARRAY['admin', 'creator', 'moderator', 'premium', 'vip', 'user']::text[]); -- A user who chose a nickname. Nothing here is the real name: the nickname is -- typed by the user, never taken from the Google or Apple account. CREATE TABLE forum.members ( user_id uuid PRIMARY KEY REFERENCES users ON DELETE CASCADE, nickname text NOT NULL, -- The nickname folded (no accents, case, spaces or look-alike digits): unique. nickname_key text NOT NULL UNIQUE, -- A starter avatar, '-' (the frontend draws it). avatar text NOT NULL, bio text NOT NULL DEFAULT '', -- What the profile shows of the user's own chart: nothing, the Sun sign, -- Sun, Moon and Ascendant, or every planet's sign. Never degrees. chart_share text NOT NULL DEFAULT 'none' CHECK (chart_share IN ('none', 'sun', 'big3', 'planets')), -- Not shown as online. invisible boolean NOT NULL DEFAULT false, -- Reactions received (counted ones), "Ez segített" marks; the rank comes from these. points integer NOT NULL DEFAULT 0, post_count integer NOT NULL DEFAULT 0, created_at timestamptz NOT NULL DEFAULT now(), nickname_changed_at timestamptz NOT NULL DEFAULT now(), last_seen_at timestamptz, -- Banned until then (year 9999: for good): may read, not write. banned_until timestamptz, ban_reason text ); -- A nickname given up stays reserved for a while, so nobody can take over a -- known member's name. CREATE TABLE forum.retired_nicknames ( nickname_key text PRIMARY KEY, user_id uuid NOT NULL REFERENCES users ON DELETE CASCADE, until timestamptz NOT NULL ); CREATE TABLE forum.topics ( id smallint PRIMARY KEY, slug text NOT NULL UNIQUE, name text NOT NULL, description text NOT NULL, -- public: anyone reads (search engines too); members: logged-in users; -- restricted: approved members only (not used yet: moderators only). visibility text NOT NULL DEFAULT 'public' CHECK (visibility IN ('public', 'members', 'restricted')), -- A listening topic: no ⌄ there, and new threads ask for listening by default. listening boolean NOT NULL DEFAULT false, position smallint NOT NULL ); INSERT INTO forum.topics (id, slug, name, description, visibility, listening, position) VALUES (1, 'ami-a-szivemen-van', 'Ami a szívemen van', 'Meghallgatás, élethelyzetek', 'members', true, 1), (2, 'belso-utak', 'Belső utak', 'Spiritualitás, meditáció, álmok', 'public', false, 2), (3, 'egbolt-es-kepletek', 'Égbolt és képletek', 'Asztrológia, megosztott elemzések', 'public', false, 3), (4, 'torzsasztal', 'Törzsasztal', 'Bármi, ami eszedbe jut', 'public', false, 4); CREATE TABLE forum.threads ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, topic_id smallint NOT NULL REFERENCES forum.topics, -- NULL once the account is deleted (the thread stays, "törölt tag"). author_id uuid REFERENCES users ON DELETE SET NULL, title text NOT NULL, -- What the author asks for: only listening, or advice and opinions. intent text CHECK (intent IN ('listen', 'advice')), created_at timestamptz NOT NULL DEFAULT now(), last_post_at timestamptz NOT NULL DEFAULT now(), -- Visible replies (the opening post not counted). reply_count integer NOT NULL DEFAULT 0, pinned boolean NOT NULL DEFAULT false, locked boolean NOT NULL DEFAULT false, deleted_at timestamptz, deleted_by uuid REFERENCES users ON DELETE SET NULL, delete_reason text ); CREATE INDEX threads_topic_idx ON forum.threads (topic_id, pinned DESC, last_post_at DESC) WHERE deleted_at IS NULL; CREATE INDEX threads_author_idx ON forum.threads (author_id, created_at DESC); CREATE TABLE forum.posts ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, thread_id bigint NOT NULL REFERENCES forum.threads ON DELETE CASCADE, author_id uuid REFERENCES users ON DELETE SET NULL, -- A reply to a reply (two levels at most: the parent is never itself a reply). parent_id bigint REFERENCES forum.posts ON DELETE SET NULL, is_opening boolean NOT NULL DEFAULT false, body text NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), edited_at timestamptz, -- The thread's author marked it "Ez segített". helped_at timestamptz, -- Hidden, not erased: a moderator's reason shows in its place, an admin can restore it. deleted_at timestamptz, deleted_by uuid REFERENCES users ON DELETE SET NULL, delete_reason text ); CREATE INDEX posts_thread_idx ON forum.posts (thread_id, id); CREATE INDEX posts_author_idx ON forum.posts (author_id, created_at DESC); CREATE INDEX posts_recent_idx ON forum.posts (created_at DESC); CREATE UNIQUE INDEX posts_opening_key ON forum.posts (thread_id) WHERE is_opening; -- One reaction per member and post; each is +1 to the author's points, 'down' (⌄) is -1. CREATE TABLE forum.reactions ( post_id bigint NOT NULL REFERENCES forum.posts ON DELETE CASCADE, user_id uuid NOT NULL REFERENCES users ON DELETE CASCADE, kind text NOT NULL CHECK (kind IN ('with_you', 'thanks', 'me_too', 'insight', 'down')), -- Whether it moved the author's points (a new member's reaction does not). counted boolean NOT NULL, created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (post_id, user_id) ); -- How far a member has read a thread ("Itt hagytad abba"). CREATE TABLE forum.reads ( user_id uuid NOT NULL REFERENCES users ON DELETE CASCADE, thread_id bigint NOT NULL REFERENCES forum.threads ON DELETE CASCADE, last_post_id bigint NOT NULL, read_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (user_id, thread_id) ); -- Followed threads: one's own and the ones replied to, or followed by hand. CREATE TABLE forum.follows ( user_id uuid NOT NULL REFERENCES users ON DELETE CASCADE, thread_id bigint NOT NULL REFERENCES forum.threads ON DELETE CASCADE, created_at timestamptz NOT NULL DEFAULT now(), PRIMARY KEY (user_id, thread_id) ); CREATE INDEX follows_thread_idx ON forum.follows (thread_id); -- Every moderation step: who (a person or, later, the AI), what, why. CREATE TABLE forum.mod_log ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, actor_id uuid REFERENCES users ON DELETE SET NULL, actor_kind text NOT NULL DEFAULT 'human' CHECK (actor_kind IN ('human', 'ai')), action text NOT NULL CHECK (action IN ('delete_post', 'restore_post', 'delete_thread', 'restore_thread', 'ban', 'unban', 'lock', 'unlock', 'pin', 'unpin')), target_user uuid REFERENCES users ON DELETE SET NULL, thread_id bigint, post_id bigint, reason text, until timestamptz, created_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX mod_log_created_idx ON forum.mod_log (created_at DESC);