diff --git a/backup-restore.js b/backup-restore.js index f53dfff..dac7bba 100644 --- a/backup-restore.js +++ b/backup-restore.js @@ -607,6 +607,9 @@ function normalizeRestoreData(data) { ai_model: optStr(r.ai_model, 200), ai_duration_ms: optInt(r.ai_duration_ms, 0, 86400000), author_id: optInt(r.author_id), + sent_at: optTs(r.sent_at), + deleted_at: optTs(r.deleted_at), + purge_at: optTs(r.purge_at), created_at: optTs(r.created_at), updated_at: optTs(r.updated_at), })); diff --git a/db/init.sql b/db/init.sql index 5f41a0f..7ec1410 100644 --- a/db/init.sql +++ b/db/init.sql @@ -680,6 +680,9 @@ CREATE TABLE IF NOT EXISTS farewell_messages ( ai_model TEXT, ai_duration_ms INTEGER, author_id INT REFERENCES users(id) ON DELETE SET NULL, + sent_at TIMESTAMPTZ, + deleted_at TIMESTAMPTZ, + purge_at TIMESTAMPTZ, created_at TIMESTAMPTZ DEFAULT now(), updated_at TIMESTAMPTZ DEFAULT now() ); diff --git a/db/migration.sql b/db/migration.sql index c985a8c..9040c5d 100644 --- a/db/migration.sql +++ b/db/migration.sql @@ -889,6 +889,9 @@ ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS ai_prompt TEXT; ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS ai_model TEXT; ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS ai_duration_ms INTEGER; ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS author_id INT; +ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS sent_at TIMESTAMPTZ; +ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ; +ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS purge_at TIMESTAMPTZ; ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ DEFAULT now(); diff --git a/lib/farewell.js b/lib/farewell.js index 64b1452..3ba6119 100644 --- a/lib/farewell.js +++ b/lib/farewell.js @@ -38,6 +38,9 @@ async function ensureFarewellTable(pool) { ai_model TEXT, ai_duration_ms INTEGER, author_id INTEGER REFERENCES users(id) ON DELETE SET NULL, + sent_at TIMESTAMPTZ, + deleted_at TIMESTAMPTZ, + purge_at TIMESTAMPTZ, created_at TIMESTAMPTZ NOT NULL DEFAULT now(), updated_at TIMESTAMPTZ NOT NULL DEFAULT now() ) @@ -54,6 +57,9 @@ async function ensureFarewellTable(pool) { await pool.query(`ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS ai_model TEXT`); await pool.query(`ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS ai_duration_ms INTEGER`); await pool.query(`ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS author_id INTEGER`); + await pool.query(`ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS sent_at TIMESTAMPTZ`); + await pool.query(`ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ`); + await pool.query(`ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS purge_at TIMESTAMPTZ`); await pool.query(`ALTER TABLE farewell_messages ADD COLUMN IF NOT EXISTS updated_at TIMESTAMPTZ NOT NULL DEFAULT now()`); await pool.query(`CREATE INDEX IF NOT EXISTS idx_farewell_resident ON farewell_messages (resident)`); await pool.query(`CREATE INDEX IF NOT EXISTS idx_farewell_pending ON farewell_messages (id) WHERE ai_status = 'pending'`); @@ -87,7 +93,7 @@ async function createFarewell(pool, data) { let templateText = null; if (templateId) { const params = [templateId]; - let sql = `SELECT text FROM farewell_messages WHERE id = $1 AND ai_status = 'done'`; + let sql = `SELECT text FROM farewell_messages WHERE id = $1 AND ai_status = 'done' AND deleted_at IS NULL`; if (!data.isAdmin) { params.push(data.authorId); sql += ` AND author_id = $${params.length}`; } const r = await pool.query(sql, params); if (!r.rows.length) { templateId = null; templateText = null; } @@ -102,19 +108,24 @@ async function createFarewell(pool, data) { return ins.rows[0]; } -async function listFarewells(pool, { isAdmin, userId, limit, offset, resident }) { +async function listFarewells(pool, { isAdmin, userId, limit, offset, resident, deleted }) { const lim = Math.min(Math.max(parseInt(limit, 10) || 20, 1), 100); const off = Math.max(parseInt(offset, 10) || 0, 0); const params = []; const cond = []; - if (!isAdmin) { params.push(userId); cond.push(`author_id = $${params.length}`); } + if (!isAdmin) { params.push(userId); cond.push(`f.author_id = $${params.length}`); } const res = String(resident || '').trim(); - if (res) { params.push('%' + res + '%'); cond.push(`resident ILIKE $${params.length}`); } - const where = cond.length ? 'WHERE ' + cond.join(' AND ') : ''; - const total = await pool.query(`SELECT count(*)::int AS n FROM farewell_messages ${where}`, params); + if (res) { params.push('%' + res + '%'); cond.push(`f.resident ILIKE $${params.length}`); } + cond.push(deleted ? 'f.deleted_at IS NOT NULL' : 'f.deleted_at IS NULL'); + const where = 'WHERE ' + cond.join(' AND '); + const total = await pool.query(`SELECT count(*)::int AS n FROM farewell_messages f ${where}`, params); params.push(lim, off); const rows = await pool.query( - `SELECT * FROM farewell_messages ${where} ORDER BY id DESC LIMIT $${params.length - 1} OFFSET $${params.length}`, + `SELECT f.*, u.name AS author_name, u.username AS author_username, u.avatar AS author_avatar + FROM farewell_messages f + LEFT JOIN users u ON u.id = f.author_id + ${where} + ORDER BY f.id DESC LIMIT $${params.length - 1} OFFSET $${params.length}`, params ); return { items: rows.rows, total: total.rows[0].n }; @@ -125,7 +136,7 @@ async function listFarewellTemplates(pool, { isAdmin, userId, resident, limit }) const res = String(resident || '').trim(); if (!res) return []; const params = ['%' + res + '%']; - let sql = `SELECT id, resident, topic, created_at, text FROM farewell_messages WHERE resident ILIKE $1 AND ai_status = 'done' AND COALESCE(text, '') <> ''`; + let sql = `SELECT id, resident, topic, created_at, text FROM farewell_messages WHERE resident ILIKE $1 AND ai_status = 'done' AND COALESCE(text, '') <> '' AND deleted_at IS NULL`; if (!isAdmin) { params.push(userId); sql += ` AND author_id = $${params.length}`; } params.push(lim); sql += ` ORDER BY id DESC LIMIT $${params.length}`; @@ -146,6 +157,50 @@ async function setFarewellStatus(pool, id, status, extra) { return r.rows[0] || null; } +async function updateFarewellText(pool, id, text) { + const r = await pool.query( + `UPDATE farewell_messages SET text = $2, updated_at = now() WHERE id = $1 AND deleted_at IS NULL RETURNING *`, + [id, text] + ); + return r.rows[0] || null; +} + +async function setFarewellSent(pool, id, sent) { + const r = await pool.query( + `UPDATE farewell_messages SET sent_at = $2, updated_at = now() WHERE id = $1 AND deleted_at IS NULL RETURNING *`, + [id, sent ? new Date() : null] + ); + return r.rows[0] || null; +} + +async function farewellForUser(pool, id, user) { + const row = await findFarewell(pool, id); + if (!row) return { notFound: true }; + if (user && user.role !== 'admin' && row.author_id !== user.id) return { forbidden: true }; + return { row }; +} + +async function deleteFarewell(pool, id, days) { + const r = await pool.query( + `UPDATE farewell_messages SET deleted_at = now(), purge_at = now() + ($2 || ' days')::interval WHERE id = $1 AND deleted_at IS NULL`, + [id, String(days)] + ); + return r.rowCount > 0; +} + +async function restoreFarewell(pool, id) { + const r = await pool.query( + `UPDATE farewell_messages SET deleted_at = NULL, purge_at = NULL WHERE id = $1 AND deleted_at IS NOT NULL`, + [id] + ); + return r.rowCount > 0; +} + +async function deleteFarewellPermanent(pool, id) { + const r = await pool.query(`DELETE FROM farewell_messages WHERE id = $1 AND deleted_at IS NOT NULL`, [id]); + return r.rowCount > 0; +} + module.exports = { FAREWELL_AI_DEFAULT_PROMPT, FAREWELL_RESIDENT_MAX, @@ -157,4 +212,10 @@ module.exports = { listFarewellTemplates, findFarewell, setFarewellStatus, + updateFarewellText, + setFarewellSent, + farewellForUser, + deleteFarewell, + restoreFarewell, + deleteFarewellPermanent, }; diff --git a/public/feedback.html b/public/feedback.html index 9d45f34..f7cc48b 100644 --- a/public/feedback.html +++ b/public/feedback.html @@ -64,6 +64,13 @@ #fbPromptModal { overflow:auto; align-items:flex-start; padding:4vh 16px; } #fbPromptModal > .edit-modal { margin:auto; } #fbQuickModal { z-index:105; } + #fwQuickModal { z-index:105; } + .fw-text { position:relative; max-height:130px; overflow:hidden; } + .fw-text.open { max-height:none; } + .fw-text:not(.open)::after { content:''; position:absolute; left:0; right:0; bottom:0; height:30px; background:linear-gradient(rgba(255,255,255,0), var(--card)); pointer-events:none; } + .fw-text-more { display:none; align-self:flex-end; font-size:.72rem; padding:2px 8px; } + .fw-text-more.visible { display:inline-flex; } + .fb-ai-pop .fb-ai-sep { border-top:1px solid var(--border); margin-top:4px; padding-top:10px; } #fbImgbbModal { z-index:110; } #fwSettingsModal { z-index:105; overflow:auto; align-items:flex-start; padding:4vh 16px; } #fwSettingsModal > .edit-modal { margin:auto; } @@ -150,7 +157,9 @@