'use strict'; const DIGEST_TITLE_MAX = 200; const DIGEST_BODY_MAX = 5000; function ensureDigestTable(pool) { return pool.query(`CREATE TABLE IF NOT EXISTS digests ( id SERIAL PRIMARY KEY, title VARCHAR(200) NOT NULL DEFAULT '', body TEXT NOT NULL DEFAULT '', author_id INT REFERENCES users(id) ON DELETE SET NULL, is_pinned BOOLEAN NOT NULL DEFAULT false, created_at TIMESTAMPTZ DEFAULT now(), updated_at TIMESTAMPTZ DEFAULT now() )`) .then(() => pool.query('ALTER TABLE digests ADD COLUMN IF NOT EXISTS title VARCHAR(200) NOT NULL DEFAULT \'\'')) .then(() => pool.query('ALTER TABLE digests ADD COLUMN IF NOT EXISTS body TEXT NOT NULL DEFAULT \'\'')) .then(() => pool.query('ALTER TABLE digests ADD COLUMN IF NOT EXISTS author_id INT REFERENCES users(id) ON DELETE SET NULL')) .then(() => pool.query('ALTER TABLE digests ADD COLUMN IF NOT EXISTS is_pinned BOOLEAN NOT NULL DEFAULT false')) .then(() => pool.query('CREATE INDEX IF NOT EXISTS idx_digests_pinned_created ON digests(is_pinned DESC, created_at DESC)')) .then(() => pool.query('CREATE INDEX IF NOT EXISTS idx_digests_created ON digests(created_at DESC)')) .then(() => pool.query("INSERT INTO settings (key, value) VALUES ('digest_enabled', 'true') ON CONFLICT (key) DO NOTHING")); } function validateDigest(body) { const title = typeof body.title === 'string' ? body.title.trim() : ''; if (!title || title.length > DIGEST_TITLE_MAX) throw new Error('Неверная длина заголовка'); const text = typeof body.body === 'string' ? body.body.trim() : ''; if (!text || text.length > DIGEST_BODY_MAX) throw new Error('Неверная длина текста'); const isPinned = body.is_pinned === true; return { title, text, isPinned }; } async function listDigests(pool) { const { rows } = await pool.query( `SELECT d.id, d.title, d.body, d.author_id, d.is_pinned, d.created_at, d.updated_at, u.name AS author_name, u.username AS author_username FROM digests d LEFT JOIN users u ON u.id = d.author_id ORDER BY d.is_pinned DESC, d.created_at DESC` ); return rows; } async function createDigest(pool, data, authorId) { const { title, text, isPinned } = validateDigest(data); const { rows } = await pool.query( `INSERT INTO digests (title, body, author_id, is_pinned) VALUES ($1, $2, $3, $4) RETURNING id, title, body, author_id, is_pinned, created_at, updated_at`, [title, text, authorId, isPinned] ); return rows[0]; } async function updateDigest(pool, id, data, authorId, isAdmin) { const { title, text, isPinned } = validateDigest(data); const { rows } = await pool.query( `UPDATE digests SET title = $1, body = $2, is_pinned = $3, updated_at = now() WHERE id = $4 AND ($5::boolean OR author_id = $6) RETURNING id, title, body, author_id, is_pinned, created_at, updated_at`, [title, text, isPinned, id, isAdmin, authorId] ); if (!rows.length) throw { http: 404, message: 'Дайджест не найден или нет прав на редактирование' }; return rows[0]; } async function deleteDigest(pool, id, authorId, isAdmin) { const { rows } = await pool.query( `DELETE FROM digests WHERE id = $1 AND ($2::boolean OR author_id = $3) RETURNING id`, [id, isAdmin, authorId] ); if (!rows.length) throw { http: 404, message: 'Дайджест не найден или нет прав на удаление' }; return rows[0]; } async function toggleDigestPin(pool, id, isAdmin) { if (!isAdmin) throw { http: 403, message: 'Только администратор может закреплять дайджесты' }; const { rows } = await pool.query( `UPDATE digests SET is_pinned = NOT is_pinned, updated_at = now() WHERE id = $1 RETURNING id, is_pinned`, [id] ); if (!rows.length) throw { http: 404, message: 'Дайджест не найден' }; return rows[0]; } module.exports = { DIGEST_TITLE_MAX, DIGEST_BODY_MAX, ensureDigestTable, listDigests, createDigest, updateDigest, deleteDigest, toggleDigestPin, };