| [7bc636b] | 1 | /**
|
|---|
| 2 | * Site middleware — resolve which site this request is for.
|
|---|
| [94a876f] | 3 | *
|
|---|
| 4 | * Resolution order (hub-modus):
|
|---|
| 5 | * 1. Pad /user/:slug → die site (legacy /sites/:slug → 301 naar /user/)
|
|---|
| 6 | * 2. Anders (solo, of hub-landing): de primaire/hoofd-site
|
|---|
| 7 | *
|
|---|
| [7bc636b] | 8 | * Sets res.locals.site for all downstream handlers.
|
|---|
| 9 | */
|
|---|
| 10 |
|
|---|
| 11 | import db from '../config/database.js';
|
|---|
| [6351545] | 12 | import { getTenancy } from '../services/SettingsService.js';
|
|---|
| [0beafed] | 13 | import { audioUrl } from '../services/AudioStreamService.js';
|
|---|
| [cb01666] | 14 | import { audioEnabled } from '../config/features.js';
|
|---|
| [7bc636b] | 15 |
|
|---|
| [7881080] | 16 | /**
|
|---|
| [834bcc3] | 17 | * The primary/main site — ONE source of truth (replaces the "oldest site ="
|
|---|
| 18 | * main" assumption that was previously scattered across resolveSite/hub/account/admin).
|
|---|
| 19 | * Reads the explicit is_primary flag; falls back to the oldest if it isn't set
|
|---|
| 20 | * anywhere yet, so existing behaviour is preserved exactly.
|
|---|
| [7881080] | 21 | */
|
|---|
| 22 | export function getPrimarySite() {
|
|---|
| 23 | return db.prepare('SELECT * FROM sites WHERE is_primary = 1 LIMIT 1').get()
|
|---|
| 24 | || db.prepare('SELECT * FROM sites ORDER BY created_at ASC LIMIT 1').get()
|
|---|
| 25 | || null;
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| [7bc636b] | 28 | export function resolveSite(req, res, next) {
|
|---|
| [6351545] | 29 | const tenancy = getTenancy();
|
|---|
| [834bcc3] | 30 | res.locals.tenancy = tenancy; // also available in views
|
|---|
| [7bc636b] | 31 |
|
|---|
| [834bcc3] | 32 | // In HUB mode /user/:slug maps to a specific site. In SOLO mode there is only
|
|---|
| 33 | // one site: we skip that routing and pin to the primary site.
|
|---|
| [6351545] | 34 | if (tenancy === 'hub') {
|
|---|
| [834bcc3] | 35 | // A Klonkt site is canonically reachable via /user/:slug. /sites/:slug is a
|
|---|
| 36 | // legacy alias → 301 to the canonical form so one URL scheme remains
|
|---|
| 37 | // (preserves path + query string; does NOT touch /admin/sites, which starts with /admin/).
|
|---|
| [bf61be7] | 38 | const m = req.path.match(/^\/(sites|user)\/([a-zA-Z0-9_-]+)(\/.*)?$/);
|
|---|
| [6351545] | 39 | if (m) {
|
|---|
| [f735304] | 40 | if (m[1] === 'sites') {
|
|---|
| 41 | return res.redirect(301, req.originalUrl.replace(/^\/sites\//, '/user/'));
|
|---|
| 42 | }
|
|---|
| [bf61be7] | 43 | const site = db.prepare('SELECT * FROM sites WHERE slug = ?').get(m[2]);
|
|---|
| [6351545] | 44 | if (site) {
|
|---|
| 45 | res.locals.site = site;
|
|---|
| [f735304] | 46 | req.url = (m[3] || '/'); // strip /user/:slug zodat downstream de rest ziet
|
|---|
| 47 | res.locals.siteUrlBase = `/user/${m[2]}`;
|
|---|
| [6351545] | 48 | return next();
|
|---|
| 49 | }
|
|---|
| 50 | }
|
|---|
| [834bcc3] | 51 | // (Removed: a dead "slug == hostname" subdomain hack. Slugs may not contain
|
|---|
| 52 | // dots, so it could never match. Real subdomain routing would match the
|
|---|
| 53 | // subdomain LABEL against the slug — a separate feature, not this.)
|
|---|
| [7bc636b] | 54 | }
|
|---|
| 55 |
|
|---|
| [834bcc3] | 56 | // Solo (or hub without a match): pin to the primary/main site.
|
|---|
| [7881080] | 57 | const defaultSite = getPrimarySite();
|
|---|
| [7bc636b] | 58 | if (defaultSite) {
|
|---|
| 59 | res.locals.site = defaultSite;
|
|---|
| 60 | res.locals.siteUrlBase = '';
|
|---|
| 61 | }
|
|---|
| 62 |
|
|---|
| 63 | next();
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | /**
|
|---|
| 67 | * Audio tracks loader — pulls site-level tracks for the persistent player widget.
|
|---|
| 68 | * Per Robin: player is separate from the footer, gated by site.enable_audio_player.
|
|---|
| 69 | * Returns empty array if no site or audio is disabled — shell.ejs uses the
|
|---|
| 70 | * length to decide whether to mount audio-player.js.
|
|---|
| 71 | */
|
|---|
| 72 | export function loadAudioTracks(req, res, next) {
|
|---|
| [cb01666] | 73 | if (!audioEnabled()) { res.locals.audioTracks = []; return next(); } // lite-modus
|
|---|
| [7bc636b] | 74 | const site = res.locals.site;
|
|---|
| 75 | if (!site || site.enable_audio_player === 0) {
|
|---|
| 76 | res.locals.audioTracks = [];
|
|---|
| 77 | return next();
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | try {
|
|---|
| [834bcc3] | 81 | // m.filename = the bare filename; the playable URL is the gated stream route
|
|---|
| 82 | // (audioUrl). The media table has NO url column — the old query selected
|
|---|
| 83 | // m.url and always failed silently (empty player). Now we build the URL from filename.
|
|---|
| [0beafed] | 84 | const rows = db.prepare(`
|
|---|
| 85 | SELECT t.id, t.title, t.artist, t.duration, t.position, m.filename
|
|---|
| [7bc636b] | 86 | FROM audio_tracks t
|
|---|
| 87 | LEFT JOIN media m ON m.id = t.media_id
|
|---|
| 88 | WHERE t.site_id = ?
|
|---|
| 89 | ORDER BY t.position ASC, t.created_at ASC
|
|---|
| 90 | `).all(site.id);
|
|---|
| [0beafed] | 91 | res.locals.audioTracks = rows.map((r) => ({
|
|---|
| 92 | id: r.id, title: r.title, artist: r.artist, duration: r.duration, position: r.position,
|
|---|
| 93 | media_url: r.filename ? audioUrl(r.filename) : null,
|
|---|
| 94 | }));
|
|---|
| [7bc636b] | 95 | } catch (e) {
|
|---|
| 96 | // media table might not be queryable in some test setups — fall back gracefully
|
|---|
| 97 | res.locals.audioTracks = [];
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | next();
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | /**
|
|---|
| 104 | * Theme loader — applies user/site theme preferences.
|
|---|
| 105 | */
|
|---|
| 106 | export function loadTheme(req, res, next) {
|
|---|
| [b20460e] | 107 | const PALETTES = ['klonkt','sage','paper','forest','stone','midnight','sunset','cream','rose','mint','lilac'];
|
|---|
| [7bc636b] | 108 |
|
|---|
| 109 | const user = req.session?.user;
|
|---|
| 110 | const site = res.locals.site;
|
|---|
| 111 |
|
|---|
| 112 | // Priority: user setting > site setting > default
|
|---|
| 113 | const palette = (user && PALETTES.includes(user.palette) ? user.palette : null)
|
|---|
| 114 | || (site && PALETTES.includes(site.palette) ? site.palette : null)
|
|---|
| [dd7e2a2] | 115 | || 'klonkt';
|
|---|
| [7bc636b] | 116 |
|
|---|
| 117 | res.locals.palette = palette;
|
|---|
| 118 | res.locals.theme = (user && ['dark','light'].includes(user.theme)) ? user.theme : 'dark';
|
|---|
| 119 |
|
|---|
| 120 | next();
|
|---|
| 121 | }
|
|---|