4.4 KiB · text History 263b40b
//! Moderation: topic locks, category posting rules, staff moderation of a
//! post (through its revision history), flags, user sanctions, the flag
//! shadowban, and the moderation log.
//!
//! * `categories.topic_min_trust` / `reply_min_trust` — who may start a topic
//!   / reply, separate from `min_trust_level` (who may read): NULL = anyone
//!   who can read it, 0..4 = that trust level or higher, 5 = staff only.
//! * `topics.is_closed` is the lock; `closed_at/by/reason` say who and why.
//! * `posts.moderated_at/by` — the body is a staff note; `posts.hidden_at` —
//!   hidden pending staff review (flags reached the threshold).
//! * `post_revisions.kind` (`edit` | `moderation` | `restore`) and
//!   `hidden_at/by` (staff hid this version's content from non-staff).
//! * `users.silenced_*`, `suspended_*`, `flags_ignored_at` (flag shadowban).
//! * `post_flags`, one per (post, user); `moderation_log`.
use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

const UP: &str = r#"
ALTER TABLE categories
    ADD COLUMN topic_min_trust INTEGER NULL,
    ADD COLUMN reply_min_trust INTEGER NULL;

ALTER TABLE topics
    ADD COLUMN closed_at TIMESTAMPTZ NULL,
    ADD COLUMN closed_by_id UUID NULL REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE,
    ADD COLUMN closed_reason TEXT NULL;

ALTER TABLE posts
    ADD COLUMN moderated_at TIMESTAMPTZ NULL,
    ADD COLUMN moderated_by_id UUID NULL REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE,
    ADD COLUMN hidden_at TIMESTAMPTZ NULL;

ALTER TABLE post_revisions
    ADD COLUMN kind VARCHAR(16) NOT NULL DEFAULT 'edit',
    ADD COLUMN hidden_at TIMESTAMPTZ NULL,
    ADD COLUMN hidden_by_id UUID NULL REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE;

ALTER TABLE users
    ADD COLUMN silenced_at TIMESTAMPTZ NULL,
    ADD COLUMN silenced_until TIMESTAMPTZ NULL,
    ADD COLUMN silence_reason TEXT NULL,
    ADD COLUMN suspended_at TIMESTAMPTZ NULL,
    ADD COLUMN suspended_until TIMESTAMPTZ NULL,
    ADD COLUMN suspend_reason TEXT NULL,
    ADD COLUMN flags_ignored_at TIMESTAMPTZ NULL;

CREATE TABLE post_flags (
    id UUID PRIMARY KEY,
    post_id UUID NOT NULL REFERENCES posts(id) ON DELETE CASCADE ON UPDATE CASCADE,
    user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE ON UPDATE CASCADE,
    reason VARCHAR(16) NOT NULL,
    note TEXT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
    resolved_at TIMESTAMPTZ NULL,
    resolved_by_id UUID NULL REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE,
    resolution VARCHAR(16) NULL
);
CREATE UNIQUE INDEX idx_post_flags_post_user ON post_flags (post_id, user_id);
CREATE INDEX idx_post_flags_pending ON post_flags (post_id) WHERE resolved_at IS NULL;

CREATE TABLE moderation_log (
    id UUID PRIMARY KEY,
    actor_id UUID NULL REFERENCES users(id) ON DELETE SET NULL ON UPDATE CASCADE,
    source VARCHAR(8) NOT NULL,
    action VARCHAR(32) NOT NULL,
    target_kind VARCHAR(16) NOT NULL,
    target_id UUID NULL,
    target_label TEXT NOT NULL,
    reason TEXT NULL,
    created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX idx_moderation_log_created ON moderation_log (created_at DESC);
"#;

const DOWN: &str = r#"
DROP TABLE IF EXISTS moderation_log;
DROP TABLE IF EXISTS post_flags;
ALTER TABLE users
    DROP COLUMN IF EXISTS flags_ignored_at,
    DROP COLUMN IF EXISTS suspend_reason,
    DROP COLUMN IF EXISTS suspended_until,
    DROP COLUMN IF EXISTS suspended_at,
    DROP COLUMN IF EXISTS silence_reason,
    DROP COLUMN IF EXISTS silenced_until,
    DROP COLUMN IF EXISTS silenced_at;
ALTER TABLE post_revisions
    DROP COLUMN IF EXISTS hidden_by_id,
    DROP COLUMN IF EXISTS hidden_at,
    DROP COLUMN IF EXISTS kind;
ALTER TABLE posts
    DROP COLUMN IF EXISTS hidden_at,
    DROP COLUMN IF EXISTS moderated_by_id,
    DROP COLUMN IF EXISTS moderated_at;
ALTER TABLE topics
    DROP COLUMN IF EXISTS closed_reason,
    DROP COLUMN IF EXISTS closed_by_id,
    DROP COLUMN IF EXISTS closed_at;
ALTER TABLE categories
    DROP COLUMN IF EXISTS reply_min_trust,
    DROP COLUMN IF EXISTS topic_min_trust;
"#;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager.get_connection().execute_unprepared(UP).await.map(|_| ())
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager.get_connection().execute_unprepared(DOWN).await.map(|_| ())
    }
}