docs: вложения статей (doc_files), подписи и библиотека картинок, быстрое ред. отзыва

This commit is contained in:
dev
2026-11-01 11:06:23 +03:00
parent a6f5953c61
commit 9a509fa742
12 changed files with 947 additions and 25 deletions
+176 -1
View File
@@ -407,11 +407,25 @@ async function ensureDocsTables(pool) {
id SERIAL PRIMARY KEY,
article_id INT REFERENCES doc_articles(id) ON DELETE CASCADE,
path TEXT NOT NULL,
caption TEXT,
uploaded_by INT REFERENCES users(id) ON DELETE SET NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
)`);
await pool.query('ALTER TABLE doc_images ADD COLUMN IF NOT EXISTS caption TEXT');
await pool.query('CREATE INDEX IF NOT EXISTS idx_doc_images_article ON doc_images(article_id)');
await pool.query('CREATE INDEX IF NOT EXISTS idx_doc_images_path ON doc_images(path)');
await pool.query(`CREATE TABLE IF NOT EXISTS doc_files (
id SERIAL PRIMARY KEY,
article_id INT REFERENCES doc_articles(id) ON DELETE CASCADE,
name TEXT NOT NULL,
path TEXT NOT NULL,
size_bytes BIGINT,
mime TEXT,
uploaded_by INT REFERENCES users(id) ON DELETE SET NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
)`);
await pool.query('CREATE INDEX IF NOT EXISTS idx_doc_files_article ON doc_files(article_id)');
await pool.query('CREATE INDEX IF NOT EXISTS idx_doc_files_path ON doc_files(path)');
const { rows } = await pool.query('SELECT 1 FROM doc_sections LIMIT 1');
if (rows.length) {
await refreshDefaultArticleBodies(pool);
@@ -810,8 +824,9 @@ async function deleteArticle(pool, user, id) {
throw httpError(403, 'Нет прав на удаление этой статьи');
}
const images = await pool.query('SELECT path FROM doc_images WHERE article_id = $1', [id]);
const files = await pool.query('SELECT path FROM doc_files WHERE article_id = $1', [id]);
await pool.query('DELETE FROM doc_articles WHERE id = $1', [id]);
return images.rows.map(r => r.path);
return images.rows.map(r => r.path).concat(files.rows.map(r => r.path));
}
async function reorderArticles(pool, sectionId, ids) {
@@ -842,6 +857,155 @@ async function registerImage(pool, { path, userId }) {
return rows[0];
}
async function listDocImages(pool, user, articleId) {
if (articleId == null) {
const { rows } = await pool.query(
`SELECT id, article_id, path, caption, uploaded_by, created_at FROM doc_images
WHERE article_id IS NULL AND ($1::boolean OR uploaded_by = $2)
ORDER BY created_at DESC`,
[user.role === 'admin', user.id]
);
return rows;
}
const { rows } = await pool.query(
`SELECT id, article_id, path, caption, uploaded_by, created_at FROM doc_images
WHERE article_id = $1 ORDER BY created_at DESC`,
[articleId]
);
return rows;
}
async function deleteDocImage(pool, user, id) {
const { rows } = await pool.query('SELECT * FROM doc_images WHERE id = $1', [reqInt(id)]);
if (!rows.length) throw httpError(404, 'Изображение не найдено');
const current = rows[0];
if (user.role !== 'admin' && current.uploaded_by !== user.id) throw httpError(403, 'Нет прав');
if (current.article_id != null) {
const article = await getArticle(pool, user, String(current.article_id));
if (!article.can_edit) throw httpError(403, 'Нет прав на редактирование статьи');
}
await pool.query('DELETE FROM doc_images WHERE id = $1', [id]);
return current.path;
}
async function updateDocImageCaption(pool, user, id, caption) {
const { rows } = await pool.query('SELECT * FROM doc_images WHERE id = $1', [reqInt(id)]);
if (!rows.length) throw httpError(404, 'Изображение не найдено');
const current = rows[0];
if (user.role !== 'admin' && current.uploaded_by !== user.id) throw httpError(403, 'Нет прав');
const cap = (caption == null ? '' : String(caption)).trim().slice(0, 300);
const newCaption = cap || null;
await pool.query('UPDATE doc_images SET caption = $1 WHERE id = $2', [newCaption, id]);
let updatedBody = null;
if (current.article_id != null) {
const article = await getArticle(pool, user, String(current.article_id));
if (article.can_edit) {
const path = current.path;
const escPath = path.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
const re = new RegExp('!\\[[^\\]]*\\]\\(' + escPath + '(?:\\s+"[^"]*")?\\)', 'g');
const replacement = () => newCaption ? '![' + newCaption + '](' + path + ')' : '![](' + path + ')';
const newBody = String(article.body || '').replace(re, replacement);
if (newBody !== article.body) {
await pool.query('UPDATE doc_articles SET body = $1, updated_at = now() WHERE id = $2', [newBody, current.article_id]);
updatedBody = newBody;
}
}
}
return { path: current.path, article_id: current.article_id, caption: newCaption, body: updatedBody };
}
async function listAllDocImages(pool, user) {
const { rows } = await pool.query(
`SELECT id, article_id, path, uploaded_by, created_at FROM doc_images ORDER BY created_at DESC`
);
return rows;
}
async function listAllDocFiles(pool, user) {
const { rows } = await pool.query(
`SELECT id, article_id, name, path, size_bytes, mime, uploaded_by, created_at FROM doc_files ORDER BY created_at DESC`
);
return rows;
}
async function registerFile(pool, { path, name, size, mime, userId, articleId = null }) {
const { rows } = await pool.query(
`INSERT INTO doc_files (article_id, name, path, size_bytes, mime, uploaded_by)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, article_id, name, path, size_bytes, mime, uploaded_by, created_at`,
[articleId, name, path, size || null, mime || null, userId]
);
return rows[0];
}
async function listDocFiles(pool, user, articleId) {
const article = await getArticle(pool, user, String(articleId));
const { rows } = await pool.query(
`SELECT id, article_id, name, path, size_bytes, mime, uploaded_by, created_at
FROM doc_files WHERE article_id = $1 ORDER BY created_at DESC`,
[article.id]
);
return rows;
}
async function listUnattachedDocFiles(pool, user) {
const { rows } = await pool.query(
`SELECT id, article_id, name, path, size_bytes, mime, uploaded_by, created_at
FROM doc_files WHERE article_id IS NULL AND ($1::boolean OR uploaded_by = $2)
ORDER BY created_at DESC`,
[user.role === 'admin', user.id]
);
return rows;
}
async function attachDocFile(pool, user, fileId, articleId) {
const id = reqInt(fileId);
const { rows: frows } = await pool.query('SELECT * FROM doc_files WHERE id = $1', [id]);
if (!frows.length) throw httpError(404, 'Файл не найден');
const file = frows[0];
if (articleId == null) {
if (user.role !== 'admin' && file.uploaded_by !== user.id) throw httpError(403, 'Нет прав');
await pool.query('UPDATE doc_files SET article_id = NULL WHERE id = $1', [id]);
return { ...file, article_id: null };
}
const article = await getArticle(pool, user, String(articleId));
if (!article.can_edit) throw httpError(403, 'Нет прав на редактирование статьи');
await pool.query('UPDATE doc_files SET article_id = $1 WHERE id = $2', [article.id, id]);
return { ...file, article_id: article.id };
}
async function replaceDocFile(pool, user, fileId, { path, name, size, mime }) {
const id = reqInt(fileId);
const { rows } = await pool.query('SELECT * FROM doc_files WHERE id = $1', [id]);
if (!rows.length) throw httpError(404, 'Файл не найден');
const current = rows[0];
if (user.role !== 'admin' && current.uploaded_by !== user.id) throw httpError(403, 'Нет прав на замену файла');
if (current.article_id != null) {
const article = await getArticle(pool, user, String(current.article_id));
if (!article.can_edit) throw httpError(403, 'Нет прав на редактирование статьи');
}
const { rows: upd } = await pool.query(
`UPDATE doc_files SET path = $1, name = $2, size_bytes = $3, mime = $4 WHERE id = $5
RETURNING id, article_id, name, path, size_bytes, mime, uploaded_by, created_at`,
[path, name, size || null, mime || null, id]
);
return { item: upd[0], oldPath: current.path };
}
async function deleteDocFile(pool, user, fileId) {
const id = reqInt(fileId);
const { rows } = await pool.query('SELECT * FROM doc_files WHERE id = $1', [id]);
if (!rows.length) throw httpError(404, 'Файл не найден');
const current = rows[0];
if (user.role !== 'admin' && current.uploaded_by !== user.id) throw httpError(403, 'Нет прав на удаление файла');
if (current.article_id != null) {
const article = await getArticle(pool, user, String(current.article_id));
if (!article.can_edit) throw httpError(403, 'Нет прав на редактирование статьи');
}
await pool.query('DELETE FROM doc_files WHERE id = $1', [id]);
return current.path;
}
async function searchArticles(pool, user, query) {
const q = String(query || '').trim();
if (!q || q.length < 2) return [];
@@ -943,6 +1107,17 @@ module.exports = {
deleteArticle,
reorderArticles,
registerImage,
listDocImages,
deleteDocImage,
updateDocImageCaption,
listAllImages: listAllDocImages,
listAllFiles: listAllDocFiles,
registerFile,
listDocFiles,
listUnattachedDocFiles,
attachDocFile,
replaceDocFile,
deleteDocFile,
searchArticles,
publicTree,
publicArticle,