source: Klonkt/src/routes/circle.js@ 92a2c46

main
Last change on this file since 92a2c46 was 2f50c53, checked in by Robin Genis <roboburr@…>, 2 months ago

fix(cirkel): strip 'RE: <url>' quote prefix from boost card titles

A boosted plain Mastodon post with no Klonkt title fell back to the raw
text, which for a quote/reply starts with 'RE: <long-url>' (the prefix
Misskey/Akkoma and some reply federation prepend) — ugly noise in the tile
title. tidySnippet() drops a leading 'RE: <url>' and any leading bare URL
so the title shows the actual prose.

  • src/routes/circle.js — tidySnippet() applied to the card text snippet

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

  • Property mode set to 100644
File size: 3.7 KB
RevLine 
[0091cb7]1/**
[8ef8c3e]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
[0091cb7]6 */
7
8import express from 'express';
9import { renderPage } from '../middleware/render.js';
[8ef8c3e]10import { apEnabled } from '../services/SettingsService.js';
[2c22bb5]11import ActivityPubService from '../services/ActivityPubService.js';
[0091cb7]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}
[5f3f9ec]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) {
[7917407]25 const media = safeJson(media_json).map((m) => ({ ...m, url: safeUrl(m.url) })).filter((m) => m.url);
[5f3f9ec]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 };
[8ef8c3e]30}
31function htmlToText(html) {
32 return String(html || '').replace(/<[^>]+>/g, ' ').replace(/&[a-z#0-9]+;/gi, ' ').replace(/\s+/g, ' ').trim();
[7917407]33}
[2f50c53]34// Tidy a plain-text snippet for use as a card title: drop a leading "RE: <url>" (the
35// quote/reply prefix Misskey/Akkoma and some reply federation prepend) and any other
36// leading bare URL, so the title shows the actual prose, not link noise.
37function tidySnippet(text) {
38 return String(text || '')
39 .replace(/^RE:\s*https?:\/\/\S+\s*/i, '')
40 .replace(/^https?:\/\/\S+\s*/i, '')
41 .trim();
42}
[0091cb7]43
44router.get('/cirkel', (req, res, next) => {
[2c22bb5]45 const site = res.locals.site;
[5045c30]46 if (!site || !apEnabled() || (ActivityPubService.autoBoostCount(site.slug) === 0 && ActivityPubService.boostedCount(site.slug) === 0)) return next();
[8ef8c3e]47
[d8e3ff7]48 const posts = ActivityPubService.getCirkelPosts(site.slug, 80).map((r) => {
[2f50c53]49 const text = tidySnippet(htmlToText(r.content));
[cf4cf27]50 // Show ONLY the title (the bold first line a Klonkt note carries), not the whole
51 // body. Title-less notes (e.g. plain Mastodon) fall back to a short text snippet.
52 const titleM = (r.content || '').match(/^\s*<p>\s*<strong>([\s\S]*?)<\/strong>/i);
53 const realTitle = titleM ? htmlToText(titleM[1]).trim() : '';
[5f3f9ec]54 const cover = coverMedia(r.media_json);
[8ef8c3e]55 const name = r.author_name || r.author_handle || 'Onbekend';
56 return {
57 id: 'ap-' + r.id,
58 slug: '',
[cf4cf27]59 title: realTitle
60 ? (realTitle.length > 90 ? realTitle.slice(0, 90) + '…' : realTitle)
61 : (text ? (text.length > 90 ? text.slice(0, 90) + '…' : text) : name),
[8ef8c3e]62 excerpt: '',
[5f3f9ec]63 cover_image_url: cover.image ? cover.image.url : null,
64 cover_video_url: cover.video ? cover.video.url : null,
[8ef8c3e]65 published_at: r.published,
66 created_at: r.published,
67 type: 'post',
68 tags: '',
69 pinned: 0,
[c7c7f02]70 isBoost: !!r.boosted, // a post YOU boosted → render in the pinned style with a Boost badge
[b7d4458]71 nsfw: r.nsfw ? 1 : 0, // remote sensitive post → blur in the Cirkel (post-card/tile)
72 content_warning: r.cw || '',
[8ef8c3e]73 status: 'published',
74 source_name: name,
75 external_url: safeUrl(r.url),
76 };
77 });
[0091cb7]78
[d8e3ff7]79 const sites = ActivityPubService.getCirkelMembers(site.slug)
80 .map((s) => ({ name: s.name || 'Onbekend', url: safeUrl(s.url), avatar: safeUrl(s.icon) }));
[8ea3d0d]81
[d8e3ff7]82 renderPage(req, res, 'pages/circle-feed', { pageTitle: 'Cirkel', bodyClass: 'on-cirkel', posts, sites });
[0091cb7]83});
84
85export default router;
Note: See TracBrowser for help on using the repository browser.