| [bf61be7] | 1 | /**
|
|---|
| [834bcc3] | 2 | * Hub home page — hub mode only. Instead of rendering the primary Klonkt site,
|
|---|
| 3 | * '/' renders a company overview here: the latest posts from ALL users combined
|
|---|
| 4 | * + a list of the Klonkt sites.
|
|---|
| [bf61be7] | 5 | *
|
|---|
| [834bcc3] | 6 | * In solo mode this does nothing (next()) and posts.js renders the single site.
|
|---|
| [bf61be7] | 7 | */
|
|---|
| 8 |
|
|---|
| 9 | import express from 'express';
|
|---|
| 10 | import db from '../config/database.js';
|
|---|
| 11 | import { renderPage } from '../middleware/render.js';
|
|---|
| [6e0249f] | 12 | import { getTenancy, getSetting } from '../services/SettingsService.js';
|
|---|
| [bf61be7] | 13 |
|
|---|
| 14 | const router = express.Router();
|
|---|
| 15 |
|
|---|
| 16 | router.get('/', (req, res, next) => {
|
|---|
| 17 | if (getTenancy() !== 'hub') return next();
|
|---|
| [834bcc3] | 18 | // If resolveSite addressed a specific site (/user/:slug or /sites/:slug),
|
|---|
| 19 | // req.url was rewritten to '/' — do NOT show the overview but let posts.js
|
|---|
| 20 | // render the site itself. siteUrlBase is set in that case.
|
|---|
| [743bb81] | 21 | if (res.locals.siteUrlBase) return next();
|
|---|
| [bf61be7] | 22 |
|
|---|
| [834bcc3] | 23 | // Latest published posts across all sites.
|
|---|
| [bf61be7] | 24 | const posts = db.prepare(`
|
|---|
| 25 | SELECT p.title, p.slug, p.excerpt, p.published_at, p.created_at,
|
|---|
| 26 | p.cover_image_url, p.type,
|
|---|
| 27 | s.slug AS site_slug, s.title AS site_title, s.profile_photo AS site_photo,
|
|---|
| 28 | u.username AS author_username
|
|---|
| 29 | FROM posts p
|
|---|
| 30 | JOIN sites s ON s.id = p.site_id
|
|---|
| 31 | LEFT JOIN users u ON u.id = p.author_id
|
|---|
| 32 | WHERE p.status = 'published'
|
|---|
| 33 | ORDER BY COALESCE(p.published_at, p.created_at) DESC
|
|---|
| 34 | LIMIT 24
|
|---|
| 35 | `).all();
|
|---|
| 36 |
|
|---|
| [834bcc3] | 37 | // The main/label site (the explicitly primary = the company/main account) is
|
|---|
| 38 | // NOT an artist; we display it separately at the top, not in the Artists roster.
|
|---|
| [ce8c8c2] | 39 | const mainSite = db.prepare(`
|
|---|
| 40 | SELECT s.id, s.slug, s.title, s.tagline, s.profile_photo, s.accent,
|
|---|
| 41 | u.avatar_url AS owner_avatar,
|
|---|
| 42 | (SELECT COUNT(*) FROM posts WHERE site_id = s.id AND status = 'published') AS post_count
|
|---|
| 43 | FROM sites s
|
|---|
| 44 | LEFT JOIN users u ON u.id = s.owner_id
|
|---|
| [7881080] | 45 | WHERE s.is_primary = 1
|
|---|
| [ce8c8c2] | 46 | LIMIT 1
|
|---|
| 47 | `).get() || null;
|
|---|
| 48 | const mainId = mainSite ? mainSite.id : '';
|
|---|
| 49 |
|
|---|
| [834bcc3] | 50 | // Featured Klonkt sites for the home roster: most active first (number of
|
|---|
| 51 | // published posts), then newest. Excl. the main site. Capped at
|
|---|
| 52 | // HOME_ROSTER_LIMIT so the home scales — full list is at /leden.
|
|---|
| [8afbdd6] | 53 | const HOME_ROSTER_LIMIT = 24;
|
|---|
| [6e0249f] | 54 | const artists = db.prepare(`
|
|---|
| [4c5169f] | 55 | SELECT s.slug, s.title, s.tagline, s.profile_photo, s.accent,
|
|---|
| [ab544fd] | 56 | u.avatar_url AS owner_avatar,
|
|---|
| [bf61be7] | 57 | (SELECT COUNT(*) FROM posts WHERE site_id = s.id AND status = 'published') AS post_count
|
|---|
| 58 | FROM sites s
|
|---|
| [ab544fd] | 59 | LEFT JOIN users u ON u.id = s.owner_id
|
|---|
| [ce8c8c2] | 60 | WHERE s.id != @mainId
|
|---|
| [8afbdd6] | 61 | ORDER BY post_count DESC, s.created_at DESC
|
|---|
| [ce8c8c2] | 62 | LIMIT @limit
|
|---|
| 63 | `).all({ mainId, limit: HOME_ROSTER_LIMIT });
|
|---|
| 64 | const totalArtists = db.prepare('SELECT COUNT(*) AS c FROM sites WHERE id != ?').get(mainId).c;
|
|---|
| [bf61be7] | 65 |
|
|---|
| [834bcc3] | 66 | // The hub page is GENERIC (not belonging to any user) — branding comes from global
|
|---|
| 67 | // settings managed by the admin in the admin panel, not from a site.
|
|---|
| [6e0249f] | 68 | const hub = {
|
|---|
| 69 | title: getSetting('hub_title') || 'Overzicht',
|
|---|
| 70 | tagline: getSetting('hub_tagline') || '',
|
|---|
| 71 | intro: getSetting('hub_intro') || '',
|
|---|
| [4c5169f] | 72 | heroImage: getSetting('hub_hero_image') || '',
|
|---|
| [63edb0d] | 73 | heroOverlay: (() => { const v = parseInt(getSetting('hub_hero_overlay'), 10); return Number.isFinite(v) ? Math.max(0, Math.min(100, v)) : 45; })(),
|
|---|
| [6e0249f] | 74 | };
|
|---|
| [bf61be7] | 75 |
|
|---|
| 76 | renderPage(req, res, 'pages/hub-home', {
|
|---|
| [6e0249f] | 77 | pageTitle: hub.title,
|
|---|
| 78 | socialDescr: hub.intro || hub.tagline || '',
|
|---|
| [4d7443b] | 79 | bodyClass: 'on-home on-hub',
|
|---|
| [6e0249f] | 80 | hub,
|
|---|
| [ce8c8c2] | 81 | mainSite,
|
|---|
| [4d7443b] | 82 | artists,
|
|---|
| [8afbdd6] | 83 | totalArtists,
|
|---|
| 84 | rosterLimit: HOME_ROSTER_LIMIT,
|
|---|
| [bf61be7] | 85 | posts,
|
|---|
| 86 | });
|
|---|
| 87 | });
|
|---|
| 88 |
|
|---|
| 89 | export default router;
|
|---|