Changeset 72ec6a4 in Klonkt for src/routes
- Timestamp:
- 07/31/2026 06:55:59 PM (6 weeks ago)
- Branches:
- main
- Children:
- 17546af
- Parents:
- bfa6fa1
- Location:
- src/routes
- Files:
-
- 7 edited
-
admin-settings.js (modified) (6 diffs)
-
admin.js (modified) (5 diffs)
-
audio.js (modified) (2 diffs)
-
download.js (modified) (1 diff)
-
guardian.js (modified) (2 diffs)
-
posts.js (modified) (4 diffs)
-
search.js (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/routes/admin-settings.js
rbfa6fa1 r72ec6a4 1 1 /** 2 2 * Admin: global settings. 3 * - tenancy mode (Solo/Hub)4 3 * - hub branding (name/tagline/intro/hero of the generic hub home page) 5 4 * … … 20 19 import { renderPage } from '../middleware/render.js'; 21 20 import { requireGod } from '../middleware/auth.js'; 22 import { get Tenancy, setTenancy, getSetting, setSetting } from '../services/SettingsService.js';21 import { getSetting, setSetting } from '../services/SettingsService.js'; 23 22 import { SUPPORTED } from '../services/i18n.js'; 24 23 import { mailerStatus, sendMail } from '../config/mailer.js'; … … 69 68 pageTitleKey: 'admin.t_settings', 70 69 bodyClass: 'on-admin', 71 tenancy: getTenancy(),72 hubTitle: getSetting('hub_title') || '',73 70 hubTagline: getSetting('hub_tagline') || '', 74 71 hubIntro: getSetting('hub_intro') || '', … … 87 84 router.post('/', requireGod, (req, res) => { 88 85 // multer.single processes multipart (hub branding form). For a plain 89 // urlencoded POST (tenancy form)multer does nothing and req.body stays intact.86 // urlencoded POST: multer does nothing and req.body stays intact. 90 87 heroUpload.single('hub_hero_file')(req, res, (err) => { 91 88 if (err) { … … 93 90 } 94 91 95 if (typeof req.body.tenancy !== 'undefined') {96 // Hub mode is removed → setTenancy only accepts solo | circle (coerces the rest).97 setTenancy(req.body.tenancy);98 }99 92 if (typeof req.body.default_lang !== 'undefined') { 100 93 // Default language for visitors (empty = follow env/browser). Validated against NL/EN/DE. … … 109 102 if (tz) { try { Intl.DateTimeFormat('en-US', { timeZone: tz }); valid = tz; } catch { valid = ''; } } 110 103 setSetting('timezone', valid); 111 }112 if (typeof req.body.hub_title !== 'undefined') {113 setSetting('hub_title', (req.body.hub_title || '').toString().slice(0, 80).trim());114 104 } 115 105 if (typeof req.body.hub_tagline !== 'undefined') { -
src/routes/admin.js
rbfa6fa1 r72ec6a4 9 9 import { renderPage } from '../middleware/render.js'; 10 10 import { requireAuth } from '../middleware/auth.js'; 11 import { getTenancy,apEnabled } from '../services/SettingsService.js';11 import { apEnabled } from '../services/SettingsService.js'; 12 12 import { getPrimarySite } from '../middleware/site.js'; 13 13 … … 17 17 // Solves the problem that drafts (status != published) were not findable anywhere: 18 18 // the timeline shows only published posts. 19 function sitePosts(siteId, siteSlug, tenancy,limit = 60) {20 const base = tenancy === 'hub' ? `/user/${siteSlug}` :'';19 function sitePosts(siteId, siteSlug, limit = 60) { 20 const base = ''; 21 21 return db.prepare(` 22 22 SELECT slug, title, status, published_at, created_at, updated_at … … 53 53 mySite, 54 54 mine, 55 posts: sitePosts(mySite.id, mySite.slug , 'hub'), // my-site is hub-only55 posts: sitePosts(mySite.id, mySite.slug), 56 56 }); 57 57 } 58 58 59 const tenancy = getTenancy(); 60 61 // The primary/main site — in solo THE site, in hub the main site. Provides the 62 // "Appearance" tile with its edit link + the posts/drafts list. 59 // THE site: it provides the "Appearance" tile with its edit link and the 60 // posts/drafts list. 63 61 const primarySite = getPrimarySite(); 64 62 … … 72 70 }; 73 71 74 // Sites/users tables are only relevant in hub mode; in solo we skip the query. 75 const sites = tenancy === 'hub' ? db.prepare(` 76 SELECT s.slug, s.title, s.created_at, u.username AS owner_username 77 FROM sites s 78 LEFT JOIN users u ON u.id = s.owner_id 79 ORDER BY s.created_at DESC 80 LIMIT 50 81 `).all() : []; 82 83 const users = tenancy === 'hub' ? db.prepare(` 84 SELECT username, email, role, created_at 85 FROM users 86 ORDER BY created_at DESC 87 LIMIT 50 88 `).all() : []; 89 90 // Posts/drafts of the primary site (in solo = the site; in hub = the admin's 91 // main site). Drafts are listed first so they are easy to find. 92 const posts = primarySite ? sitePosts(primarySite.id, primarySite.slug, tenancy) : []; 72 // Posts/drafts of the site. Drafts are listed first so they are easy to find. 73 const posts = primarySite ? sitePosts(primarySite.id, primarySite.slug) : []; 93 74 94 75 renderPage(req, res, 'pages/admin', { 95 76 pageTitleKey: 'admin.t_admin', 96 77 bodyClass: 'on-admin', 97 tenancy, 98 circlesOn: apEnabled(), // solo vs cirkels tagline (federatie aan/uit) 78 circlesOn: apEnabled(), // federatie aan/uit in de tagline 99 79 primarySite, 100 80 stats, 101 sites,102 81 posts, 103 users,104 82 }); 105 83 }); … … 111 89 pageTitleKey: 'admin.t_manual', 112 90 bodyClass: 'on-admin', 113 tenancy: getTenancy(),114 91 }); 115 92 }); -
src/routes/audio.js
rbfa6fa1 r72ec6a4 156 156 const id = String(req.params.id || ''); 157 157 if (!/^[A-Za-z0-9_-]+$/.test(id)) return res.status(400).json({ error: 'bad id' }); 158 const isHub = res.locals.tenancy === 'hub';159 158 const row = db.prepare(` 160 159 SELECT p.slug, s.slug AS site_slug … … 164 163 `).get('%[[track:' + id + ']]%'); 165 164 if (!row) return res.status(404).json({ error: 'not found' }); 166 const url = isHub ? `/user/${row.site_slug}/${row.slug}` :`/${row.slug}`;165 const url = `/${row.slug}`; 167 166 res.json({ url }); 168 167 }); -
src/routes/download.js
rbfa6fa1 r72ec6a4 34 34 ).get(site.id); 35 35 if (!post) return {}; 36 try { return postNeighbors(site, post , res.locals.tenancy === 'hub'); } catch (e) { return {}; }36 try { return postNeighbors(site, post); } catch (e) { return {}; } 37 37 } 38 38 const __dirname = path.dirname(fileURLToPath(import.meta.url)); -
src/routes/guardian.js
rbfa6fa1 r72ec6a4 10 10 */ 11 11 import express from 'express'; 12 import crypto from 'crypto';13 import bcrypt from 'bcryptjs';14 12 import path from 'path'; 15 13 import { fileURLToPath } from 'url'; … … 595 593 }); 596 594 597 // ── Losse guardians (Guardian 2): uitnodigen en aansluiten ─────────────── 598 // De familie nodigt oma uit; zij kiest naam + wachtwoord en heeft daarmee een 599 // guardian-only account: user + minimale site (guardian_only=1). Alles wat al 600 // per slug werkt (actor, inbox, offers, push, deze PWA) werkt dan meteen. 601 602 router.post('/invite', requireAuth, (req, res) => { 603 const token = crypto.randomBytes(16).toString('base64url'); 604 db.prepare('INSERT INTO ap_guardian_invites (token, created_by) VALUES (?,?)') 605 .run(token, req.session.user.id); 606 const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); 607 const url = `${base}/guardian/join/${token}`; 608 res.send(`<!doctype html><meta charset="utf-8"><body style="font-family:sans-serif;max-width:480px;margin:40px auto"> 609 <h2>Invite a guardian</h2> 610 <p>Share this link. It lets one person create a guardian account here:</p> 611 <p><a href="${url}">${url}</a></p> 612 <p><a href="/guardian">Back</a></p></body>`); 613 }); 614 615 function joinForm(token, error) { 616 return `<!doctype html><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"> 617 <body style="font-family:sans-serif;max-width:420px;margin:40px auto"> 618 <h2>Become a guardian</h2> 619 <p>Watch over someone you care about. Pick a name and a password; that is all.</p> 620 ${error ? `<p style="color:#b00">${error}</p>` : ''} 621 <form method="post" action="/guardian/join/${token}"> 622 <p><input name="name" placeholder="your name (grandma)" required pattern="[a-z0-9_-]{1,32}" 623 style="width:100%;padding:10px" autocapitalize="none"></p> 624 <p><input name="password" type="password" placeholder="password" required minlength="8" 625 style="width:100%;padding:10px"></p> 626 <p><button style="width:100%;padding:12px">Create my guardian account</button></p> 627 </form></body>`; 628 } 629 630 router.get('/join/:token', (req, res) => { 631 const inv = db.prepare('SELECT * FROM ap_guardian_invites WHERE token = ? AND used_at IS NULL') 632 .get(req.params.token); 633 if (!inv) return res.status(404).send('This invite is no longer valid.'); 634 res.send(joinForm(req.params.token)); 635 }); 636 637 router.post('/join/:token', express.urlencoded({ extended: false }), (req, res) => { 638 const inv = db.prepare('SELECT * FROM ap_guardian_invites WHERE token = ? AND used_at IS NULL') 639 .get(req.params.token); 640 if (!inv) return res.status(404).send('This invite is no longer valid.'); 641 const name = String(req.body.name || '').trim().toLowerCase(); 642 const password = String(req.body.password || ''); 643 if (!/^[a-z0-9_-]{1,32}$/.test(name)) return res.status(400).send(joinForm(req.params.token, 'Only lowercase letters, digits, - and _.')); 644 if (password.length < 8) return res.status(400).send(joinForm(req.params.token, 'Password: at least 8 characters.')); 645 if (db.prepare('SELECT 1 FROM sites WHERE slug = ?').get(name) || db.prepare('SELECT 1 FROM users WHERE username = ?').get(name)) { 646 return res.status(409).send(joinForm(req.params.token, 'That name is taken, pick another.')); 647 } 648 const userId = crypto.randomUUID(); 649 db.prepare('INSERT INTO users (id, username, email, password_hash, role) VALUES (?,?,?,?,?)') 650 .run(userId, name, `${name}@guardian.invalid`, bcrypt.hashSync(password, 10), 'member'); 651 db.prepare('INSERT INTO sites (id, slug, title, owner_id, is_primary, guardian_only) VALUES (?,?,?,?,0,1)') 652 .run(crypto.randomUUID(), name, name, userId); 653 db.prepare('UPDATE ap_guardian_invites SET used_by = ?, used_at = CURRENT_TIMESTAMP WHERE token = ?') 654 .run(userId, req.params.token); 655 req.session.user = { id: userId, username: name, role: 'member' }; 656 res.redirect('/guardian'); 657 }); 595 // Losse guardian-accounts (guardian-lite: /invite + /join, user + site met 596 // guardian_only=1) zijn verwijderd op 31-7-2026. Een instance is een eigenaar; 597 // zo'n account was de laatste multi-user-rest en zette bovendien andermans 598 // wachtwoordhash, sessie en PRIVATE actor-sleutel in jouw database, wat een 599 // verhuizing (shaer-qw6q) onmogelijk netjes maakte. Een guardian hoort een 600 // eigen Klonkt te hebben; de adoptie loopt dan gewoon over de federatie. 658 601 659 602 export default router; -
src/routes/posts.js
rbfa6fa1 r72ec6a4 763 763 } 764 764 765 function postNeighbors(site, post, isHub) { 766 const urlBaseFor = (p) => (isHub && p && p.site_slug) ? `/user/${p.site_slug}` : ''; 767 const ordered = isHub 768 ? db.prepare(` 769 SELECT p.id, p.slug, p.title, p.pinned, s.slug AS site_slug 770 FROM posts p JOIN sites s ON s.id = p.site_id 771 WHERE p.status = 'published' 772 ORDER BY p.published_at DESC 773 `).all() 774 : db.prepare(` 775 SELECT id, slug, title, pinned FROM posts 776 WHERE site_id = ? AND status = 'published' 777 ORDER BY (pinned = 0) ASC, pinned ASC, published_at DESC 778 `).all(site.id); 765 function postNeighbors(site, post) { 766 const ordered = db.prepare(` 767 SELECT id, slug, title, pinned FROM posts 768 WHERE site_id = ? AND status = 'published' 769 ORDER BY (pinned = 0) ASC, pinned ASC, published_at DESC 770 `).all(site.id); 779 771 const idx = ordered.findIndex((p) => p.id === post.id); 780 772 const newerPost = idx > 0 ? ordered[idx - 1] : null; 781 773 const olderPost = (idx >= 0 && idx < ordered.length - 1) ? ordered[idx + 1] : null; 782 if (newerPost) newerPost._urlBase = urlBaseFor(newerPost);783 if (olderPost) olderPost._urlBase = urlBaseFor(olderPost);774 if (newerPost) newerPost._urlBase = ''; 775 if (olderPost) olderPost._urlBase = ''; 784 776 return { newerPost, olderPost }; 785 777 } … … 1344 1336 const _unlocked = _u && _u.purpose === 'unlocked' && _u.siteId === site.id && String(_u.post) === String(post.slug); 1345 1337 if (post.paid && !canEditThis && !_unlocked) { 1346 const { newerPost, olderPost } = postNeighbors(site, post , res.locals.tenancy === 'hub');1338 const { newerPost, olderPost } = postNeighbors(site, post); 1347 1339 return renderPage(req, res, 'pages/paid-gate', { 1348 1340 pageTitle: post.title || 'Voor supporters', … … 1364 1356 // Same Newer/Older navigation as on a normal post, so the visitor doesn't get 1365 1357 // stuck on the fan gate but can keep browsing. 1366 const { newerPost, olderPost } = postNeighbors(site, post , res.locals.tenancy === 'hub');1358 const { newerPost, olderPost } = postNeighbors(site, post); 1367 1359 return renderPage(req, res, 'pages/fan-gate', { 1368 1360 pageTitle: post.title || 'Alleen voor fans', … … 1396 1388 // Prev / next chronological (kept for back-compat — "post-nav" feature 1397 1389 // below the article still uses these as a simple linear navigation). 1398 // Hub mode: Related posts + Newer/Older pull from ALL users (all sites), 1399 // newest first. Solo mode: within the current site (old behaviour). 1400 const isHub = res.locals.tenancy === 'hub'; 1401 // Per-post URL base: in hub a link points to /user/<site-slug>/<post-slug>. 1402 const urlBaseFor = (p) => (isHub && p && p.site_slug) ? `/user/${p.site_slug}` : ''; 1390 const urlBaseFor = () => ''; 1403 1391 1404 1392 // Newer/Older across ALL posts (shared helper — also used by the fan gate). 1405 const { newerPost, olderPost } = postNeighbors(site, post , isHub);1393 const { newerPost, olderPost } = postNeighbors(site, post); 1406 1394 1407 1395 // ── Related posts: same-tag matching with recency fallback ───── 1408 1396 // Fetch ~50 candidates, score by tag overlap, take top 3. 1409 1397 // Excluding self via `id != ?`. 1410 const candidates = isHub 1411 ? db.prepare(` 1412 SELECT p.id, p.slug, p.title, p.cover_image_url, p.cover_video_url, p.published_at, p.tags, p.nsfw, p.content_warning, s.slug AS site_slug 1413 FROM posts p JOIN sites s ON s.id = p.site_id 1414 WHERE p.status = 'published' AND p.id != ? 1415 ORDER BY p.published_at DESC LIMIT 50 1416 `).all(post.id) 1417 : db.prepare(` 1418 SELECT id, slug, title, cover_image_url, cover_video_url, published_at, tags, nsfw, content_warning 1419 FROM posts 1420 WHERE site_id = ? AND status = 'published' AND id != ? 1421 ORDER BY published_at DESC LIMIT 50 1422 `).all(site.id, post.id); 1398 const candidates = db.prepare(` 1399 SELECT id, slug, title, cover_image_url, cover_video_url, published_at, tags, nsfw, content_warning 1400 FROM posts 1401 WHERE site_id = ? AND status = 'published' AND id != ? 1402 ORDER BY published_at DESC LIMIT 50 1403 `).all(site.id, post.id); 1423 1404 1424 1405 // Parse tags JSON safely; missing/malformed → empty array. -
src/routes/search.js
rbfa6fa1 r72ec6a4 51 51 function searchSite(req, res, rawQ, lim) { 52 52 const site = res.locals.site; 53 const isHub = res.locals.tenancy === 'hub';54 53 const base = res.locals.siteUrlBase || ''; 55 const urlFor = (slug) => (isHub ? `/user/${site.slug}/${slug}` : `/${slug}`);54 const urlFor = (slug) => `/${slug}`; 56 55 // Eigen vertaler (werkt ook in de JSON-route, waar res.locals.t niet bestaat). 57 56 const lang = resolveLang(req); … … 172 171 if (rawQ.length < 2) return res.json({ posts: [], tracks: [], events: [], pages: [] }); 173 172 174 const isHub = res.locals.tenancy === 'hub'; 175 const urlFor = (slug) => (isHub ? `/user/${site.slug}/${slug}` : `/${slug}`); 173 const urlFor = (slug) => `/${slug}`; 176 174 const r = searchSite(req, res, rawQ, { posts: 5, tracks: 4, events: 3, pages: 4 }); 177 175 res.json({
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)