From 62d96e5066c5eb70d955c35b08abad0233d84bff Mon Sep 17 00:00:00 2001 From: dev Date: Thu, 5 Nov 2026 13:45:09 +0300 Subject: [PATCH] Refactor digest: storage, backup and core logic --- backup-restore.js | 1 + db/init.sql | 1 + db/migration.sql | 1 + lib/digest.js | 84 ++++++++++++++++++-------- public/digest.html | 16 ++++- public/js/digest.js | 140 +++++++++++++++++++++++++++++++++++++------- server.js | 47 ++++++++++++--- 7 files changed, 233 insertions(+), 57 deletions(-) diff --git a/backup-restore.js b/backup-restore.js index f99a991..8adc0fd 100644 --- a/backup-restore.js +++ b/backup-restore.js @@ -711,6 +711,7 @@ function normalizeRestoreData(data) { preview_path: optUploadPath(x.preview_path, 255) || null, post_at: optTs(x.post_at), hide_at: optTs(x.hide_at), + deleted_at: optTs(x.deleted_at), created_at: optTs(x.created_at), updated_at: optTs(x.updated_at), })); diff --git a/db/init.sql b/db/init.sql index c310f3e..b251cd7 100644 --- a/db/init.sql +++ b/db/init.sql @@ -277,6 +277,7 @@ CREATE TABLE IF NOT EXISTS digests ( post_at TIMESTAMPTZ, hide_at TIMESTAMPTZ, published_at TIMESTAMPTZ, + deleted_at TIMESTAMPTZ, created_at TIMESTAMPTZ DEFAULT now(), updated_at TIMESTAMPTZ DEFAULT now() ); diff --git a/db/migration.sql b/db/migration.sql index 4450efb..e32a8c2 100644 --- a/db/migration.sql +++ b/db/migration.sql @@ -276,6 +276,7 @@ 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; +ALTER TABLE digests ADD COLUMN IF NOT EXISTS deleted_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 ( diff --git a/lib/digest.js b/lib/digest.js index 09e01d3..bdc6894 100644 --- a/lib/digest.js +++ b/lib/digest.js @@ -20,6 +20,8 @@ function ensureDigestTable(pool) { preview_path VARCHAR(255), post_at TIMESTAMPTZ, hide_at TIMESTAMPTZ, + published_at TIMESTAMPTZ, + deleted_at TIMESTAMPTZ, created_at TIMESTAMPTZ DEFAULT now(), updated_at TIMESTAMPTZ DEFAULT now() )`) @@ -30,6 +32,8 @@ function ensureDigestTable(pool) { .then(() => pool.query('ALTER TABLE digests ADD COLUMN IF NOT EXISTS preview_path VARCHAR(255)')) .then(() => pool.query('ALTER TABLE digests ADD COLUMN IF NOT EXISTS post_at TIMESTAMPTZ')) .then(() => pool.query('ALTER TABLE digests ADD COLUMN IF NOT EXISTS hide_at TIMESTAMPTZ')) + .then(() => pool.query('ALTER TABLE digests ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ')) + .then(() => pool.query('ALTER TABLE digests ADD COLUMN IF NOT EXISTS published_at TIMESTAMPTZ')) .then(() => pool.query(`CREATE TABLE IF NOT EXISTS digest_images ( id SERIAL PRIMARY KEY, digest_id INT NOT NULL REFERENCES digests(id) ON DELETE CASCADE, @@ -57,15 +61,25 @@ function validateDigest(body) { return { title, text, isPinned, previewPath, postAt, hideAt }; } -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.published_at, d.created_at, d.updated_at, +async function listDigests(pool, { admin = false, userId = null, deleted = false } = {}) { + const fields = `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.deleted_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 - FROM digests d - LEFT JOIN users u ON u.id = d.author_id - WHERE ($1::boolean OR (d.published_at IS NOT NULL AND (d.hide_at IS NULL OR d.hide_at > now()))) + FILTER (WHERE di.id IS NOT NULL) FROM digest_images di WHERE di.digest_id = d.id), '[]'::json) AS images`; + const base = `FROM digests d LEFT JOIN users u ON u.id = d.author_id`; + if (deleted) { + const { rows } = await pool.query( + `SELECT ${fields} ${base} + WHERE d.deleted_at IS NOT NULL AND ($1::boolean OR d.author_id = $2) + ORDER BY d.deleted_at DESC, d.id DESC`, + [admin, userId] + ); + return rows; + } + const { rows } = await pool.query( + `SELECT ${fields} ${base} + WHERE d.deleted_at IS NULL + AND ($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] ); @@ -88,7 +102,7 @@ async function updateDigest(pool, id, data, authorId, isAdmin) { const { title, text, isPinned, previewPath, postAt, hideAt } = validateDigest(data); 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) + WHERE id = $7 AND deleted_at IS NULL AND ($8::boolean OR author_id = $9) 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] ); @@ -96,27 +110,49 @@ async function updateDigest(pool, id, data, authorId, isAdmin) { return rows[0]; } -async function deleteDigest(pool, id, authorId, isAdmin, unlink) { - 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] - ); +async function softDeleteDigest(pool, id, authorId, isAdmin) { const { rows } = await pool.query( - `DELETE FROM digests WHERE id = $1 AND ($2::boolean OR author_id = $3) RETURNING id`, + `UPDATE digests SET deleted_at = now(), updated_at = now() + WHERE id = $1 AND deleted_at IS NULL AND ($2::boolean OR author_id = $3) + RETURNING id, deleted_at`, [id, isAdmin, authorId] ); - if (!rows.length) throw { http: 404, message: 'Дайджест не найден или нет прав на удаление' }; - if (typeof unlink === 'function') imgs.forEach(r => unlink(r.path)); + if (!rows.length) throw { http: 404, message: 'Дайджест не найден или уже удалён' }; return rows[0]; } +async function restoreDigest(pool, id, authorId, isAdmin) { + const { rows } = await pool.query( + `UPDATE digests SET deleted_at = NULL, updated_at = now() + WHERE id = $1 AND deleted_at IS NOT NULL AND ($2::boolean OR author_id = $3) + RETURNING id, deleted_at`, + [id, isAdmin, authorId] + ); + if (!rows.length) throw { http: 404, message: 'Дайджест не найден или не удалён' }; + return rows[0]; +} + +async function deleteDigestPermanent(pool, id, authorId, isAdmin, unlink) { + const { rows: found } = await pool.query( + `SELECT id, preview_path FROM digests + WHERE id = $1 AND deleted_at IS NOT NULL AND ($2::boolean OR author_id = $3)`, + [id, isAdmin, authorId] + ); + if (!found.length) throw { http: 404, message: 'Дайджест не найден или не удалён' }; + const { rows: imgs } = await pool.query('SELECT path FROM digest_images WHERE digest_id = $1', [id]); + await pool.query('DELETE FROM digests WHERE id = $1', [id]); + if (typeof unlink === 'function') { + imgs.forEach(r => unlink(r.path)); + if (found[0].preview_path) unlink(found[0].preview_path); + } + return found[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 + WHERE post_at IS NOT NULL AND post_at <= now() AND published_at IS NULL AND deleted_at IS NULL RETURNING id, title, author_id, post_at, published_at` ); return rows; @@ -125,7 +161,7 @@ async function publishDueDigests(pool) { 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`, + `UPDATE digests SET is_pinned = NOT is_pinned, updated_at = now() WHERE id = $1 AND deleted_at IS NULL RETURNING id, is_pinned`, [id] ); if (!rows.length) throw { http: 404, message: 'Дайджест не найден' }; @@ -142,7 +178,7 @@ async function listDigestImages(pool, digestId) { async function addDigestImage(pool, digestId, path, name, size, authorId, isAdmin) { const { rows } = await pool.query( - 'SELECT id FROM digests WHERE id = $1 AND ($2::boolean OR author_id = $3)', + 'SELECT id FROM digests WHERE id = $1 AND deleted_at IS NULL AND ($2::boolean OR author_id = $3)', [digestId, isAdmin, authorId] ); if (!rows.length) throw { http: 404, message: 'Дайджест не найден или нет прав на добавление вложений' }; @@ -160,7 +196,7 @@ async function deleteDigestImage(pool, imageId, authorId, isAdmin, unlink) { const { rows } = await pool.query( `SELECT di.id, di.path FROM digest_images di JOIN digests d ON d.id = di.digest_id - WHERE di.id = $1 AND ($2::boolean OR d.author_id = $3)`, + WHERE di.id = $1 AND d.deleted_at IS NULL AND ($2::boolean OR d.author_id = $3)`, [imageId, isAdmin, authorId] ); if (!rows.length) throw { http: 404, message: 'Вложение не найдено или нет прав на удаление' }; @@ -177,7 +213,9 @@ module.exports = { listDigests, createDigest, updateDigest, - deleteDigest, + softDeleteDigest, + restoreDigest, + deleteDigestPermanent, toggleDigestPin, listDigestImages, addDigestImage, diff --git a/public/digest.html b/public/digest.html index 865c8a9..4cb6171 100644 --- a/public/digest.html +++ b/public/digest.html @@ -9,7 +9,10 @@ .digest-layout { display: grid; grid-template-columns: 280px minmax(0, 1fr); gap: 20px; align-items: start; } .digest-side { position: sticky; top: 20px; background: var(--card); border: 1px solid var(--border); border-radius: 12px; overflow: hidden; display: flex; flex-direction: column; max-height: calc(100dvh - 40px); } .digest-side-head { display: flex; align-items: center; gap: 8px; padding: 12px 14px; border-bottom: 1px solid var(--border); } - .digest-side-title { font-size: .85rem; font-weight: 700; white-space: nowrap; } + .digest-side-title { font-size: .85rem; font-weight: 700; white-space: nowrap; min-width: 0; overflow: hidden; text-overflow: ellipsis; } + .digest-deleted-btn { width: 28px; height: 28px; padding: 0; flex: 0 0 auto; } + .digest-deleted-btn svg { width: 15px; height: 15px; } + .digest-deleted-btn.active { background: rgba(239,68,68,.12); border-color: rgba(239,68,68,.35); color: #dc2626; } .digest-side-search { padding: 10px 12px; border-bottom: 1px solid var(--border); } .digest-side-search .digest-search { min-width: 0; max-width: none; } .digest-nav { overflow-y: auto; overscroll-behavior: contain; display: flex; flex-direction: column; flex: 1 1 auto; min-height: 0; } @@ -20,19 +23,24 @@ .digest-nav-title { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; font-size: .85rem; font-weight: 600; line-height: 1.35; color: var(--text); overflow-wrap: anywhere; } .digest-nav-title svg { width: 12px; height: 12px; margin-right: 4px; vertical-align: -1px; color: #b45309; } .digest-nav-date { font-size: .72rem; color: var(--muted); white-space: nowrap; } + .digest-nav-item.is-deleted .digest-nav-title { color: var(--muted); } + .digest-nav-item.is-deleted .digest-nav-title svg { color: #dc2626; } .digest-nav-empty { padding: 22px 14px; color: var(--muted); font-size: .85rem; text-align: center; } .digest-search { position: relative; flex: 1; min-width: 240px; max-width: 460px; display: flex; align-items: center; } .digest-search > svg { position: absolute; left: 14px; width: 16px; height: 16px; color: var(--muted); pointer-events: none; } .digest-search input { width: 100%; background: var(--card); border: 1px solid var(--border); color: var(--text); border-radius: 11px; padding: 11px 14px 11px 40px; font-size: .9rem; font-family: inherit; outline: none; transition: border-color .15s, box-shadow .15s; } .digest-search input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(37,99,235,.1); } - .digest-count { font-size: .75rem; font-weight: 500; color: var(--muted); margin-left: auto; white-space: nowrap; } + .digest-count { font-size: .75rem; font-weight: 500; color: var(--muted); margin-left: auto; white-space: nowrap; min-width: 0; overflow: hidden; text-overflow: ellipsis; } .digest-list { display: flex; flex-direction: column; gap: 20px; min-width: 0; } .digest-card { position: relative; background: var(--card); border: 1px solid var(--border); border-radius: 16px; display: flex; flex-direction: column; min-width: 0; overflow: hidden; scroll-margin-top: 20px; transition: box-shadow .18s, border-color .18s; } .digest-card:hover { box-shadow: 0 6px 24px rgba(0,0,0,.06); } .digest-card.pinned { border-left: 3px solid #f59e0b; background: linear-gradient(180deg, rgba(245,158,11,.055), var(--card) 180px); } .digest-card.digest-hidden { opacity: .62; } + .digest-card.is-deleted { border-style: dashed; background: var(--bg); } + .digest-card.is-deleted .digest-body { opacity: .78; } + .digest-card.is-deleted .digest-cover { filter: grayscale(.6); } .digest-card-top { padding: 24px 28px 0; display: flex; flex-direction: column; gap: 12px; } .digest-badges { display: flex; flex-wrap: wrap; gap: 6px; } @@ -40,6 +48,7 @@ .digest-badge svg { width: 12px; height: 12px; } .digest-badge.scheduled { background: rgba(37,99,235,.1); color: var(--accent); } .digest-badge.dim { background: var(--bg); color: var(--muted); } + .digest-badge.deleted { background: rgba(239,68,68,.12); color: #dc2626; } .digest-title { margin: 0; font-size: 1.35rem; font-weight: 700; line-height: 1.3; letter-spacing: -.01em; word-break: break-word; } .digest-meta { display: flex; align-items: center; gap: 10px; min-width: 0; } .digest-avatar { width: 36px; height: 36px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; flex-shrink: 0; background: rgba(37,99,235,.1); color: var(--accent); font-weight: 700; font-size: .85rem; text-transform: uppercase; } @@ -140,8 +149,9 @@