diff --git a/src/database/migrations/009-legal-slugs.sql b/src/database/migrations/009-legal-slugs.sql new file mode 100644 index 0000000..bc67d1f --- /dev/null +++ b/src/database/migrations/009-legal-slugs.sql @@ -0,0 +1,9 @@ +-- The legal pages move to the addresses platforms expect (M17) +-- +-- Discord, and most app stores, ask for a terms URL and a privacy URL. Serving +-- them under `/wiki/` works but reads badly in a form field, so they move to +-- the root. Renaming rather than recreating keeps whatever the organization +-- has already written in them. + +UPDATE wiki_pages SET slug = 'terms-of-service' WHERE slug = 'conditions-utilisation'; +UPDATE wiki_pages SET slug = 'privacy-policy' WHERE slug = 'politique-confidentialite'; diff --git a/src/web/api/wiki.js b/src/web/api/wiki.js index 99ebbb8..ccd41ae 100644 --- a/src/web/api/wiki.js +++ b/src/web/api/wiki.js @@ -12,6 +12,7 @@ import { updatePage, } from '../../database/repositories/wiki-repository.js'; import { badRequest, forbidden, notFound } from '../http.js'; +import { pagePath } from '../services/wiki-seed.js'; /** * Public wiki, written from the dashboard (M17). @@ -26,6 +27,7 @@ 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, + path: pagePath(page.slug), title: page.title, summary: page.summary, category: page.category, diff --git a/src/web/public/views/wiki.js b/src/web/public/views/wiki.js index b82c405..5979ac8 100644 --- a/src/web/public/views/wiki.js +++ b/src/web/public/views/wiki.js @@ -167,7 +167,8 @@ export function render(host, { state }) { 'div', {}, h('strong', { text: page.title }), - h('div', { class: 'muted', text: page.summary || `/wiki/${page.slug}` }), + h('div', { class: 'muted', text: page.summary || page.path }), + h('div', { class: 'muted', style: { fontFamily: 'var(--mono)' }, text: page.path }), ), }, { label: 'Catégorie', render: (page) => badge(page.category, 'info') }, @@ -196,7 +197,7 @@ export function render(host, { state }) { page.published && h('a', { class: 'btn btn-ghost btn-sm', - href: `/wiki/${page.slug}`, + href: page.path, target: '_blank', rel: 'noreferrer', text: 'Voir', diff --git a/src/web/public/wiki.js b/src/web/public/wiki.js index 88ae525..242e297 100644 --- a/src/web/public/wiki.js +++ b/src/web/public/wiki.js @@ -17,12 +17,16 @@ const api = async (path) => { }; const currentSlug = () => { - const path = window.location.pathname.replace(/^\/wiki\/?/, '').replace(/\/$/, ''); + // A page answers either at `/wiki/` or, for the legal ones, at + // `/`; the last segment is the slug in both cases. + const path = window.location.pathname.replace(/^\/wiki\/?/, '').replace(/^\/|\/$/g, ''); return decodeURIComponent(path) || null; }; -function navigate(slug, { replace = false } = {}) { - const url = slug ? `/wiki/${encodeURIComponent(slug)}` : '/wiki'; +const pathOf = (page) => page?.path ?? (page?.slug ? `/wiki/${encodeURIComponent(page.slug)}` : '/wiki'); + +function navigate(target, { replace = false } = {}) { + const url = typeof target === 'string' ? target : pathOf(target); if (replace) window.history.replaceState({}, '', url); else window.history.pushState({}, '', url); draw(); @@ -55,13 +59,13 @@ function sidebar() { ? h('p', { class: 'muted', text: 'Aucun résultat.' }) : found.map((page) => h('a', { - href: `/wiki/${page.slug}`, + href: pathOf(page), text: page.title, onClick: (event) => { event.preventDefault(); search.value = ''; mount(results); - navigate(page.slug); + navigate(page); }, }), ), @@ -90,12 +94,12 @@ function sidebar() { h('div', { class: 'nav-group', text: category.name }), category.pages.map((page) => h('a', { - href: `/wiki/${page.slug}`, + href: pathOf(page), text: page.title, class: page.slug === state.slug ? 'active' : '', onClick: (event) => { event.preventDefault(); - navigate(page.slug); + navigate(page); }, }), ), diff --git a/src/web/server.js b/src/web/server.js index 2dc855c..f56c0c9 100644 --- a/src/web/server.js +++ b/src/web/server.js @@ -11,7 +11,7 @@ import { authorize } from './auth/guard.js'; import { broadcast, closeEventStreams, topicForPath } from './events.js'; import { HttpError, readJsonBody, readRawBody, sendEmpty, sendJson, sendStatic } from './http.js'; import { createRouter } from './router.js'; -import { seedWiki } from './services/wiki-seed.js'; +import { ROOT_PAGES, seedWiki } from './services/wiki-seed.js'; /** HTTP server of the dashboard (M16-T01). */ @@ -73,8 +73,13 @@ export function createHandler({ client = null, setupOnly = false, startedAt = Da const isApi = pathname.startsWith('/api/') || pathname.startsWith('/auth/'); if (!isApi) { // The wiki is a separate public page: it must never fall back to the - // dashboard shell, which would send a visitor to a login screen. - const fallback = pathname === '/wiki' || pathname.startsWith('/wiki/') ? 'wiki.html' : 'index.html'; + // dashboard shell, which would send a visitor to a login screen. The + // legal pages answer at the root, where platforms expect them. + const isWiki = + pathname === '/wiki' || + pathname.startsWith('/wiki/') || + ROOT_PAGES.includes(pathname.replace(/^\/|\/$/g, '')); + const fallback = isWiki ? 'wiki.html' : 'index.html'; const served = await sendStatic(response, publicDir, pathname, { fallback, request }); if (!served) sendEmpty(response, 404); return; diff --git a/src/web/services/wiki-seed.js b/src/web/services/wiki-seed.js index 0368d2d..e6a04b7 100644 --- a/src/web/services/wiki-seed.js +++ b/src/web/services/wiki-seed.js @@ -15,6 +15,17 @@ import { countPages, createPage, getPageBySlug } from '../../database/repositori const log = createLogger('wiki'); +/** + * Pages served at the root rather than under `/wiki/`. + * + * Discord, and most platforms, ask for a terms URL and a privacy URL; pasting + * `/wiki/...` in those fields reads like an afterthought. + */ +export const ROOT_PAGES = Object.freeze(['terms-of-service', 'privacy-policy']); + +/** Public address of a page, root pages included. */ +export const pagePath = (slug) => (ROOT_PAGES.includes(slug) ? `/${slug}` : `/wiki/${slug}`); + const TERMS = `## 1. Objet Ces conditions régissent l'utilisation du bot Discord **FWS-Bot** et de son tableau de bord, exploités par **[NOM DE L'ENTITÉ RESPONSABLE]** (ci-après « nous »). @@ -185,8 +196,8 @@ Ces pages sont rédigées et publiées depuis le tableau de bord du bot : elles ## Pour commencer -- **Conditions d'utilisation** — ce à quoi tu t'engages en utilisant le bot. -- **Politique de confidentialité** — quelles données sont traitées, pourquoi, et tes droits. +- [**Conditions d'utilisation**](/terms-of-service) — ce à quoi tu t'engages en utilisant le bot. +- [**Politique de confidentialité**](/privacy-policy) — quelles données sont traitées, pourquoi, et tes droits. ## Contribuer @@ -206,7 +217,7 @@ const DEFAULT_PAGES = [ isProtected: false, }, { - slug: 'conditions-utilisation', + slug: 'terms-of-service', title: 'Conditions d’utilisation', summary: 'Ce à quoi tu t’engages en utilisant le bot FWS.', category: 'Légal', @@ -216,7 +227,7 @@ const DEFAULT_PAGES = [ isProtected: true, }, { - slug: 'politique-confidentialite', + slug: 'privacy-policy', title: 'Politique de confidentialité', summary: 'Données traitées, durées de conservation et droits, selon la LPD suisse.', category: 'Légal', diff --git a/tests/web-wiki.test.js b/tests/web-wiki.test.js index 25f994b..bede6b8 100644 --- a/tests/web-wiki.test.js +++ b/tests/web-wiki.test.js @@ -127,19 +127,19 @@ describe('wiki seeding', () => { it('creates the welcome page and both legal pages', () => { assert.equal(seedWiki(), 3); - assert.ok(getPageBySlug('conditions-utilisation').published); - assert.ok(getPageBySlug('politique-confidentialite').published); + assert.ok(getPageBySlug('terms-of-service').published); + assert.ok(getPageBySlug('privacy-policy').published); }); it('marks the legal pages as protected', () => { seedWiki(); - assert.equal(getPageBySlug('politique-confidentialite').protected, true); + assert.equal(getPageBySlug('privacy-policy').protected, true); assert.equal(getPageBySlug('accueil').protected, false); }); it('names the Swiss law it is written against', () => { seedWiki(); - const privacy = getPageBySlug('politique-confidentialite').content; + const privacy = getPageBySlug('privacy-policy').content; assert.match(privacy, /LPD/); assert.match(privacy, /PFPDT/); assert.match(privacy, /art\. 25 LPD/); @@ -147,18 +147,18 @@ describe('wiki seeding', () => { it('never overwrites what the organisation has written', () => { seedWiki(); - updatePage(getPageBySlug('conditions-utilisation').id, { content: 'Notre propre texte.' }); + updatePage(getPageBySlug('terms-of-service').id, { content: 'Notre propre texte.' }); assert.equal(seedWiki(), 0); - assert.equal(getPageBySlug('conditions-utilisation').content, 'Notre propre texte.'); + assert.equal(getPageBySlug('terms-of-service').content, 'Notre propre texte.'); }); it('brings a deleted legal page back, but not the welcome one', () => { seedWiki(); - getDatabase().exec("DELETE FROM wiki_pages WHERE slug IN ('politique-confidentialite', 'accueil')"); + getDatabase().exec("DELETE FROM wiki_pages WHERE slug IN ('privacy-policy', 'accueil')"); assert.equal(seedWiki(), 1); - assert.ok(getPageBySlug('politique-confidentialite')); + assert.ok(getPageBySlug('privacy-policy')); assert.equal(getPageBySlug('accueil'), null); }); }); @@ -188,7 +188,7 @@ describe('wiki over HTTP', () => { }); it('serves a published page without a session', async () => { - const response = await get('/api/wiki/pages/conditions-utilisation'); + const response = await get('/api/wiki/pages/terms-of-service'); assert.equal(response.status, 200); assert.match((await response.json()).content, /droit suisse/); }); @@ -203,12 +203,37 @@ describe('wiki over HTTP', () => { assert.ok(Array.isArray(body.results)); }); + it('serves the legal pages at the root, where platforms expect them', async () => { + for (const path of ['/terms-of-service', '/privacy-policy']) { + const response = await get(path); + assert.equal(response.status, 200, `${path} ne répond pas`); + assert.match(await response.text(), /wiki\.js/, `${path} ne sert pas le site public`); + } + }); + + it('tells each page where it lives', async () => { + const body = await get('/api/wiki').then((response) => response.json()); + const pages = body.categories.flatMap((category) => category.pages); + + assert.equal(pages.find((page) => page.slug === 'terms-of-service').path, '/terms-of-service'); + assert.equal(pages.find((page) => page.slug === 'privacy-policy').path, '/privacy-policy'); + assert.equal(pages.find((page) => page.slug === 'accueil').path, '/wiki/accueil'); + }); + + it('keeps an ordinary page under /wiki', async () => { + createPage({ slug: 'guide', title: 'Guide', content: 'x', published: true }); + const response = await get('/guide'); + + // Not a wiki root page: it falls back to the dashboard shell. + assert.doesNotMatch(await response.text(), /wiki\.js/); + }); + it('keeps the administration behind the login', async () => { assert.equal((await get('/api/wiki/admin')).status, 401); }); it('serves its own page rather than the dashboard shell', async () => { - const body = await get('/wiki/conditions-utilisation').then((response) => response.text()); + const body = await get('/wiki/terms-of-service').then((response) => response.text()); assert.match(body, /wiki\.js/); assert.doesNotMatch(body, /id="app"/); });