import { and, desc, eq, sql } from 'drizzle-orm'; import { db, tables } from './db/index.ts'; import { buildManuscriptJson, type ManuscriptJson } from './export.ts'; import { importManuscript, parseManuscriptJson, type ImportResult, type StoredImageData } from './import.ts'; import { RecoveryError, requireRecoveryProject } from './item-history.ts'; import { readImage } from './uploads.ts'; export type CheckpointSource = 'manual' | 'structure'; export type CheckpointSummary = { id: string; label: string; source: CheckpointSource; createdAt: Date; }; export type CheckpointContent = CheckpointSummary & { manuscript: ManuscriptJson }; // Must run inside the structural writer's transaction, before the first change. export function recordProjectCheckpoint( projectId: string, label: string, source: CheckpointSource = 'structure' ): string { requireRecoveryProject(projectId); const trimmed = label.trim(); if (!trimmed) throw new RecoveryError('A project version needs a name.'); if (source !== 'manual' && source !== 'structure') { throw new RecoveryError('Unknown project version source.'); } const manuscript = buildManuscriptJson(projectId, { includeCheckpoints: false }); const id = crypto.randomUUID(); db.insert(tables.projectCheckpoint) .values({ id, projectId, label: trimmed, source, manuscript, createdAt: new Date() }) .run(); return id; } export function takeProjectCheckpoint( projectId: string, label: string, source: CheckpointSource = 'manual' ): string { return db.transaction(() => recordProjectCheckpoint(projectId, label, source)); } export function listCheckpoints(projectId: string): CheckpointSummary[] { requireRecoveryProject(projectId); return db .select({ id: tables.projectCheckpoint.id, label: tables.projectCheckpoint.label, source: tables.projectCheckpoint.source, createdAt: tables.projectCheckpoint.createdAt }) .from(tables.projectCheckpoint) .where(eq(tables.projectCheckpoint.projectId, projectId)) .orderBy(desc(tables.projectCheckpoint.createdAt), desc(sql`${tables.projectCheckpoint}.rowid`)) .all(); } export function loadCheckpoint(projectId: string, checkpointId: string): CheckpointContent { requireRecoveryProject(projectId); const row = db .select() .from(tables.projectCheckpoint) .where( and( eq(tables.projectCheckpoint.projectId, projectId), eq(tables.projectCheckpoint.id, checkpointId) ) ) .get(); if (!row) throw new RecoveryError('No such project version.', 'not_found'); let manuscript: ManuscriptJson; try { manuscript = parseManuscriptJson(JSON.stringify(row.manuscript)); } catch { throw new RecoveryError('That project version does not contain a readable manuscript.'); } return { id: row.id, label: row.label, source: row.source, createdAt: row.createdAt, manuscript }; } /** * The picture facts a project copy carries. A copy shares the image files with the * project it came from, so it keeps the same ids and takes the media type from * the bytes on disk, never from the row. Keys are the note ids the manuscript * names. */ function storedImagesForCopy(projectId: string): Map { const rows = db .select({ noteId: tables.noteImage.noteId, imageId: tables.noteImage.imageId, width: tables.noteImage.width, height: tables.noteImage.height, alt: tables.noteImage.alt, originalName: tables.noteImage.originalName }) .from(tables.noteImage) .innerJoin(tables.note, eq(tables.noteImage.noteId, tables.note.id)) .where(eq(tables.note.projectId, projectId)) .all(); const facts = new Map(); for (const row of rows) { const imageId = row.imageId; const display = imageId ? readImage(imageId, 'display') : null; const original = imageId && display ? readImage(imageId, 'original') : null; facts.set(row.noteId, { imageId: display ? imageId : null, mime: display ? (original?.mime ?? display.mime) : '', bytes: original ? original.bytes.byteLength : 0, width: row.width, height: row.height, alt: row.alt, originalName: row.originalName }); } return facts; } export function copyProject( ownerId: string, projectId: string, title: string, checkpointId?: string ): ImportResult { if (!ownerId.trim()) throw new RecoveryError('A project copy needs an owner.'); const trimmed = title.trim(); if (!trimmed) throw new RecoveryError('A project copy needs a name.'); requireRecoveryProject(projectId); const manuscript = checkpointId !== undefined ? loadCheckpoint(projectId, checkpointId).manuscript : buildManuscriptJson(projectId); const copy = structuredClone(manuscript); copy.project.title = trimmed; return importManuscript(ownerId, copy, { storedImages: storedImagesForCopy(projectId) }); }