diff --git a/db/init.sql b/db/init.sql index 90c2165..ad64996 100644 --- a/db/init.sql +++ b/db/init.sql @@ -273,11 +273,24 @@ CREATE TABLE IF NOT EXISTS digests ( body TEXT NOT NULL DEFAULT '', author_id INT REFERENCES users(id) ON DELETE SET NULL, is_pinned BOOLEAN NOT NULL DEFAULT false, + preview_path VARCHAR(255), + post_at TIMESTAMPTZ, + hide_at TIMESTAMPTZ, + published_at TIMESTAMPTZ, created_at TIMESTAMPTZ DEFAULT now(), updated_at TIMESTAMPTZ DEFAULT now() ); CREATE INDEX IF NOT EXISTS idx_digests_pinned_created ON digests(is_pinned DESC, created_at DESC); CREATE INDEX IF NOT EXISTS idx_digests_created ON digests(created_at DESC); +CREATE TABLE IF NOT EXISTS digest_images ( + id SERIAL PRIMARY KEY, + digest_id INT NOT NULL REFERENCES digests(id) ON DELETE CASCADE, + path VARCHAR(255) NOT NULL, + name VARCHAR(255) NOT NULL DEFAULT '', + size INT NOT NULL DEFAULT 0, + created_at TIMESTAMPTZ DEFAULT now() +); +CREATE INDEX IF NOT EXISTS idx_digest_images_digest ON digest_images(digest_id); CREATE TABLE IF NOT EXISTS sessions ( id SERIAL PRIMARY KEY, diff --git a/db/migration.sql b/db/migration.sql index ce8a6b8..7884a58 100644 --- a/db/migration.sql +++ b/db/migration.sql @@ -272,8 +272,21 @@ ALTER TABLE digests ADD COLUMN IF NOT EXISTS author_id INT REFERENCES users(id) ALTER TABLE digests ADD COLUMN IF NOT EXISTS is_pinned BOOLEAN NOT NULL DEFAULT false; ALTER TABLE digests ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ DEFAULT now(); ALTER TABLE digests ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ DEFAULT now(); +ALTER TABLE digests ADD COLUMN IF NOT EXISTS preview_path VARCHAR(255); +ALTER TABLE digests ADD COLUMN IF NOT EXISTS post_at TIMESTAMPTZ; +ALTER TABLE digests ADD COLUMN IF NOT EXISTS hide_at TIMESTAMPTZ; +ALTER TABLE digests ADD COLUMN IF NOT EXISTS published_at TIMESTAMPTZ; CREATE INDEX IF NOT EXISTS idx_digests_pinned_created ON digests(is_pinned DESC, created_at DESC); CREATE INDEX IF NOT EXISTS idx_digests_created ON digests(created_at DESC); +CREATE TABLE IF NOT EXISTS digest_images ( + id SERIAL PRIMARY KEY, + digest_id INT NOT NULL REFERENCES digests(id) ON DELETE CASCADE, + path VARCHAR(255) NOT NULL, + name VARCHAR(255) NOT NULL DEFAULT '', + size INT NOT NULL DEFAULT 0, + created_at TIMESTAMPTZ DEFAULT now() +); +CREATE INDEX IF NOT EXISTS idx_digest_images_digest ON digest_images(digest_id); CREATE TABLE IF NOT EXISTS api_keys ( id SERIAL PRIMARY KEY, diff --git a/lib/digest.js b/lib/digest.js index f3780e9..09e01d3 100644 --- a/lib/digest.js +++ b/lib/digest.js @@ -23,8 +23,8 @@ function ensureDigestTable(pool) { 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 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('ALTER TABLE digests ADD COLUMN IF NOT EXISTS preview_path VARCHAR(255)')) @@ -57,26 +57,29 @@ function validateDigest(body) { return { title, text, isPinned, previewPath, postAt, hideAt }; } -async function listDigests(pool) { +async function listDigests(pool, { admin = false } = {}) { const { rows } = await pool.query( - `SELECT d.id, d.title, d.body, d.author_id, d.is_pinned, d.preview_path, d.post_at, d.hide_at, d.created_at, d.updated_at, + `SELECT d.id, d.title, d.body, d.author_id, d.is_pinned, d.preview_path, d.post_at, d.hide_at, d.published_at, d.created_at, d.updated_at, u.name AS author_name, u.username AS author_username, COALESCE((SELECT json_agg(json_build_object('id', di.id, 'path', di.path, 'name', di.name, 'size', di.size) ORDER BY di.id) - FILTER (WHERE di.id IS NOT NULL)) FROM digest_images di WHERE di.digest_id = d.id), '[]'::json) AS images + FILTER (WHERE di.id IS NOT NULL) FROM digest_images di WHERE di.digest_id = d.id), '[]'::json) AS images FROM digests d LEFT JOIN users u ON u.id = d.author_id - ORDER BY d.is_pinned DESC, d.created_at DESC` + WHERE ($1::boolean OR (d.published_at IS NOT NULL AND (d.hide_at IS NULL OR d.hide_at > now()))) + ORDER BY d.is_pinned DESC, d.created_at DESC`, + [admin] ); return rows; } async function createDigest(pool, data, authorId) { const { title, text, isPinned, previewPath, postAt, hideAt } = validateDigest(data); + const publishNow = postAt == null; const { rows } = await pool.query( - `INSERT INTO digests (title, body, author_id, is_pinned, preview_path, post_at, hide_at) - VALUES ($1, $2, $3, $4, $5, $6, $7) - RETURNING id, title, body, author_id, is_pinned, preview_path, post_at, hide_at, created_at, updated_at`, - [title, text, authorId, isPinned, previewPath, postAt, hideAt] + `INSERT INTO digests (title, body, author_id, is_pinned, preview_path, post_at, hide_at, published_at) + VALUES ($1, $2, $3, $4, $5, $6, $7, $8) + RETURNING id, title, body, author_id, is_pinned, preview_path, post_at, hide_at, published_at, created_at, updated_at`, + [title, text, authorId, isPinned, previewPath, postAt, hideAt, publishNow ? new Date().toISOString() : null] ); return rows[0]; } @@ -86,7 +89,7 @@ async function updateDigest(pool, id, data, authorId, isAdmin) { const { rows } = await pool.query( `UPDATE digests SET title = $1, body = $2, is_pinned = $3, preview_path = $4, post_at = $5, hide_at = $6, updated_at = now() WHERE id = $7 AND ($8::boolean OR author_id = $9) - RETURNING id, title, body, author_id, is_pinned, preview_path, post_at, hide_at, created_at, updated_at`, + RETURNING id, title, body, author_id, is_pinned, preview_path, post_at, hide_at, published_at, created_at, updated_at`, [title, text, isPinned, previewPath, postAt, hideAt, id, isAdmin, authorId] ); if (!rows.length) throw { http: 404, message: 'Дайджест не найден или нет прав на редактирование' }; @@ -94,7 +97,12 @@ async function updateDigest(pool, id, data, authorId, isAdmin) { } async function deleteDigest(pool, id, authorId, isAdmin, unlink) { - const { rows: imgs } = await pool.query('SELECT path FROM digest_images WHERE digest_id = $1', [id]); + const { rows: imgs } = await pool.query( + `SELECT path FROM digest_images WHERE digest_id = $1 + UNION ALL + SELECT preview_path AS path FROM digests WHERE id = $1 AND preview_path IS NOT NULL`, + [id] + ); const { rows } = await pool.query( `DELETE FROM digests WHERE id = $1 AND ($2::boolean OR author_id = $3) RETURNING id`, [id, isAdmin, authorId] @@ -104,6 +112,16 @@ async function deleteDigest(pool, id, authorId, isAdmin, unlink) { return rows[0]; } +async function publishDueDigests(pool) { + const { rows } = await pool.query( + `UPDATE digests + SET published_at = now() + WHERE post_at IS NOT NULL AND post_at <= now() AND published_at IS NULL + RETURNING id, title, author_id, post_at, published_at` + ); + return rows; +} + async function toggleDigestPin(pool, id, isAdmin) { if (!isAdmin) throw { http: 403, message: 'Только администратор может закреплять дайджесты' }; const { rows } = await pool.query( @@ -164,4 +182,5 @@ module.exports = { listDigestImages, addDigestImage, deleteDigestImage, + publishDueDigests, }; diff --git a/public/digest.html b/public/digest.html index 94cea73..a0fb248 100644 --- a/public/digest.html +++ b/public/digest.html @@ -18,6 +18,20 @@ .digest-actions { display: flex; gap: 6px; flex-wrap: wrap; } .digest-empty { color: var(--muted); font-size: .95rem; padding: 16px; } .digest-new-btn { margin-left: auto; } + .digest-preview-img { max-width: 240px; max-height: 150px; border: 1px solid var(--border); border-radius: 8px; margin: 8px 0 4px 0; display: block; } + .digest-images { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; } + .digest-image { position: relative; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; width: 130px; height: 95px; flex-shrink: 0; } + .digest-image img { width: 100%; height: 100%; object-fit: cover; display: block; } + .digest-image-remove { position: absolute; top: 4px; right: 4px; background: rgba(0,0,0,.7); color: #fff; border: none; border-radius: 6px; padding: 2px 7px; font-size: .75rem; cursor: pointer; } + .digest-image-remove:hover { background: #ef4444; } + .digest-date-row { display: flex; gap: 14px; flex-wrap: wrap; align-items: center; } + .digest-date-row .settings-field { flex: 1; min-width: 200px; margin: 0; } + .field-hint { font-size: .78rem; color: var(--muted); margin-top: 4px; display: block; } + .digest-none { color: var(--muted); font-size: .85rem; font-style: italic; } + .digest-head-img { margin: 8px 0 10px 0; } + .digest-images-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; } + .digest-card-thumb { width: 70px; height: 55px; object-fit: cover; border: 1px solid var(--border); border-radius: 6px; flex-shrink: 0; } + .digest-hidden { opacity: 0.72; }
@@ -50,6 +64,40 @@