| 1 | /**
|
|---|
| 2 | * Circle feed + local reading page.
|
|---|
| 3 | * GET /cirkel -> overview (same timeline/grid view as the home)
|
|---|
| 4 | * GET /cirkel/:id -> individual remote post in own chrome (stay on your site)
|
|---|
| 5 | * Only active when tenancy === 'circle' (otherwise next() -> postsRoutes/404).
|
|---|
| 6 | * See docs/cirkels-v1-spec.md §5c.
|
|---|
| 7 | */
|
|---|
| 8 |
|
|---|
| 9 | import express from 'express';
|
|---|
| 10 | import { renderPage } from '../middleware/render.js';
|
|---|
| 11 | import db from '../config/database.js';
|
|---|
| 12 | import { getTenancy, apEnabled } from '../services/SettingsService.js';
|
|---|
| 13 | import ActivityPubService from '../services/ActivityPubService.js';
|
|---|
| 14 |
|
|---|
| 15 | const router = express.Router();
|
|---|
| 16 |
|
|---|
| 17 | function htmlToText(html) {
|
|---|
| 18 | return String(html || '').replace(/<[^>]+>/g, ' ').replace(/&[a-z#0-9]+;/gi, ' ').replace(/\s+/g, ' ').trim();
|
|---|
| 19 | }
|
|---|
| 20 |
|
|---|
| 21 | function safeUrl(u) {
|
|---|
| 22 | return typeof u === 'string' && /^https?:\/\//i.test(u) ? u : null;
|
|---|
| 23 | }
|
|---|
| 24 | function safeJson(s) {
|
|---|
| 25 | try { return s ? JSON.parse(s) : []; } catch { return []; }
|
|---|
| 26 | }
|
|---|
| 27 | function mediaImage(media_json) {
|
|---|
| 28 | const media = safeJson(media_json).map((m) => ({ ...m, url: safeUrl(m.url) })).filter((m) => m.url);
|
|---|
| 29 | return media.find((m) => m.type === 'image') || null;
|
|---|
| 30 | }
|
|---|
| 31 |
|
|---|
| 32 | // ── Overview ─────────────────────────────────────────────────
|
|---|
| 33 | // New model: the Cirkel = posts from the accounts this site auto-boosts
|
|---|
| 34 | // ("feature an artist"), sourced from ActivityPub. Cards link to the source
|
|---|
| 35 | // post (external_url). Legacy circle-tenancy remote_posts are merged in until
|
|---|
| 36 | // the old pull-protocol is removed (Phase 4).
|
|---|
| 37 | router.get('/cirkel', (req, res, next) => {
|
|---|
| 38 | const site = res.locals.site;
|
|---|
| 39 | if (!site) return next();
|
|---|
| 40 | const slug = site.slug;
|
|---|
| 41 | const isCircle = getTenancy() === 'circle';
|
|---|
| 42 | const abCount = apEnabled() ? ActivityPubService.autoBoostCount(slug) : 0;
|
|---|
| 43 | if (!abCount && !isCircle) return next(); // no cirkel on this site
|
|---|
| 44 |
|
|---|
| 45 | let posts = [];
|
|---|
| 46 |
|
|---|
| 47 | // Featured (auto-boosted) fediverse posts → link to the original.
|
|---|
| 48 | if (abCount) {
|
|---|
| 49 | posts = ActivityPubService.getCirkelPosts(slug, 80).map((r) => {
|
|---|
| 50 | const text = htmlToText(r.content);
|
|---|
| 51 | const image = mediaImage(r.media_json);
|
|---|
| 52 | const name = r.author_name || r.author_handle || 'Onbekend';
|
|---|
| 53 | return {
|
|---|
| 54 | id: 'ap-' + r.id,
|
|---|
| 55 | slug: '',
|
|---|
| 56 | title: text ? (text.length > 90 ? text.slice(0, 90) + '…' : text) : name,
|
|---|
| 57 | excerpt: '',
|
|---|
| 58 | cover_image_url: image ? image.url : null,
|
|---|
| 59 | published_at: r.published,
|
|---|
| 60 | created_at: r.published,
|
|---|
| 61 | type: 'post',
|
|---|
| 62 | tags: '',
|
|---|
| 63 | pinned: 0,
|
|---|
| 64 | status: 'published',
|
|---|
| 65 | source_name: name,
|
|---|
| 66 | external_url: safeUrl(r.url),
|
|---|
| 67 | };
|
|---|
| 68 | });
|
|---|
| 69 | }
|
|---|
| 70 |
|
|---|
| 71 | // Legacy circle-tenancy remote_posts (local reading page, no external_url).
|
|---|
| 72 | if (isCircle) {
|
|---|
| 73 | const rows = db.prepare(`
|
|---|
| 74 | SELECT p.id, p.published, p.title, p.summary, p.media_json, p.tags, a.name AS actor_name
|
|---|
| 75 | FROM remote_posts p JOIN remote_actors a ON a.id = p.actor_id
|
|---|
| 76 | ORDER BY COALESCE(p.published, p.fetched_at) DESC LIMIT 100
|
|---|
| 77 | `).all().map((r) => {
|
|---|
| 78 | const image = mediaImage(r.media_json);
|
|---|
| 79 | return {
|
|---|
| 80 | id: r.id, slug: 'cirkel/' + encodeURIComponent(r.id),
|
|---|
| 81 | title: r.title || '(zonder titel)', excerpt: r.summary || '',
|
|---|
| 82 | cover_image_url: image ? image.url : null, published_at: r.published, created_at: r.published,
|
|---|
| 83 | type: 'post', tags: r.tags || '', pinned: 0, status: 'published', source_name: r.actor_name || 'Onbekend',
|
|---|
| 84 | };
|
|---|
| 85 | });
|
|---|
| 86 | posts = posts.concat(rows);
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | posts.sort((a, b) => String(b.published_at || '').localeCompare(String(a.published_at || '')));
|
|---|
| 90 |
|
|---|
| 91 | // Members header (avatars): featured artists + legacy circle links.
|
|---|
| 92 | let sites = abCount
|
|---|
| 93 | ? ActivityPubService.getCirkelMembers(slug).map((s) => ({ name: s.name || 'Onbekend', url: safeUrl(s.url), avatar: safeUrl(s.icon) }))
|
|---|
| 94 | : [];
|
|---|
| 95 | if (isCircle) {
|
|---|
| 96 | const old = db.prepare(`
|
|---|
| 97 | SELECT a.name, a.url, a.avatar FROM remote_actors a
|
|---|
| 98 | JOIN circle_links l ON l.remote_actor_id = a.id WHERE l.status = 'active' ORDER BY a.name
|
|---|
| 99 | `).all().map((s) => ({ name: s.name || 'Onbekend', url: safeUrl(s.url), avatar: safeUrl(s.avatar) }));
|
|---|
| 100 | sites = sites.concat(old);
|
|---|
| 101 | }
|
|---|
| 102 |
|
|---|
| 103 | renderPage(req, res, 'pages/circle-feed', { pageTitle: 'Cirkel', bodyClass: 'on-cirkel', posts, sites });
|
|---|
| 104 | });
|
|---|
| 105 |
|
|---|
| 106 | // ── Individual remote post (local reading) ────────────────────
|
|---|
| 107 | router.get('/cirkel/:id', (req, res, next) => {
|
|---|
| 108 | if (getTenancy() !== 'circle') return next();
|
|---|
| 109 |
|
|---|
| 110 | const row = db.prepare(`
|
|---|
| 111 | SELECT p.id, p.published, p.title, p.summary, p.url, p.media_json, p.tags,
|
|---|
| 112 | a.name AS actor_name, a.url AS actor_url, a.avatar AS actor_avatar
|
|---|
| 113 | FROM remote_posts p
|
|---|
| 114 | JOIN remote_actors a ON a.id = p.actor_id
|
|---|
| 115 | WHERE p.id = ?
|
|---|
| 116 | `).get(req.params.id);
|
|---|
| 117 |
|
|---|
| 118 | if (!row) return next();
|
|---|
| 119 |
|
|---|
| 120 | const image = mediaImage(row.media_json);
|
|---|
| 121 | const post = {
|
|---|
| 122 | title: row.title || '(zonder titel)',
|
|---|
| 123 | body: row.summary || '', // platte tekst (gesanitized bij ingest)
|
|---|
| 124 | published: row.published,
|
|---|
| 125 | image: image ? image.url : null,
|
|---|
| 126 | sourceName: row.actor_name || 'Onbekend',
|
|---|
| 127 | sourceUrl: safeUrl(row.actor_url),
|
|---|
| 128 | sourceAvatar: safeUrl(row.actor_avatar),
|
|---|
| 129 | originalUrl: safeUrl(row.url),
|
|---|
| 130 | tags: (row.tags || '').split(',').map((t) => t.trim()).filter(Boolean),
|
|---|
| 131 | sourceTagBase: safeUrl(row.actor_url) ? safeUrl(row.actor_url).replace(/\/+$/, '') : null,
|
|---|
| 132 | };
|
|---|
| 133 |
|
|---|
| 134 | renderPage(req, res, 'pages/circle-post', {
|
|---|
| 135 | pageTitle: post.title,
|
|---|
| 136 | bodyClass: 'on-cirkel-post',
|
|---|
| 137 | post,
|
|---|
| 138 | });
|
|---|
| 139 | });
|
|---|
| 140 |
|
|---|
| 141 | export default router;
|
|---|