source: Klonkt/src/routes/circle.js@ 86fa2d2

main
Last change on this file since 86fa2d2 was 5f3f9ec, checked in by roboburr <roboburr@…>, 2 months ago

fix(cirkel): render a federated video cover as <video>, not a broken <img>

An animated cover that federates as an MP4 (Video attachment) put the video URL into cover_image_url
— circle.js's mediaImage fell back to media[0] even when it was the video — so the Cirkel feed
rendered <img src=...mp4> = a broken image. Now the video is extracted separately (never used as the
cover image) and post-tile/post-card render a muted looping <video> for a video-only cover.

Co-Authored-By: Claude <noreply@…>

  • Property mode set to 100644
File size: 3.3 KB
Line 
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
8import express from 'express';
9import { renderPage } from '../middleware/render.js';
10import { apEnabled } from '../services/SettingsService.js';
11import ActivityPubService from '../services/ActivityPubService.js';
12
13const router = express.Router();
14
15function safeUrl(u) {
16 return typeof u === 'string' && /^https?:\/\//i.test(u) ? u : null;
17}
18function safeJson(s) {
19 try { return s ? JSON.parse(s) : []; } catch { return []; }
20}
21// The cover image + (separately) a cover video from a remote note's media. NEVER use a video/audio
22// item as the cover image — that produced a broken <img> for an animated cover that federated as an
23// MP4 (the video becomes a <video> instead).
24function coverMedia(media_json) {
25 const media = safeJson(media_json).map((m) => ({ ...m, url: safeUrl(m.url) })).filter((m) => m.url);
26 const video = media.find((m) => /video/i.test(m.type || '')) || null;
27 const image = media.find((m) => /image/i.test(m.type || ''))
28 || (media[0] && !/(video|audio)/i.test(media[0].type || '') ? media[0] : null);
29 return { image, video };
30}
31function htmlToText(html) {
32 return String(html || '').replace(/<[^>]+>/g, ' ').replace(/&[a-z#0-9]+;/gi, ' ').replace(/\s+/g, ' ').trim();
33}
34
35router.get('/cirkel', (req, res, next) => {
36 const site = res.locals.site;
37 if (!site || !apEnabled() || (ActivityPubService.autoBoostCount(site.slug) === 0 && ActivityPubService.boostedCount(site.slug) === 0)) return next();
38
39 const posts = ActivityPubService.getCirkelPosts(site.slug, 80).map((r) => {
40 const text = htmlToText(r.content);
41 // Show ONLY the title (the bold first line a Klonkt note carries), not the whole
42 // body. Title-less notes (e.g. plain Mastodon) fall back to a short text snippet.
43 const titleM = (r.content || '').match(/^\s*<p>\s*<strong>([\s\S]*?)<\/strong>/i);
44 const realTitle = titleM ? htmlToText(titleM[1]).trim() : '';
45 const cover = coverMedia(r.media_json);
46 const name = r.author_name || r.author_handle || 'Onbekend';
47 return {
48 id: 'ap-' + r.id,
49 slug: '',
50 title: realTitle
51 ? (realTitle.length > 90 ? realTitle.slice(0, 90) + '…' : realTitle)
52 : (text ? (text.length > 90 ? text.slice(0, 90) + '…' : text) : name),
53 excerpt: '',
54 cover_image_url: cover.image ? cover.image.url : null,
55 cover_video_url: cover.video ? cover.video.url : null,
56 published_at: r.published,
57 created_at: r.published,
58 type: 'post',
59 tags: '',
60 pinned: 0,
61 isBoost: !!r.boosted, // a post YOU boosted → render in the pinned style with a Boost badge
62 nsfw: r.nsfw ? 1 : 0, // remote sensitive post → blur in the Cirkel (post-card/tile)
63 content_warning: r.cw || '',
64 status: 'published',
65 source_name: name,
66 external_url: safeUrl(r.url),
67 };
68 });
69
70 const sites = ActivityPubService.getCirkelMembers(site.slug)
71 .map((s) => ({ name: s.name || 'Onbekend', url: safeUrl(s.url), avatar: safeUrl(s.icon) }));
72
73 renderPage(req, res, 'pages/circle-feed', { pageTitle: 'Cirkel', bodyClass: 'on-cirkel', posts, sites });
74});
75
76export default router;
Note: See TracBrowser for help on using the repository browser.