import { query, tx } from './db'; import { ensureAnalysisSchema } from './analysis-schema'; import { HttpError } from './api'; import { BLOCK_TYPES, LINK_KINDS, Block, PageBlock } from './blocks'; const bad = (msg: string): never => { throw new HttpError(400, msg); }; const PAGE = /^(paper\/[^/\s]+\/[^/\s]+|content\/[a-z0-9][a-z0-9-]*)$/; export function checkPage(page: unknown): string { if (typeof page !== 'string' || !PAGE.test(page)) bad('page must be "paper//" (a paper) or "content/" (an analysis page), e.g. paper/acl2026/2026.acl-long.139'); return page as string; } const isStr = (x: unknown) => typeof x === 'string'; const strs = (x: unknown) => Array.isArray(x) && x.every(isStr); /** Validate one block; throws HttpError(400) with a message naming the problem. */ export function validateBlock(type: unknown, data: any, at = 'block'): void { if (!isStr(type) || !BLOCK_TYPES.includes(type as string)) bad(`${at}: unknown block type ${JSON.stringify(type)} (one of ${BLOCK_TYPES.join(', ')})`); if (!data || typeof data !== 'object' || Array.isArray(data)) bad(`${at}: data must be an object`); const noHeading = (t: string) => { if (/^\s*#{1,6}\s/m.test(t)) bad(`${at}: no headings in blocks (a block is a story, not a section); remove the "#" line`); }; switch (type) { case 'markdown': if (!isStr(data.text) || !data.text.trim()) bad(`${at}: markdown needs text`); noHeading(data.text); break; case 'list': if (!strs(data.items) || !data.items.length) bad(`${at}: list needs items (strings)`); if (data.lead !== undefined && !isStr(data.lead)) bad(`${at}: lead must be a string`); break; case 'callout': if (!isStr(data.text) || !data.text.trim()) bad(`${at}: callout needs text`); if (data.tone && !['info', 'note', 'warn'].includes(data.tone)) bad(`${at}: tone is info, note or warn`); break; case 'table': if (!strs(data.columns) || !Array.isArray(data.rows) || !data.rows.every((r: unknown) => strs(r) && (r as string[]).length === data.columns.length)) bad(`${at}: table rows must be string arrays as long as columns`); break; case 'chart': if (!isStr(data.title) || !strs(data.labels) || !Array.isArray(data.series) || !data.series.length || !data.series.every((s: any) => isStr(s.name) && Array.isArray(s.values) && s.values.length === data.labels.length && s.values.every((v: unknown) => typeof v === 'number'))) bad(`${at}: chart needs title, labels[] and series[{name, values[]}] with one number per label`); break; case 'mermaid': if (!isStr(data.code) || !data.code.trim()) bad(`${at}: mermaid needs code`); break; case 'figure': if (!isStr(data.src) || !isStr(data.alt)) bad(`${at}: figure needs src and alt`); break; case 'code': if (!isStr(data.text)) bad(`${at}: code needs text`); break; case 'links': if (!Array.isArray(data.items) || !data.items.length || !data.items.every((l: any) => LINK_KINDS.includes(l?.kind) && isStr(l.title) && isStr(l.url))) bad(`${at}: links needs items[{kind, title, url, note?}], kind one of ${LINK_KINDS.join(', ')}`); break; case 'stats': if (!isStr(data.node) || !Array.isArray(data.items) || !data.items.every((i: any) => isStr(i.label) && (i.metric !== undefined || i.value !== undefined))) bad(`${at}: stats needs node and items[{label, metric? | value?}]`); break; case 'tool-table': break; case 'paper-table': if (!isStr(data.node)) bad(`${at}: paper-table needs node`); break; case 'benchmark-table': if (!isStr(data.node) || !['areas', 'papers'].includes(data.scope)) bad(`${at}: benchmark-table needs node and scope ("areas" or "papers")`); break; } } interface Row { id: number; position: number; type: string; data: any; revision: number; updated_at: Date } const toBlock = (r: Row): PageBlock => ({ id: r.id, position: r.position, revision: r.revision, updatedAt: new Date(r.updated_at).toISOString().slice(0, 10), block: { type: r.type, data: r.data } as Block }); /** The blocks of a page in order (empty if none). */ export async function getBlocks(page: string): Promise { await ensureAnalysisSchema(); return (await query('SELECT id, position, type, data, revision, updated_at FROM analysis.page_block WHERE page = ? ORDER BY position, id', [checkPage(page)])).map(toBlock); } /** Pages that have blocks, with counts. */ export async function listPages() { await ensureAnalysisSchema(); return query<{ page: string; blocks: number; updated_at: Date }>('SELECT page, COUNT(*) AS blocks, MAX(updated_at) AS updated_at FROM analysis.page_block GROUP BY page ORDER BY MAX(updated_at) DESC'); } /** Replace all blocks of a page (new ids). */ export async function replacePage(page: string, blocks: unknown) { checkPage(page); if (!Array.isArray(blocks) || !blocks.length) bad('send {"blocks":[{"type","data"}, ...]} with at least one block'); (blocks as any[]).forEach((b, i) => validateBlock(b?.type, b?.data, `blocks[${i}]`)); await ensureAnalysisSchema(); await tx(async (q) => { await q('SELECT pg_advisory_xact_lock(hashtext(?::text))', [page]); await q('DELETE FROM analysis.page_block WHERE page = ?', [page]); for (let i = 0; i < (blocks as any[]).length; i++) { const b = (blocks as any[])[i]; await q('INSERT INTO analysis.page_block(page, position, type, data) VALUES (?,?,?,?::jsonb)', [page, i, b.type, JSON.stringify(b.data)]); } }); return { page, blocks: await getBlocks(page) }; } /** Add one block: at the end, or right after / before another block of the same page. */ export async function addBlock(page: string, body: any) { checkPage(page); validateBlock(body?.type, body?.data); await ensureAnalysisSchema(); const id = await tx(async (q) => { await q('SELECT pg_advisory_xact_lock(hashtext(?::text))', [page]); const at = await placeAt(q, page, body); await q('UPDATE analysis.page_block SET position = position + 1 WHERE page = ? AND position >= ?', [page, at]); return (await q<{ id: number }>('INSERT INTO analysis.page_block(page, position, type, data) VALUES (?,?,?,?::jsonb) RETURNING id', [page, at, body.type, JSON.stringify(body.data)]))[0].id; }); return getBlock(id); } type Q = (sql: string, params?: unknown[]) => Promise; async function placeAt(q: Q, page: string, body: { after?: number; before?: number }): Promise { const ref = body.after ?? body.before; if (ref === undefined || ref === null) return (await q<{ n: number }>('SELECT COALESCE(MAX(position) + 1, 0) AS n FROM analysis.page_block WHERE page = ?', [page]))[0].n; const r = await q<{ position: number }>('SELECT position FROM analysis.page_block WHERE id = ? AND page = ?', [ref, page]); if (!r.length) throw new HttpError(404, `block ${ref} is not on page ${page}`); return body.after !== undefined && body.after !== null ? r[0].position + 1 : r[0].position; } export async function getBlock(id: number) { await ensureAnalysisSchema(); const r = await query('SELECT id, page, position, type, data, revision, updated_at FROM analysis.page_block WHERE id = ?', [id]); if (!r.length) throw new HttpError(404, `no block ${id}`); return { page: r[0].page, ...toBlock(r[0]) }; } /** Change a block: `data` replaces its data, `set` merges top-level keys (null removes a key), `type` changes its type, * `after` / `before` (a block id of the same page) moves it. */ export async function patchBlock(id: number, body: any) { if (!body || typeof body !== 'object') bad('body must be a JSON object'); const cur = await getBlock(id); let type: string = body.type ?? cur.block.type; let data: any = body.data ?? cur.block.data; if (body.set) { if (typeof body.set !== 'object' || Array.isArray(body.set)) bad('set must be an object of keys to change'); data = { ...data, ...body.set }; for (const k of Object.keys(body.set)) if (body.set[k] === null) delete data[k]; } validateBlock(type, data); await tx(async (q) => { await q('SELECT pg_advisory_xact_lock(hashtext(?::text))', [cur.page]); if (body.after !== undefined || body.before !== undefined) { if ((body.after ?? body.before) === id) bad('cannot move a block next to itself'); await q('UPDATE analysis.page_block SET position = -1 WHERE id = ?', [id]); const at = await placeAt(q, cur.page, body); await q('UPDATE analysis.page_block SET position = position + 1 WHERE page = ? AND position >= ?', [cur.page, at]); await q('UPDATE analysis.page_block SET position = ? WHERE id = ?', [at, id]); await renumber(q, cur.page); } await q('UPDATE analysis.page_block SET type = ?, data = ?::jsonb, revision = revision + 1, updated_at = now() WHERE id = ?', [type, JSON.stringify(data), id]); }); return getBlock(id); } async function renumber(q: Q, page: string) { await q('UPDATE analysis.page_block b SET position = r.n FROM (SELECT id, ROW_NUMBER() OVER (ORDER BY position, id) - 1 AS n FROM analysis.page_block WHERE page = ?) r WHERE b.id = r.id', [page]); } export async function deleteBlock(id: number) { const cur = await getBlock(id); await tx(async (q) => { await q('SELECT pg_advisory_xact_lock(hashtext(?::text))', [cur.page]); await q('DELETE FROM analysis.page_block WHERE id = ?', [id]); await renumber(q, cur.page); }); return { deleted: id, page: cur.page }; } /** Insert `defaults` as the blocks of `page` if it has none yet (safe against concurrent first views). */ export async function seedPage(page: string, defaults: { type: string; data: any }[]) { checkPage(page); await ensureAnalysisSchema(); await tx(async (q) => { await q('SELECT pg_advisory_xact_lock(hashtext(?::text))', [page]); if ((await q('SELECT 1 FROM analysis.page_block WHERE page = ? LIMIT 1', [page])).length) return; for (let i = 0; i < defaults.length; i++) { validateBlock(defaults[i].type, defaults[i].data, `default block ${i}`); await q('INSERT INTO analysis.page_block(page, position, type, data) VALUES (?,?,?,?::jsonb)', [page, i, defaults[i].type, JSON.stringify(defaults[i].data)]); } }); }