| 1 | /**
|
|---|
| 2 | * Circle feed — the artists this site features (auto-boosts), sourced from
|
|---|
| 3 | * ActivityPub. Cards link to the source post. Available whenever the site
|
|---|
| 4 | * auto-boosts at least one account; otherwise next() -> postsRoutes.
|
|---|
| 5 | * GET /cirkel
|
|---|
| 6 | */
|
|---|
| 7 |
|
|---|
| 8 | import express from 'express';
|
|---|
| 9 | import { renderPage } from '../middleware/render.js';
|
|---|
| 10 | import { apEnabled } from '../services/SettingsService.js';
|
|---|
| 11 | import ActivityPubService from '../services/ActivityPubService.js';
|
|---|
| 12 |
|
|---|
| 13 | const router = express.Router();
|
|---|
| 14 |
|
|---|
| 15 | function safeUrl(u) {
|
|---|
| 16 | return typeof u === 'string' && /^https?:\/\//i.test(u) ? u : null;
|
|---|
| 17 | }
|
|---|
| 18 | function safeJson(s) {
|
|---|
| 19 | try { return s ? JSON.parse(s) : []; } catch { return []; }
|
|---|
| 20 | }
|
|---|
| 21 | function mediaImage(media_json) {
|
|---|
| 22 | const media = safeJson(media_json).map((m) => ({ ...m, url: safeUrl(m.url) })).filter((m) => m.url);
|
|---|
| 23 | return media.find((m) => /image/i.test(m.type || '')) || media[0] || null;
|
|---|
| 24 | }
|
|---|
| 25 | function htmlToText(html) {
|
|---|
| 26 | return String(html || '').replace(/<[^>]+>/g, ' ').replace(/&[a-z#0-9]+;/gi, ' ').replace(/\s+/g, ' ').trim();
|
|---|
| 27 | }
|
|---|
| 28 |
|
|---|
| 29 | router.get('/cirkel', (req, res, next) => {
|
|---|
| 30 | const site = res.locals.site;
|
|---|
| 31 | if (!site || !apEnabled() || (ActivityPubService.autoBoostCount(site.slug) === 0 && ActivityPubService.boostedCount(site.slug) === 0)) return next();
|
|---|
| 32 |
|
|---|
| 33 | const posts = ActivityPubService.getCirkelPosts(site.slug, 80).map((r) => {
|
|---|
| 34 | const text = htmlToText(r.content);
|
|---|
| 35 | // Show ONLY the title (the bold first line a Klonkt note carries), not the whole
|
|---|
| 36 | // body. Title-less notes (e.g. plain Mastodon) fall back to a short text snippet.
|
|---|
| 37 | const titleM = (r.content || '').match(/^\s*<p>\s*<strong>([\s\S]*?)<\/strong>/i);
|
|---|
| 38 | const realTitle = titleM ? htmlToText(titleM[1]).trim() : '';
|
|---|
| 39 | const image = mediaImage(r.media_json);
|
|---|
| 40 | const name = r.author_name || r.author_handle || 'Onbekend';
|
|---|
| 41 | return {
|
|---|
| 42 | id: 'ap-' + r.id,
|
|---|
| 43 | slug: '',
|
|---|
| 44 | title: realTitle
|
|---|
| 45 | ? (realTitle.length > 90 ? realTitle.slice(0, 90) + '…' : realTitle)
|
|---|
| 46 | : (text ? (text.length > 90 ? text.slice(0, 90) + '…' : text) : name),
|
|---|
| 47 | excerpt: '',
|
|---|
| 48 | cover_image_url: image ? image.url : null,
|
|---|
| 49 | published_at: r.published,
|
|---|
| 50 | created_at: r.published,
|
|---|
| 51 | type: 'post',
|
|---|
| 52 | tags: '',
|
|---|
| 53 | pinned: 0,
|
|---|
| 54 | isBoost: !!r.boosted, // a post YOU boosted → render in the pinned style with a Boost badge
|
|---|
| 55 | nsfw: r.nsfw ? 1 : 0, // remote sensitive post → blur in the Cirkel (post-card/tile)
|
|---|
| 56 | content_warning: r.cw || '',
|
|---|
| 57 | status: 'published',
|
|---|
| 58 | source_name: name,
|
|---|
| 59 | external_url: safeUrl(r.url),
|
|---|
| 60 | };
|
|---|
| 61 | });
|
|---|
| 62 |
|
|---|
| 63 | const sites = ActivityPubService.getCirkelMembers(site.slug)
|
|---|
| 64 | .map((s) => ({ name: s.name || 'Onbekend', url: safeUrl(s.url), avatar: safeUrl(s.icon) }));
|
|---|
| 65 |
|
|---|
| 66 | renderPage(req, res, 'pages/circle-feed', { pageTitle: 'Cirkel', bodyClass: 'on-cirkel', posts, sites });
|
|---|
| 67 | });
|
|---|
| 68 |
|
|---|
| 69 | export default router;
|
|---|