feat(wiki): serve a public documentation written from the panel

Pages live in SQLite and are read without a session: documentation nobody
can reach without an account is not documentation. Drafts answer 404 even
when their address is guessed, and the site never falls back to the
dashboard shell, which would send a visitor to a login screen.

The wiki ships with terms of use and a privacy policy written against the
revised Swiss LPD, describing table by table what this codebase actually
stores. Both are marked protected: they can be rewritten, not deleted, and
a deleted one comes back on the next start — a bot that processes personal
data must always be able to show its notice.

The bracketed details still have to be completed and the whole reviewed;
this is a starting point written from the schema, not legal advice.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
nocodeandClaude Opus 5 committed 2026-09-23 23:20:06 +02:00
1 parent 621f3e404b
commit c76c0b96eb
6 files changed
+601 -3

No files matched your search

+3
View File
@@ -14,6 +14,7 @@ import { registerOverviewRoutes } from './overview.js';
import { registerReleaseRoutes } from './releases.js';
import { registerRepositoryRoutes } from './repositories.js';
import { registerSettingsRoutes } from './settings.js';
import { registerWikiRoutes } from './wiki.js';
const here = path.dirname(fileURLToPath(import.meta.url));
@@ -72,6 +73,8 @@ export function registerApiRoutes(router, { setupOnly = false } = {}) {
registerAuthRoutes(router);
registerConfigurationRoutes(router);
// The public documentation stays reachable even while the bot is being set up.
registerWikiRoutes(router);
// Long-lived stream the pages listen to instead of polling (M16-T12).
router.get(
+209
View File
@@ -0,0 +1,209 @@
import { ACCESS_LEVELS } from '../../config/constants.js';
import { env } from '../../config/env.js';
import { logEvent } from '../../database/repositories/event-log-repository.js';
import {
createPage,
getPage,
getPageBySlug,
listAllPages,
listPublished,
removePage,
searchPublished,
updatePage,
} from '../../database/repositories/wiki-repository.js';
import { badRequest, forbidden, notFound } from '../http.js';
/**
* Public wiki, written from the dashboard (M17).
*
* The reading routes are deliberately open: a documentation nobody can reach
* without an account is not documentation. The writing routes sit behind the
* Dev level like the rest of the panel.
*/
const SLUG = /^[a-z0-9][a-z0-9-]{0,80}$/;
/** Shown in a listing: everything but the body, which can be long. */
const summarize = (page) => ({
slug: page.slug,
title: page.title,
summary: page.summary,
category: page.category,
position: page.position,
updatedAt: page.updated_at,
});
const full = (page) => ({ ...summarize(page), content: page.content });
const adminView = (page) => ({
id: page.id,
...full(page),
published: page.published,
protected: page.protected,
createdBy: page.created_by,
createdAt: page.created_at,
updatedBy: page.updated_by,
});
/** Categories in reading order, each with its pages. */
function tree(pages) {
const categories = [];
for (const page of pages) {
let category = categories.find((entry) => entry.name === page.category);
if (!category) categories.push((category = { name: page.category, pages: [] }));
category.pages.push(summarize(page));
}
return categories;
}
export function registerWikiRoutes(router) {
// --- Public ----------------------------------------------------------------
router.get(
'/api/wiki',
() => {
const pages = listPublished();
return {
site: {
title: 'Documentation FWS',
links: env.links,
},
categories: tree(pages),
count: pages.length,
};
},
{ public: true },
);
router.get(
'/api/wiki/pages/:slug',
({ params }) => {
const page = getPageBySlug(params.slug);
// A draft must not be readable by guessing its address.
if (!page || !page.published) throw notFound('Page introuvable.');
return full(page);
},
{ public: true },
);
router.get(
'/api/wiki/search',
({ query }) => {
const term = (query.get('q') ?? '').trim();
if (term.length < 2) return { results: [] };
return { results: searchPublished(term).map(summarize) };
},
{ public: true },
);
// --- Administration --------------------------------------------------------
router.get('/api/wiki/admin', () => ({ pages: listAllPages().map(adminView) }), {
access: ACCESS_LEVELS.STAFF,
});
router.get(
'/api/wiki/admin/:id',
({ params }) => {
const page = getPage(Number.parseInt(params.id, 10));
if (!page) throw notFound('Page introuvable.');
return adminView(page);
},
{ access: ACCESS_LEVELS.STAFF },
);
router.post(
'/api/wiki/admin',
({ body, actor }) => {
const slug = String(body.slug ?? '')
.trim()
.toLowerCase();
if (!SLUG.test(slug)) {
throw badRequest('Adresse invalide : minuscules, chiffres et tirets, 80 caractères maximum.');
}
if (getPageBySlug(slug)) throw badRequest(`Une page utilise déjà l’adresse « ${slug} ».`);
const title = String(body.title ?? '').trim();
if (title.length < 2) throw badRequest('Le titre doit faire au moins 2 caractères.');
const page = createPage({
slug,
title,
summary: body.summary ? String(body.summary) : null,
content: String(body.content ?? ''),
category: String(body.category ?? '').trim() || 'Général',
position: Number.parseInt(body.position ?? 0, 10) || 0,
published: Boolean(body.published),
createdBy: actor.userId,
});
logEvent({
guildId: env.discord.guildId || null,
type: 'web.wiki.create',
actorId: actor.userId,
payload: { slug },
});
return adminView(page);
},
{ access: ACCESS_LEVELS.DEV },
);
router.patch(
'/api/wiki/admin/:id',
({ params, body, actor }) => {
const page = getPage(Number.parseInt(params.id, 10));
if (!page) throw notFound('Page introuvable.');
const patch = {};
if (body.slug !== undefined) {
const slug = String(body.slug).trim().toLowerCase();
if (!SLUG.test(slug)) throw badRequest('Adresse invalide.');
const clash = getPageBySlug(slug);
if (clash && clash.id !== page.id) throw badRequest(`Une page utilise déjà l’adresse « ${slug} ».`);
patch.slug = slug;
}
if (body.title !== undefined) {
const title = String(body.title).trim();
if (title.length < 2) throw badRequest('Le titre doit faire au moins 2 caractères.');
patch.title = title;
}
for (const key of ['summary', 'content', 'category']) {
if (body[key] !== undefined) patch[key] = String(body[key]);
}
if (body.position !== undefined) patch.position = body.position;
if (body.published !== undefined) patch.published = Boolean(body.published);
if (Object.keys(patch).length === 0) throw badRequest('Aucun champ modifiable fourni.');
const updated = updatePage(page.id, patch, actor.userId);
logEvent({
guildId: env.discord.guildId || null,
type: 'web.wiki.update',
actorId: actor.userId,
payload: { slug: updated.slug, fields: Object.keys(patch) },
});
return adminView(updated);
},
{ access: ACCESS_LEVELS.DEV },
);
router.delete(
'/api/wiki/admin/:id',
({ params, actor }) => {
const page = getPage(Number.parseInt(params.id, 10));
if (!page) throw notFound('Page introuvable.');
if (page.protected) {
throw forbidden('Cette page est protégée : elle peut être réécrite, mais pas supprimée.');
}
removePage(page.id);
logEvent({
guildId: env.discord.guildId || null,
type: 'web.wiki.delete',
actorId: actor.userId,
payload: { slug: page.slug },
});
return { ok: true };
},
{ access: ACCESS_LEVELS.OWNER },
);
}