/** * ActivityPubService — Klonkt as a real ActivityPub actor (fediverse bridge). * * Phase 1 (this file): the PUBLISH/discoverable side. * - per-site RSA keypair (Mastodon-compatible HTTP Signatures; separate from * the Ed25519 keys used by the lighter Cirkels v1) * - builders for the Actor document, Note objects and the Outbox collection * - apWants(): HTTP content-negotiation helper (activity+json vs HTML) * * The interactive side (inbox: Follow/Accept, signature verify, delivery to * followers) lands in the next step and is tested live against Mastodon. * * AP actor URLs live under /ap/* so they never clash with the human pages: * actor = /ap/users/ * inbox = /inbox outbox = /outbox * note = /ap/notes/ */ import crypto from 'crypto'; import fs from 'fs'; import path from 'path'; import dns from 'dns'; import net from 'net'; import db from '../config/database.js'; import HtmlSanitizerService from './HtmlSanitizerService.js'; import AudioEmbedService from './AudioEmbedService.js'; import EmbedResolver from './EmbedResolver.js'; import Push from './PushService.js'; import { t as i18nT } from './i18n.js'; import Blocklist from './BlocklistService.js'; import * as Guardianship from './guardianship/index.js'; import { PUBLIC, AP_CONTEXT, safeUrl, actorId, noteId, guessMediaType, normalizeTags, tagParts, hashtagTags, buildHashtagList, pagedCollection, PAGINA_GROOTTE, artiestUrl } from './ap-core.js'; // Doorgeven wat hier altijd vandaan kwam, zodat elke bestaande aanroep blijft werken. export { AP_CONTEXT, actorId, noteId, guessMediaType }; // De muziekkant woont in music/ (shaer-drc). Doorgeven wat hier altijd // vandaan kwam, zodat elke bestaande aanroep blijft werken. import { luisteraars } from './music/index.js'; import { TRACK_KOLOMMEN, playlistOpenTracks, siteOpenTracks, openTrack, trackHostPosts, buildTrackAudio, buildTrackCollection, buildTrackCreate, buildPlaylistCollection, listPlaylistsAP, playlistLinkTags, buildPostTrackCollection, uitgavePost, buildLibrary, libraryId, licentieUri, channelCategory, } from './music/index.js'; export { playlistOpenTracks, siteOpenTracks, openTrack, trackHostPosts, buildTrackAudio, buildTrackCollection, buildTrackCreate, buildPlaylistCollection, listPlaylistsAP, playlistLinkTags, licentieUri, buildPostTrackCollection, uitgavePost, buildLibrary, libraryId, }; // Short random suffix so two activity ids minted in the same millisecond (e.g. // parallel saves) don't collide and get deduped by a receiver. const rid = () => crypto.randomBytes(4).toString('hex'); // Keep only http(s) URLs — drops javascript:/data:/etc so a remote actor can't // smuggle a dangerous scheme into a stored href/src (rendered in owner-only views). // ── SSRF guard for outbound fetches ─────────────────────────────── // Remote URLs (actor/keyId/webfinger/inbox/inReplyTo) are attacker-controlled, so // every outbound fetch must refuse hosts that resolve to private/loopback ranges // (cloud metadata, internal services) — on the initial host AND each redirect hop. function isBlockedIp(ip) { if (!ip) return true; const v = net.isIP(ip); if (v === 4) { const o = ip.split('.').map(Number); return o[0] === 127 || o[0] === 10 || o[0] === 0 || (o[0] === 172 && o[1] >= 16 && o[1] <= 31) || (o[0] === 192 && o[1] === 168) || (o[0] === 169 && o[1] === 254) || (o[0] === 100 && o[1] >= 64 && o[1] <= 127); // CGNAT } if (v === 6) { const s = ip.toLowerCase().replace(/^\[|\]$/g, ''); return s === '::1' || s === '::' || s.startsWith('fc') || s.startsWith('fd') || s.startsWith('fe80') || s.startsWith('::ffff:127.') || s.startsWith('::ffff:10.') || s.startsWith('::ffff:192.168.') || s.startsWith('::ffff:169.254.') || s.startsWith('::ffff:172.'); } return true; // not an IP literal we recognise → refuse } /** * Uitzonderingen op de SSRF-poort, voor een testkudde op de eigen machine * (shaer-6wt: honderd wards met een guardian, Barts opdracht 8-8). * * WAAROM DIT MAG BESTAAN. De bescherming hierboven is er omdat een actor-URI van * een VREEMDE komt: een aanvaller die "http://169.254.169.254/" doorgeeft laat * ons zijn werk doen. Deze lijst gaat niet over vreemden -- hij staat in de * omgeving van deze server, wordt door de beheerder gezet, en is leeg tenzij * iemand hem expliciet vult. * * WAAROM HIJ ZO SMAL IS. Geen vlag die "loopback is oke" zegt, maar een lijst * van precieze host:poort-paren. `[::1]:3060` opent niet 127.0.0.1, niet poort * 3061, en niets in het interne netwerk. Een brede vlag zou de bescherming in * een dev-omgeving uitzetten, en dev-omgevingen worden productie. * * AP_ALLOW_HOSTS="[::1]:3060,[::1]:3061" */ const AP_ALLOW_HOSTS = new Set( String(process.env.AP_ALLOW_HOSTS || '').split(',').map((x) => x.trim().toLowerCase()).filter(Boolean), ); function isAllowedTestHost(u) { if (!AP_ALLOW_HOSTS.size) return false; return AP_ALLOW_HOSTS.has(u.host.toLowerCase()); } async function assertPublicHost(hostname) { // URL.hostname geeft een IPv6-literal MET blokhaken ("[::1]"), en net.isIP // herkent die vorm niet. Zonder strippen viel elk IPv6-adres door naar de // DNS-tak, waar het strandde op ENOTFOUND: geweigerd, maar per ongeluk en met // de verkeerde reden. isBlockedIp strippde ze al -- die verwachtte dus input // die hier nooit aankwam. const naakt = String(hostname || '').replace(/^\[|\]$/g, ''); if (net.isIP(naakt)) { if (isBlockedIp(naakt)) throw new Error('ssrf-blocked-ip'); return; } const addrs = await dns.promises.lookup(naakt, { all: true }); if (!addrs.length || addrs.some((a) => isBlockedIp(a.address))) throw new Error('ssrf-blocked-host'); } // One honest name on ALL outbound federation traffic (Robins vraag, 31-7): // safeFetch went out with the bare Node default before, and polite fediverse // citizens say who they are (some instances even refuse anonymous UAs). A // caller-provided User-Agent (the EmbedResolver) still wins. let _uaVer = '1.0'; try { _uaVer = JSON.parse(fs.readFileSync(new URL('../../package.json', import.meta.url))).version || _uaVer; } catch { /* keep default */ } const KLONKT_UA = `Klonkt/${_uaVer} (+https://klonkt.com)`; export async function safeFetch(url, opts = {}, maxRedirects = 3) { let target = url; for (let hop = 0; ; hop++) { const u = new URL(target); // throws on malformed → caller's catch if (u.protocol !== 'https:' && u.protocol !== 'http:') throw new Error('ssrf-bad-scheme'); // Alleen op de precieze host:poort uit AP_ALLOW_HOSTS, en per hop opnieuw: // een omleiding naar een ANDER intern adres blijft geweigerd. if (!isAllowedTestHost(u)) await assertPublicHost(u.hostname); const r = await fetch(target, { ...opts, headers: { 'User-Agent': KLONKT_UA, ...(opts.headers || {}) }, redirect: 'manual', signal: AbortSignal.timeout(8000), }); const loc = (r.status >= 300 && r.status < 400) ? r.headers.get('location') : null; if (loc && hop < maxRedirects) { target = new URL(loc, target).toString(); continue; } return r; } } const MAX_OUTBOX = 20; // Cache-buster for the music listen-link → forces Mastodon to re-crawl a FRESH // (square) player card. Bump this whenever the twitter:player card dimensions change. const FEDI_CARD_VER = '2'; // ── RSA keys per actor (lazy, cached in DB) ─────────────────────── // Prepared lazily (NOT at module load) — the ap_keys table is created in // initializeDatabase(), which runs after this module is imported. let _sel, _ins; function keyStmts() { if (!_sel) { _sel = db.prepare('SELECT public_pem, private_pem FROM ap_keys WHERE slug = ?'); _ins = db.prepare('INSERT OR IGNORE INTO ap_keys (slug, public_pem, private_pem, created_at) VALUES (?,?,?,CURRENT_TIMESTAMP)'); } return { sel: _sel, ins: _ins }; } export function getOrCreateKeys(slug) { const { sel, ins } = keyStmts(); const row = sel.get(slug); if (row) return row; const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', { modulusLength: 2048, publicKeyEncoding: { type: 'spki', format: 'pem' }, privateKeyEncoding: { type: 'pkcs8', format: 'pem' }, }); ins.run(slug, publicKey, privateKey); return sel.get(slug) || { public_pem: publicKey, private_pem: privateKey }; } // ── content negotiation ─────────────────────────────────────────── // True when the caller wants ActivityPub JSON rather than the HTML page. export function apWants(req) { const a = String(req.headers.accept || '').toLowerCase(); return a.includes('application/activity+json') || (a.includes('application/ld+json') && a.includes('activitystreams')); } const AP_CONTENT_TYPE = 'application/activity+json; charset=utf-8'; /** * Hetzelfde antwoord als de vorige keer? Dan 304 (Barts punt, 9-8). * * De inbox doet dit al met `since` + `wait`, en de guardian-wachtrijen niet: die * stuurden bij elke verversing de hele lijst terug, ook als er niets veranderd * was. Bij honderd wards is dat 217 KB JSON die de telefoon opnieuw moet * parsen -- over de lijn valt het mee (2,8 KB gzip), maar het OPBOUWEN van * veertienhonderd objecten is wat je merkt. * * EEN INHOUDS-ETAG, geen cursor. Een cursor vraagt een tweede beschrijving van * wanneer iets "veranderd" is, en die kan uit de pas gaan lopen met wat er * werkelijk in het antwoord staat; een hash van het antwoord zelf kan dat per * definitie niet. De server bouwt het antwoord nog steeds (26 ms) -- wat we * besparen is de overdracht en het parsen. * * NOOIT 304 OP EEN LEEG ANTWOORD. Dezelfde les als de '0'-uitzondering bij de * inbox: gaat er bij het opbouwen iets mis en komt er een lege lijst uit, dan is * die hash ook stabiel, en zou een client voor eeuwig 304 krijgen op niets. */ export function etagFor(body) { return `"${crypto.createHash('sha256').update(body).digest('base64url').slice(0, 27)}"`; } export function sendMaybe304(req, res, obj, { cacheControl, contentType } = {}) { const body = JSON.stringify(obj); const leeg = !obj || (Array.isArray(obj.orderedItems) && obj.orderedItems.length === 0); res.set('Vary', 'Authorization'); if (!leeg) { const tag = etagFor(body); res.set('ETag', tag); if (req.headers['if-none-match'] === tag) return res.status(304).end(); } res.type(contentType || AP_CONTENT_TYPE); // `no-cache` betekent NIET "niet bewaren": de client bewaart het antwoord en // vraagt elke keer of het nog klopt. Precies wat we willen -- zonder dit // stuurt een browser geen If-None-Match en is de ETag decoratie. res.set('Cache-Control', cacheControl || 'private, no-cache'); return res.send(body); } export function sendAP(res, obj, cacheControl) { res.type(AP_CONTENT_TYPE); // A per-caller (e.g. guardian-widened) view must not be publicly cached. res.set('Cache-Control', cacheControl || 'public, max-age=120'); res.send(JSON.stringify(obj)); } // ── document builders ───────────────────────────────────────────── /** Eén Link uit een AS2 `url` kiezen op mediaType. Een `url` mag een string, * een Link of een array van beide zijn; dit is de enige plek die dat weet. */ function pickLink(url, test) { const links = Array.isArray(url) ? url : (url ? [url] : []); for (const l of links) { const href = safeUrl(typeof l === 'string' ? l : (l && l.href)); const mt = (l && typeof l === 'object' && l.mediaType) || ''; if (href && test(mt)) return { href, mediaType: mt }; } return null; } /** * De `url` van de actor als kanaal (shaer-0nh): de webpagina en, als die er is, * de RSS-feed ernaast. * * De RSS-link gaat er ALLEEN in voor de site waar de instance op gepind staat. * Sinds hub-modus verdween serveert routes/feed.js `/feed.xml` van de primaire * site en bestaat `/user/` niet meer als route; een feed-link voor een * andere site zou naar de verkeerde feed wijzen. Liever een link minder dan een * link die iemand anders' muziek belooft. */ export function channelUrls(base, site) { const isPrimair = site.slug === site.primary_slug; const pagina = `${base}/${isPrimair ? '' : 'user/' + encodeURIComponent(site.slug)}`; const uit = [{ type: 'Link', href: pagina, mediaType: 'text/html' }]; if (isPrimair) uit.push({ type: 'Link', href: `${base}/feed.xml`, mediaType: 'application/rss+xml' }); return uit; } /** * Welke objectsoorten deze inbox in de tijdlijn opneemt. * * `Audio` staat erbij sinds de kanaalbeslissing (shaer-0nh): een Funkwhale- * kanaal stuurt Create(Audio), geen Note. Uitbreiden gebeurt HIER en in * timelineFields -- en uitdrukkelijk NIET door vreemde soorten tot Note om te * vormen. Een Audio is geen Note, en die soort willen we kunnen blijven zien. */ const TIJDLIJN_SOORTEN = new Set(['Note', 'Article', 'Question', 'Audio']); /** * Wat de tijdlijn van een binnengekomen object nodig heeft, PER SOORT: de * inhoud-HTML, de bijlagen voor media_json, en de link van het item. * * Eén plek, zodat een nieuwe soort erbij een tak is en geen speurtocht. De * Krant rendert media_json al naar soort -- audio/* wordt een speler -- dus een * track komt vanzelf als echte speler binnen zonder dat de weergave iets van * Funkwhale hoeft te weten. */ /** * De waarschuwingstekst van een object, of niets. * * `summary` IS in AS2 een SAMENVATTING -- "a natural language summarization of * the object". Dat Mastodon dat veld hergebruikt als waarschuwing is Mastodons * conventie, en die zet er `sensitive` bij. Zonder `sensitive` is een summary * dus gewoon een samenvatting. * * WordPress + ActivityPub stuurt daar de EXCERPT van een artikel in, netjes * afgekapt voor Mastodon. Wij lazen dat als waarschuwing en verborgen de post * daarmee achter zijn eigen eerste alinea (Barts melding, 13-8: * europeanpirates.eu). Niemand krijgt dan te zien wat er staat, en de * waarschuwing waarschuwt nergens voor. */ export function contentWarning(o) { if (!o || !o.sensitive) return null; const s = typeof o.summary === 'string' ? o.summary.trim() : ''; return s || null; } export function timelineFields(o) { // De hoes: een `image` op het object. Bij een Note alleen als terugval (daar // is het de kaart-afbeelding van een player-post), bij een Audio altijd, // want daar IS het de albumhoes. const hoes = () => { if (!o.image) return null; const im = Array.isArray(o.image) ? o.image[0] : o.image; const iu = safeUrl(typeof im === 'string' ? im : (im && im.url)); return iu ? { url: iu, type: (im && im.mediaType) || 'image/jpeg' } : null; }; if (o.type === 'Audio') { const geluid = pickLink(o.url, (mt) => /^audio\//i.test(mt)); // De webpagina van de track. Zonder mediaType is dat de veilige aanname: // er een speler op zetten zou een HTML-pagina als geluid aanbieden. const pagina = pickLink(o.url, (mt) => /^text\/html/i.test(mt)) || pickLink(o.url, (mt) => !mt); const atts = []; const h = hoes(); if (h) atts.push(h); // eerst kijken, dan luisteren if (geluid) atts.push({ url: geluid.href, type: geluid.mediaType || 'audio/mpeg' }); // Een Audio heeft geen `content`; de titel is wat er te lezen valt. Door de // sanitizer, want hij komt van een vreemde server. return { html: o.name ? HtmlSanitizerService.sanitize(`

${o.name}

`) : '', atts, url: pagina ? pagina.href : null, }; } // Een ARTIKEL heeft een titel, en die is het eerste wat je wilt zien. Zonder // dit kwam een WordPress-post binnen als kale body: de titel zit in `name` en // die gooiden we weg, terwijl de excerpt in `summary` ten onrechte als // waarschuwing dienstdeed. Nu allebei goed -- en dit is dezelfde greep die // resolveRemoteNote al doet voor niet-Note-objecten, dus de tijdlijn en het // antwoordpad zeggen eindelijk hetzelfde. if (o.type && o.type !== 'Note' && typeof o.name === 'string' && o.name.trim()) { const kop = `

${HtmlSanitizerService.escape ? HtmlSanitizerService.escape(o.name) : o.name}

`; const atts = mediaFromNote(o); const pagina = pickLink(o.url, (mt) => !mt || /html/i.test(mt)); return { html: HtmlSanitizerService.sanitize(kop + (o.content || '')), atts, url: pagina ? pagina.href : null, }; } // Note / Question -- ongewijzigd gedrag. const atts = (Array.isArray(o.attachment) ? o.attachment : []) .map((a) => ({ url: safeUrl(a && a.url), type: (a && a.mediaType) || '' })) .filter((m) => m.url); if (!atts.some((m) => !m.type || /image/i.test(m.type))) { const h = hoes(); if (h) atts.push(h); } const pagina = pickLink(o.url, () => true); return { html: HtmlSanitizerService.sanitize(o.content || ''), atts, url: pagina ? pagina.href : null }; } /** * De site achter een library-uri, of null. Zelfde strengheid als localSlugOf: * de uri moet met ONZE basis beginnen en de site moet bestaan -- anders levert * andermans /library met dezelfde padstaart hier een volger op onze naam op. */ function libraryOwnerSlug(uri) { const u = String(uri || ''); if (!u.endsWith('/library')) return null; return localSlugOf(u.slice(0, -'/library'.length)); } export function buildActor(base, site) { const id = actorId(base, site.slug); const keys = getOrCreateKeys(site.slug); // FEP-633c §5.3: a ward's follows are gated (guardians approve), so the actor // MUST advertise manuallyApprovesFollowers:true — otherwise a follower's server // (Mastodon) assumes auto-accept and shows "Following" while we hold it pending. const isWard = (() => { try { return Guardianship.listGuardians(site.slug).length > 0; } catch { return false; } })(); const actor = { '@context': AP_CONTEXT, id, type: 'Person', preferredUsername: site.slug, name: site.title || site.slug, summary: site.tagline || site.description || '', // Een Link-ARRAY in plaats van een kale string (shaer-0nh): zo adverteert // een kanaal zichzelf, en zo vindt een podcast-app de feed. De text/html // staat VOORAAN, want een lezer die maar één url verwacht pakt de eerste -- // dezelfde vorm die Funkwhale in productie met Mastodon uitwisselt. url: channelUrls(base, site), ...(channelCategory(site) ? { category: channelCategory(site) } : {}), // …and the same honesty for the OWNER gate (Robins wens, 18-8): a site // with approve_followers on holds follows pending until the owner decides. manuallyApprovesFollowers: isWard || !!site.approve_followers, discoverable: true, inbox: `${id}/inbox`, outbox: `${id}/outbox`, followers: `${id}/followers`, following: `${id}/following`, featured: `${id}/featured`, // AS2-kern `streams`: "supplementary Collections which may be of // interest" -- precies wat de playlist-lijst is (shaer-ayc, stap 2). // Geen eigen vocabulaire nodig, en wie het niet kent negeert het. streams: [`${id}/tracks`, `${id}/playlists`], // AP §5.6: the private blocked collection (owner-only GET). The server // list is the source of truth for Shaer's "in Orbit"; clients keep no // separate state. blocked: `${id}/blocked`, // FEP-1580: de vertaaltabel van een verhuizing plus de Moves die hem // rechtvaardigen. Deze twee staan er ALTIJD, ook leeg, en dat is met opzet: // de FEP wijst er apart op dat "een verhuizing zonder objecten" en "een // server die dit niet kent" anders niet uit elkaar te houden zijn. migration: `${id}/migration`, moves: `${id}/moves`, // FEP-633c §2: shaer:guardians / shaer:isGuardian / shaer:queues // (guardianship module owns these). ...Guardianship.guardianshipActorProps(id, site.slug), // C2S clients (Shaer apps) discover auth + upload here — no hardcoded paths. // All four are ActivityPub-spec `endpoints` terms. Dynamic client registration // (RFC 7591) is discovered via /.well-known/oauth-authorization-server, not here. endpoints: { sharedInbox: `${base}/ap/inbox`, oauthAuthorizationEndpoint: `${base}/oauth/authorize`, oauthTokenEndpoint: `${base}/oauth/token`, uploadMedia: `${id}/uploadMedia`, }, publicKey: { id: `${id}#main-key`, owner: id, publicKeyPem: keys.public_pem, }, }; if (site.profile_photo) { const u = /^https?:/.test(site.profile_photo) ? site.profile_photo : `${base}${site.profile_photo.startsWith('/') ? '' : '/'}${site.profile_photo}`; actor.icon = { type: 'Image', url: u }; } // Account creation date — shown by Mastodon + read by indexers (additive, standard AS2). if (site.created_at) { try { actor.published = new Date(site.created_at).toISOString(); } catch { /* skip bad date */ } } // FEP-7628: former identities this account claims. The OLD server checks for // exactly this back-reference before it will move followers here, so the // list must be on the public actor, not tucked away in settings. try { const aka = JSON.parse(site.ap_aliases || '[]'); if (Array.isArray(aka)) { const clean = aka.filter((u) => typeof u === 'string' && /^https?:\/\//i.test(u) && u !== id); if (clean.length) actor.alsoKnownAs = clean; } } catch { /* skip malformed ap_aliases */ } // FEP-7628 slice 3: this account moved. The old actor stays online AS A // SIGNPOST — that is the whole point of keeping it: whoever missed the Move // activity (offline server, later visitor) still learns where we went by // fetching us. Per the FEP the moved actor "should be considered inactive", // and publishers should stop delivering here. if (site.moved_to && /^https?:\/\//i.test(String(site.moved_to))) actor.movedTo = String(site.moved_to); // Zie movedLock() verderop: het serveren van movedTo is de ENE helft, het // stilzetten van de uitgaande kant de andere. // De MusicBrainz-koppeling van de artiest (shaer-mbz). Alleen als hij ZELF // gekozen heeft -- er staat niets als er niets gekoppeld is, want een lege // of geraden verwijzing is erger dan geen. // // schema:sameAs en niet alsoKnownAs: dat laatste is in AS2 voor vroegere // identiteiten van dezelfde actor, en FEP-7628 leunt erop bij een verhuizing. // Een MBID hier neerzetten zou een verhuizing kunnen laten mislukken. const mbUrl = artiestUrl(site.mb_artist_id); if (mbUrl) actor.sameAs = mbUrl; // Profile links → PropertyValue rows: Mastodon/PeerTube/WordPress-ActivityPub render these as // profile metadata (rel=me enables link-back verification). Additive; ignored by simpler receivers. try { const links = JSON.parse(site.profile_links || '[]'); if (Array.isArray(links) && links.length) { const esc = (s) => String(s).replace(/[<>&]/g, (c) => ({ '<': '<', '>': '>', '&': '&' }[c])); const rows = links .filter((l) => l && l.url && /^https?:/i.test(l.url)) .map((l) => ({ type: 'PropertyValue', name: esc(l.platform || 'Link'), value: `${esc(String(l.url).replace(/^https?:\/\//, ''))}`, })); if (rows.length) actor.attachment = rows; } } catch { /* skip malformed profile_links */ } return actor; } // Does a post's audio shortcodes reference at least one PLAYABLE (file-backed) // track? Link-only tracks (external Spotify/YouTube, media_id NULL) don't count — // they have no Klonkt-hosted audio to embed, so no player card / cover-suppression. export function hasPlayableAudio(content, siteId) { if (!content || !/\[\[(track|album|playlist):/i.test(content)) return false; try { for (const m of content.matchAll(/\[\[track:([A-Za-z0-9_-]+)\]\]/g)) { const r = db.prepare('SELECT media_id FROM audio_tracks WHERE id = ?').get(m[1]); if (r && r.media_id) return true; } for (const m of content.matchAll(/\[\[album:([^\]]+)\]\]/g)) { if (db.prepare('SELECT 1 FROM audio_tracks WHERE site_id = ? AND album = ? AND media_id IS NOT NULL LIMIT 1').get(siteId, m[1].trim())) return true; } for (const m of content.matchAll(/\[\[playlist:([A-Za-z0-9_-]+)\]\]/g)) { if (db.prepare('SELECT 1 FROM playlist_tracks pt JOIN audio_tracks t ON t.id = pt.track_id WHERE pt.playlist_id = ? AND t.media_id IS NOT NULL LIMIT 1').get(m[1])) return true; } } catch { /* non-fatal */ } return false; } // A single post as an AS2 Note (the object), and as a Create activity (for outbox/delivery). export function buildNote(base, site, post, opts = {}) { // Replies are Notes too. buildNote is the single entry point for ALL Notes; a reply is // (for now) the simple flavor: pre-baked content, no title/cover/image/audio/embed // machinery, addressed to the parent actor + thread. This early branch keeps that output // byte-identical to the old buildReplyNote. When rich replies land (images/audio/embeds), // this branch collapses and replies flow through the full post pipeline below. `post` here // is the ap_outbox reply row (id, in_reply_to, content, post_slug, created_at, to_actor). if (opts.isReply) { const meR = actorId(base, site.slug); // Rich replies: attachments column (JSON [{url, mediaType, name}]) → AS2 // attachment array with absolute URLs and the matching object type. let replyAtt; try { const list = post.attachments ? JSON.parse(post.attachments) : []; if (Array.isArray(list) && list.length) { replyAtt = list.map((a) => ({ type: a.mediaType.startsWith('image/') ? 'Image' : a.mediaType.startsWith('audio/') ? 'Audio' : 'Video', mediaType: a.mediaType, url: /^https?:/i.test(a.url) ? a.url : `${base}${a.url}`, name: a.name || undefined, })); } } catch { /* malformed attachments never block the Note */ } return { id: noteId(base, post.id), type: 'Note', attributedTo: meR, inReplyTo: post.in_reply_to || undefined, content: post.content, // Reply language (rich replies): the AS2 language map next to `content`. contentMap: post.language ? { [post.language]: post.content } : undefined, attachment: replyAtt, url: post.post_slug ? `${base}/${encodeURIComponent(post.post_slug)}` : undefined, published: toISO(post.created_at), // A direct note (private mention, shaer-tqc) addresses ONLY its // recipients: no Public anywhere, so it cannot be boosted and never // shows in public timelines (the Mastodon DM model). to: post.visibility === 'direct' ? (JSON.parse(post.to_actors || '[]')) : (post.to_actor ? [post.to_actor] : [PUBLIC]), // Followers-only reply ('friends', shaer detail-view Reply): the parent // author (in `to`) + our followers, but NO Public — it does not federate // into open discovery. Default reply stays quiet-public (Public in cc). cc: post.visibility === 'direct' ? [] : post.visibility === 'friends' ? [`${meR}/followers`] : [PUBLIC, `${meR}/followers`], // FEP-633c 5.2.1: a ward's call for help. Only ever on direct notes. ...Guardianship.helpRequestProps(post), ...Guardianship.waveProps(post), ...Guardianship.awayProps(post), // FEP-633c §2.2: object hint that the author is a ward. ...Guardianship.hasGuardiansProps(site.slug), tag: [ ...mentionTags(post.content), ...hashtagTags(base, post.content), ], }; } const id = noteId(base, post.id); const aId = actorId(base, site.slug); const human = `${base}/${encodeURIComponent(post.slug)}`; // Mastodon ignores a Note's `name`, so put the title INTO the content (bold // first line) — the standard blog→fediverse convention. post.content is // already sanitized HTML; the title is plain text, so escape it. const escTitle = String(post.title || '').replace(/[<>&]/g, (c) => ({ '<': '<', '>': '>', '&': '&' }[c])); const titleHtml = post.title ? `

${escTitle}

` : ''; // Paid post (klonkt-demo-aki): federate a PUBLIC teaser + link, never the full // content, so nothing leaks past the paywall. No media attachments either. if (post.paid) { const esc = (x) => String(x || '').replace(/[<>&]/g, (c) => ({ '<': '<', '>': '>', '&': '&' }[c])); const _firstP = (String(post.content || '').match(/]*>([\s\S]*?)<\/p>/i) || [null, ''])[1] || ''; const rawTeaser = String(post.excerpt || '').trim() || _firstP.replace(/<[^>]+>/g, ' ').replace(/&[a-z#0-9]+;/gi, ' ').replace(/\s+/g, ' ').trim().slice(0, 280); return { '@context': AP_CONTEXT, id, type: 'Note', attributedTo: aId, content: `${titleHtml}

${esc(rawTeaser)}${rawTeaser ? '…' : ''}

Lees de volledige post (supporters)

`, url: human, published: toISO(post.published_at || post.created_at || Date.now()), to: [PUBLIC], cc: [`${aId}/followers`], tag: [...hashtagTags(base, post.content)], replies: `${id}/replies`, // DE WAARSCHUWING REIST MEE (Barts melding, 15-8). Deze vroege return liet // `sensitive` en `summary` vallen, want die worden pas na de gewone tak // gezet. Gevolg: een betaalde post met een waarschuwing ging ZONDER die // waarschuwing de deur uit -- en de teaser is publiek, dus juist die had // hem nodig. Een gevoelige teaser zonder vlag is erger dan geen teaser. sensitive: !!post.nsfw, ...(post.nsfw ? { summary: post.content_warning || 'Gevoelige inhoud' } : {}), ...Guardianship.hasGuardiansProps(site.slug), }; } // Images travel as AP `attachment` (Mastodon strips from content). Collect // the cover + any inline , make absolute, then strip from the content // to avoid duplicate rendering on clients that DO keep them. const abs = (u) => !u ? null : (/^https?:/i.test(u) ? u : `${base}${u.startsWith('/') ? '' : '/'}${u}`); const hadAudio = /\[\[(track|album|playlist):/i.test(post.content || ''); const playable = hasPlayableAudio(post.content || '', site && site.id); // A post with an external embed (Spotify/YouTube/SoundCloud/Vimeo/Bandcamp/Apple) should let // Mastodon render the embed's player CARD. Mastodon shows EITHER media attachments OR a link // card, never both — so when the post has an embed link we skip the image attachments so the // card wins. (On Klonkt nothing changes: the cover + the embed player still render.) const hasEmbed = (() => { const c = post.content || ''; if (/\[\[embed:/i.test(c)) return true; for (const m of c.matchAll(/https?:\/\/[^\s"'<>]+/gi)) if (AudioEmbedService.detectProvider(m[0])) return true; return false; })(); // Link-only tracks (external Spotify/YouTube/SoundCloud, no hosted file): collect their links // so we federate them — Mastodon cards the first (its player), the rest show as clickable links // — instead of a bare "listen on site" link, and we suppress the cover so the card can show. const trackEmbedLinks = (() => { if (playable) return []; const out = []; try { for (const m of (post.content || '').matchAll(/\[\[track:([A-Za-z0-9_-]+)\]\]/g)) { const r = db.prepare('SELECT media_id, link_spotify, link_youtube, link_soundcloud FROM audio_tracks WHERE id = ?').get(m[1]); if (r && !r.media_id) for (const u of [r.link_spotify, r.link_youtube, r.link_soundcloud]) if (u && /^https?:\/\//i.test(u)) out.push(u); } } catch { /* non-fatal */ } return [...new Set(out)].slice(0, 6); })(); const noImages = playable || hasEmbed || trackEmbedLinks.length > 0; // suppress images → let the player/embed card show const urls = []; // Posts with PLAYABLE hosted audio suppress image attachments so Mastodon renders // the player CARD (twitter:player) instead of the cover — media attachment and // link/player card are mutually exclusive on Mastodon. Link-only audio (external) // keeps its cover (no player card to show). // An animated cover federates as the muted loop MP4 (→ a Video attachment): animated WebP is // unreliable on Mastodon and its iOS apps; the MP4 plays everywhere. Else the still cover image. // Each entry carries the media URL + its alt text (federated as the AS2 attachment `name`, for a11y). // Media a C2S composer attached (shaer-j3uh): federate with their REAL // mediaType, because the extension map below knows no audio and would call // an m4a an Image. Pushed BEFORE the covers: a C2S video doubles as the // cover video, and the URL-dedupe keeps the FIRST entry, which must be the // one that knows its type and poster. Images also live inline in the // content, so the dedupe keeps those single too. try { for (const a of JSON.parse(post.c2s_attachments || '[]')) { if (a && a.url) urls.push({ url: abs(a.url), name: a.name || '', mt: a.mediaType, poster: a.poster ? abs(a.poster) : null }); } } catch { /* malformed never blocks the Note */ } if (post.cover_video_url && !noImages) urls.push({ url: abs(post.cover_video_url), name: post.cover_alt || '' }); else if (post.cover_image_url && !noImages) urls.push({ url: abs(post.cover_image_url), name: post.cover_alt || '' }); let body = post.content || ''; // Only federate inline images we can actually serve: absolute http(s) URLs, or our own // /media/ uploads. A relative path we don't host (e.g. a stale /images/... ref) would 404 // and show up as a black tile in Mastodon's attachment grid. Carry the … through // as the attachment description. if (!noImages) for (const m of body.matchAll(/]*>/gi)) { const tag = m[0]; const src = (tag.match(/\bsrc="([^"]+)"/i) || [])[1]; if (!src || !(/^https?:\/\//i.test(src) || src.startsWith('/media/'))) continue; const alt = (tag.match(/\balt="([^"]*)"/i) || [])[1] || ''; urls.push({ url: abs(src), name: alt }); } body = body.replace(/]*>/gi, ''); // Video and audio tags leave the federated content the same way (30-7): // they ride as AS2 attachments (c2s_attachments), and the tag itself // carries a RELATIVE /media src that is dead everywhere but our own web. // Leaving it in showed every remote reader a broken player above the // working one. The web keeps its tags: this strip is federation-only. body = body.replace(/]*>[\s\S]*?<\/video>/gi, '').replace(/]*\/?>/gi, ''); body = body.replace(/]*>[\s\S]*?<\/audio>/gi, '').replace(/]*\/?>/gi, ''); // Audio shortcodes: do NOT federate the raw audio file — Klonkt deliberately // gates audio (the /audio/stream URL has friction), and shipping it as an AP // audio attachment would hand Mastodon a plain, downloadable mp3 URL. Instead, // replace the shortcodes with a "🎵 listen on the site" link so the post invites // a click-through to the protected player (discovery without leaking the file). const esc = (s) => String(s == null ? '' : s).replace(/[<>&]/g, (c) => ({ '<': '<', '>': '>', '&': '&' }[c])); // Elke titel met zijn track-id erbij, zodat hij hieronder een EIGEN link // krijgt naar #track- op de postpagina (shaer-38y). Zonder id was dit een // vetgedrukte opsomming waar je niets mee kon: vijf namen en een enkele // "listen on"-link naar de post als geheel. Elke track heeft daar al een // anker -- direct ingesloten, in een album of in een playlist -- dus dit // wijst naar precies het nummer waar de naam bij hoort. const audioLabels = []; try { const zien = new Set(); const voegToe = (id, titel) => { const t = String(titel || '').trim(); if (!t) return; const sleutel = id || ('naam:' + t); if (zien.has(sleutel)) return; zien.add(sleutel); audioLabels.push({ id: id || null, titel: t }); }; // In de volgorde van de POST: een enkele scan over alle drie de vormen, // zodat de opsomming leest zoals de post is neergezet. for (const m of body.matchAll(/\[\[(track|album|playlist):([^\]]+)\]\]/gi)) { const soort = m[1].toLowerCase(), waarde = m[2].trim(); if (soort === 'track') { const r = db.prepare('SELECT id, title FROM audio_tracks WHERE id = ?').get(waarde); if (r) voegToe(r.id, r.title); } else if (soort === 'album') { const rs = db.prepare('SELECT id, title FROM audio_tracks WHERE site_id = ? AND album = ? ORDER BY rowid').all(site.id, waarde); if (rs.length) for (const r of rs) voegToe(r.id, r.title); else voegToe(null, waarde); // album zonder tracks: dan maar de naam } else { for (const r of db.prepare('SELECT t.id, t.title FROM playlist_tracks pt JOIN audio_tracks t ON t.id = pt.track_id WHERE pt.playlist_id = ? ORDER BY pt.position').all(waarde)) voegToe(r.id, r.title); } } } catch { /* non-fatal */ } // fedi_open tracks → real AS2 Audio attachments (the actual file URL, served ungated) so // EVERY client incl. the Mastodon apps plays them inline natively. Gated tracks (default) // stay link/card-only — the file is never exposed for them. Resolve from post.content so a // later body mutation can't affect it. const openAudio = []; if (hadAudio) { const seenA = new Set(); const addRow = (r) => { const fn = r.filename || (r.storage_path || '').split('/').pop(); if (!fn || seenA.has(fn)) return; seenA.add(fn); const a = { type: 'Audio', mediaType: r.mime_type || 'audio/mpeg', url: `${base}/audio/stream/${encodeURIComponent(fn)}`, name: r.title || 'Audio' }; // Cover art on the Audio attachment (AS2 `icon`): track cover, else the post cover. // Mastodon renders it as the artwork thumbnail on its native audio player. const art = abs(r.cover_url || post.cover_image_url || null); if (art) a.icon = { type: 'Image', mediaType: guessMediaType(art), url: art }; openAudio.push(a); }; const SEL = 'SELECT t.title, t.cover_url, m.filename, m.storage_path, m.mime_type FROM audio_tracks t JOIN media m ON m.id = t.media_id WHERE t.fedi_open = 1 AND '; try { for (const mm of (post.content || '').matchAll(/\[\[track:([A-Za-z0-9_-]+)\]\]/g)) { const r = db.prepare(SEL + 't.id = ?').get(mm[1]); if (r) addRow(r); } for (const mm of (post.content || '').matchAll(/\[\[album:([^\]]+)\]\]/g)) for (const r of db.prepare(SEL + 't.site_id = ? AND t.album = ? ORDER BY t.rowid').all(site.id, mm[1].trim())) addRow(r); for (const mm of (post.content || '').matchAll(/\[\[playlist:([A-Za-z0-9_-]+)\]\]/g)) for (const r of db.prepare('SELECT t.title, t.cover_url, m.filename, m.storage_path, m.mime_type FROM playlist_tracks pt JOIN audio_tracks t ON t.id = pt.track_id JOIN media m ON m.id = t.media_id WHERE t.fedi_open = 1 AND pt.playlist_id = ? ORDER BY pt.position').all(mm[1])) addRow(r); } catch { /* non-fatal */ } } // ONVERTAALD voor een verhuizing (FEP-1580). De doelinstantie IS een Klonkt: // die rendert [[track:]], [[album:]] en [[playlist:]] zelf en maakt er een // speler van. Bakken we ze eerst om, dan komt er een tekstlink aan en is de // speler weg. Onherstelbaar bovendien: het bakken STRIPT de shorthand en // plakt achteraan hooguit VIER titels, dus een album van tien nummers // overleeft het niet. // // Dezelfde regel als bij de outbox en de tracks: wie ondertekend vraagt // namens de actor waar wij naartoe verhuisd zijn, krijgt onze eigen kijk. if (!opts.rauweInhoud) body = body.replace(/\[\[(track|album|playlist):[^\]]+\]\]/gi, ''); // External embeds ([[embed:url]]) → emit the bare URL as a link so Mastodon // renders its OWN preview/player card (YouTube/Spotify/SoundCloud/etc) instead // of federating the raw shortcode text. body = body.replace(/\[\[embed:([^\]]+)\]\]/gi, (mm, raw) => { const u = esc(raw.trim().replace(/&/g, '&')); return `

${u}

`; }); if (hadAudio && !opts.rauweInhoud) { // Elke titel als eigen link naar zijn anker; een titel zonder id (een // albumnaam zonder tracks) blijft gewone tekst. const lbl = audioLabels.slice(0, 4) .map((a) => (a.id ? `${esc(a.titel)}` : esc(a.titel))) .join(', '); if (trackEmbedLinks.length) { // Link-only track(s): emit the external link(s). Mastodon cards the first (Spotify → its // player), the rest render as clickable links — the fediverse-native "embed + links". body += `

🎵 ${lbl ? `${lbl}` : ''}

`; for (const u of trackEmbedLinks) { const eu = esc(u); body += `

${eu}

`; } } else { // For playable posts, append a version param to the listen-link so Mastodon // sees a NEW card URL and re-crawls it (fresh SQUARE player card) instead of // reusing the cached landscape one. Invisible: the link TEXT stays clean, the // page ignores the param. Bump FEDI_CARD_VER when the card dimensions change. const listenHref = playable ? `${human}?fc=${FEDI_CARD_VER}` : human; body += `

🎵 ${lbl ? `${lbl} — ` : ''}listen on ${esc(site.title || 'the site')}

`; } } // Klonkt renders post content with white-space:pre-wrap, so raw newlines ARE line // breaks on the site. Mastodon (plain HTML) collapses whitespace and would drop them, // so convert newlines to
for the federated copy (content already made with // shift+enter uses
and has no \n → this is a no-op there). body = body.replace(/\r?\n/g, '
'); body = linkHashtags(base, body); // link inline #hashtags in the post body too body = linkUrls(body); // bare URLs → clickable links on the federated copy // Append the tags-field hashtags to the content so Mastodon renders them as clickable // hashtags (a Hashtag that's only in the `tag` array isn't shown inline). CamelCase // multi-word tags; skip any already present inline in the body. { const inlineTags = new Set(hashtagTags(base, body).map((h) => h.name.slice(1).toLowerCase())); const addSeen = new Set(); const tagLinks = normalizeTags(post.tags).map(tagParts).filter(Boolean) .filter((p) => !inlineTags.has(p.slug) && !addSeen.has(p.slug) && addSeen.add(p.slug)) .map((p) => ``); if (tagLinks.length) body += `

${tagLinks.join(' ')}

`; } const seen = new Set(); const attachment = urls.filter((x) => x && x.url) .filter((x) => { if (seen.has(x.url)) return false; seen.add(x.url); return true; }) .map((x) => { const mt = x.mt || guessMediaType(x.url); // the stored type wins; the extension map is the fallback const ty = /^image\//i.test(mt) ? 'Image' : /^video\//i.test(mt) ? 'Video' : /^audio\//i.test(mt) ? 'Audio' : 'Document'; const a = { type: ty, mediaType: mt, url: x.url }; if (x.name) a.name = String(x.name).slice(0, 1500); // alt text / description (AS2 `name`) if (x.poster) a.icon = { type: 'Image', url: x.poster }; // the video's still (shaer-zowq) return a; }); for (const a of openAudio) attachment.push(a); // fedi_open tracks → native Audio players // Inline @user@host mentions: the Mention tag objects + the mentioned actor URIs. Only // present when the content was already mention-linked (deliverCreate/Update resolve them // at send time); a plain buildNote (outbox/notes) yields none. const _mentionTags = mentionTags(body); const _mentionCc = _mentionTags.map((t) => t.href); const note = { id, type: 'Note', attributedTo: aId, content: titleHtml + body, url: human, published: new Date(post.published_at || post.created_at || Date.now()).toISOString(), // fan_only = "fans only" → followers-only visibility (delivered to your followers // but not addressed to Public, so Mastodon shows it only to them and can't boost it). to: (post.fan_only || post.ap_visibility === 'quiet') ? [`${aId}/followers`] : [PUBLIC], // Mentioned actors (from inline @user@host links the caller resolved) are addressed in cc // so Mastodon notifies them; empty unless the content was mention-linked (delivery time). cc: [...new Set([ ...(post.ap_visibility === 'quiet' ? [PUBLIC] : []), // quiet public: Public in cc, not to ...((post.fan_only || post.ap_visibility === 'quiet') ? [] : [`${aId}/followers`]), ..._mentionCc])], tag: [...buildHashtagList(base, post.tags, body), ..._mentionTags, ...playlistLinkTags(base, site, post.content, post)], replies: `${id}/replies`, // NSFW → Mastodon-style content warning: sensitive (blurs media) + a summary/spoiler // (hides the whole post behind a "Gevoelige inhoud" button until the reader opens it). sensitive: !!post.nsfw, }; // FEP-633c §2.2: object hint that the author is a ward (safely ignorable). Object.assign(note, Guardianship.hasGuardiansProps(site.slug)); // FEP-044f: this post quotes a fediverse object. Emit it the way the network // actually reads it, and address the quoted author so they get told. applyQuoteProps(note, post.quote_uri, post.quote_actor); if (post.nsfw) note.summary = post.content_warning || 'Gevoelige inhoud'; if (attachment.length) note.attachment = attachment; // When the cover attachment is suppressed (hosted audio OR an external embed/link-only track → // so Mastodon shows the player/link card, not media), still expose the cover via AS2 `image` so // card/grid consumers (the Klonkt Cirkel/News feed) can show it. Mastodon ignores a Note's // `image`, so its card is unaffected — but a Klonkt receiver reads it (handleInbox o.image). if (post.cover_image_url && noImages) { const cov = abs(post.cover_image_url); if (cov) { note.image = { type: 'Image', mediaType: guessMediaType(cov), url: cov }; if (post.cover_alt) note.image.name = String(post.cover_alt).slice(0, 1500); } } // Experiment (mirrors PeerTube / schema.org `embedUrl`): point at the GATED player page // (/embed) so a client that honours embedUrl can show an inline player WITHOUT ever // getting the audio file — the anti-steal posture is untouched. `embedUrl` is a real // standard field name (not a Klonkt invention); if Mastodon's apps honour it on a Note we // make it JSON-LD-clean with a context term, otherwise it degrades to the player card. if (playable) note.embedUrl = `${base}/embed?post=${encodeURIComponent(post.slug)}`; // Content language → AS2 contentMap (a BCP-47-keyed copy of the content). Mastodon reads the // language from its key for the timeline language filter + the translate button. Emitted // alongside `content` (Mastodon sends both); a plain receiver just uses `content`. if (post.language && /^[a-z]{2,3}(-[A-Za-z]{2,4})?$/.test(post.language)) note.contentMap = { [post.language]: note.content }; // A hosted poll → federate as an AS2 Question (options + live tally). Do this last so it // reuses the note's content/addressing/tags, then swaps the type and strips media. const ownPoll = parseOwnPoll(post.poll_json); if (ownPoll) applyPollToNote(note, post.id, ownPoll); return note; } // All reply note URIs on a local post (inbound fediverse replies + our own // outbound replies) — backs the Note's `replies` Collection so remote servers // can fetch the whole thread. export function getReplyUris(base, postId) { const out = []; try { for (const r of db.prepare("SELECT object_uri FROM ap_interactions WHERE kind = 'reply' AND post_id = ? AND object_uri != '' ORDER BY created_at").all(postId)) out.push(r.object_uri); for (const r of db.prepare('SELECT id FROM ap_outbox WHERE post_id = ? ORDER BY rowid').all(postId)) out.push(`${base}/ap/notes/${r.id}`); } catch { /* non-fatal */ } return out; } // Notifications "seen" tracking → a real bell badge. Stored per site in app_settings. export function markNotificationsSeen(slug) { try { db.prepare("INSERT INTO app_settings (key, value, updated_at) VALUES (?, ?, CURRENT_TIMESTAMP) ON CONFLICT(key) DO UPDATE SET value = excluded.value, updated_at = CURRENT_TIMESTAMP") .run(`fedi_notif_seen:${slug}`, new Date().toISOString()); } catch { /* non-fatal */ } } export function countUnseenNotifications(slug) { try { const row = db.prepare('SELECT value FROM app_settings WHERE key = ?').get(`fedi_notif_seen:${slug}`); const seen = row ? Date.parse(row.value) : 0; let n = 0; for (const it of getNotifications(slug, 50)) { if (Date.parse(it.created_at) > seen) n++; } return n; } catch { return 0; } } // The seen-watermark itself (ms epoch, 0 = never marked) — the Messages page reads it // BEFORE marking seen, so it can render unread dots on the items newer than last visit. export function notificationsSeenAt(slug) { try { const row = db.prepare('SELECT value FROM app_settings WHERE key = ?').get(`fedi_notif_seen:${slug}`); return row ? (Date.parse(row.value) || 0) : 0; } catch { return 0; } } // Messages = the unified inbox (Reacties + Meldingen merged, decision Robin+Bart 2026-07-16): // every notification PLUS your own outbound replies ('sent', with edit/delete via their // outboxId), sorted as one stream. Consecutive likes/boosts on the same post collapse into // one grouped item (actors list + count) so activity doesn't drown out conversations. /** ap_outbox.attachments ([{url, mediaType, name}]) naar de vorm die note-body * leest (media_json: [{url, type, name}]). Geeft null bij niets of rommel, * zodat een kapotte kolom hooguit media kost en niet de hele regel. */ function outboxMediaJson(attachments) { if (!attachments) return null; try { const list = JSON.parse(attachments); if (!Array.isArray(list) || !list.length) return null; const media = list .filter((a) => a && a.url) .map((a) => ({ url: a.url, type: a.mediaType || a.type || '', name: a.name || undefined })); return media.length ? JSON.stringify(media) : null; } catch { return null; } } export function getMessages(slug, limit, offset) { const off = Math.max(0, offset || 0); const lim = limit || 60; // The stream is grouped (consecutive likes/boosts collapse), so paging is done by // recomputing the whole stream top-down and slicing [off, off+lim] — stable across // pages. Fetch a buffer past off+lim so grouping-shrinkage can't hide a full page. const need = off + lim + 100; const items = getNotifications(slug, need); try { for (const m of listOutbox(slug).slice(0, need)) { items.push({ type: 'sent', outboxId: m.id, to_handle: m.to_handle, to_actor: m.to_actor, to_actors: m.to_actors, in_reply_to: m.in_reply_to, post_slug: m.post_slug, content: m.content, editable: m.editable, language: m.language, created_at: m.created_at, // Je eigen bericht hoort er hetzelfde uit te zien als dat van een ander: // note-body rendert Berichten, de Krant en de Guardian-PWA, maar leest // media uit media_json met een `type`, terwijl ap_outbox ze als // `attachments` met een `mediaType` bewaart. Zonder deze vertaling kwam // een foto die JIJ meestuurde als kale tekst binnen. media_json: outboxMediaJson(m.attachments), }); } } catch { /* ignore */ } // Een verzonden antwoord kent zijn post_slug maar niet de titel (ap_outbox // bewaart die niet). Zonder titel toont een draad waarin JIJ als enige iets // zei alleen een slug, dus vullen we ze in één query aan. try { const missing = [...new Set(items.filter((i) => i.post_slug && !i.post_title).map((i) => i.post_slug))]; if (missing.length) { const rows = db.prepare( `SELECT slug, title FROM posts WHERE slug IN (${missing.map(() => '?').join(',')}) AND site_id = (SELECT id FROM sites WHERE slug = ?)`, ).all(...missing, slug); const byslug = new Map(rows.map((r) => [r.slug, r.title])); for (const i of items) if (i.post_slug && !i.post_title) i.post_title = byslug.get(i.post_slug) || null; } } catch { /* zonder titel valt de draad terug op de slug */ } items.sort((a, b) => _msgTs(b) - _msgTs(a)); // NaN-safe (zie getNotifications) const out = []; for (const it of items) { const prev = out[out.length - 1]; if ((it.type === 'like' || it.type === 'announce') && prev && prev.type === it.type && prev.post_slug === it.post_slug) { prev.actors = prev.actors || [prev.name || prev.handle || '?']; prev.actors.push(it.name || it.handle || '?'); prev.count = (prev.count || 1) + 1; continue; } out.push(it); } // Antwoorden, mentions en je eigen verzonden berichten vouwen samen tot // draden; likes/boosts/follows/reports blijven losse regels. Na deze stap // telt een draad als één item voor de paginering, wat klopt: je scrolt door // gesprekken, niet door losse zinnen. return groupConversations(out).slice(off, off + lim); } // De drie soorten die samen een gesprek vormen. Vroeger zaten ze in drie // aparte chips: 'reply' en 'mention' onder Berichten/Gesprekken (afhankelijk van // de zichtbaarheid) en 'sent' onder Verzonden. Wie een uitwisseling wilde volgen // moest dus tussen chips heen en weer, terwijl het één draad is. const CONV_TYPES = new Set(['reply', 'mention', 'sent']); /** Waar hangt dit bericht aan? Twee soorten draden, en de volgorde telt: * * 1. Aan een post van jou. Een ontvangen antwoord kent zijn post via de join * op `posts`, een verzonden antwoord via ap_outbox.post_slug. Dat is * dezelfde sleutel, en daarom staan ze nu in dezelfde draad. * 2. Aan een persoon. Een mention hangt aan niets van jou (het is iemands * eigen post waarin je genoemd wordt) en heeft geen post_slug; die draad * loopt per tegenpartij. * * De post wint van de persoon: twee mensen die onder dezelfde post reageren * voeren één gesprek, geen twee. Geeft null terug voor alles wat geen gesprek * is (likes, boosts, follows, reports, poll-uitslagen); die stromen ongemoeid * door. */ export function threadKey(it) { if (!it || !CONV_TYPES.has(it.type)) return null; if (it.post_slug) return `post:${it.post_slug}`; let who = it.handle || it.to_handle || ''; // Een direct bericht kan zonder to_handle in de tabel staan (de handle van de // ontvanger was niet af te leiden). De eerste uit to_actors is dan alsnog de // tegenpartij, en zonder deze terugval kreeg een gesprek dat JIJ begon geen // draad -- precies het geval waarin het meest onlogisch is dat het los blijft. if (!who && it.to_actors) { try { const first = JSON.parse(it.to_actors)[0]; if (first) who = deriveHandle(first); } catch { /* geen bruikbare lijst → geen sleutel, het blijft een losse regel */ } } const norm = String(who || '').trim().toLowerCase().replace(/^@/, ''); return norm ? `actor:${norm}` : null; } /** Vouw losse berichten samen tot draden, met alles wat geen gesprek is * ongemoeid ertussen. Verwacht [items] al gesorteerd op created_at aflopend * (zoals getMessages ze aanlevert); de draad komt daardoor op de plek van zijn * nieuwste bericht te staan en `created_at` van de draad IS dat bericht. Binnen * de draad draait het om: een gesprek leest naar beneden, oud naar nieuw. */ export function groupConversations(items) { const threads = new Map(); const out = []; for (const it of items || []) { const key = threadKey(it); if (!key) { out.push(it); continue; } let t = threads.get(key); if (!t) { // Eerste keer dat we deze draad zien = het nieuwste bericht erin, want de // invoer is aflopend gesorteerd. Vandaar created_at hier en niet later. t = { type: 'thread', key, post: null, people: [], messages: [], created_at: it.created_at }; threads.set(key, t); out.push(t); } t.messages.push(it); // De context bij de draad: gaat het over een post, dan hoort de link // erbij, anders is een los antwoord in een lijst niet te plaatsen. // De titel blijft LEEG zolang hij onbekend is, in plaats van terug te // vallen op de slug: het nieuwste bericht in een draad is vaak je eigen // verzonden antwoord, en dat kent alleen de slug. Zou die de titel worden, // dan kan het ontvangen antwoord eronder de echte titel niet meer // invullen. De terugval op de slug hoort in de weergave, niet in de data. if (it.post_slug) { if (!t.post) t.post = { slug: it.post_slug, title: it.post_title || null }; else if (!t.post.title && it.post_title) t.post.title = it.post_title; } } for (const t of threads.values()) { t.messages.sort((a, b) => _msgTs(a) - _msgTs(b)); t.count = t.messages.length; // Wie zit er in dit gesprek, jij niet meegerekend: 'sent' ben jij. const seen = new Set(); for (const m of t.messages) { if (m.type === 'sent') continue; const h = m.handle || m.name; if (!h || seen.has(h)) continue; seen.add(h); t.people.push({ name: m.name, handle: m.handle, icon: m.icon, url: m.url }); } // Heb JIJ in deze draad iets gezegd? Bepaalt of hij als uitwisseling of als // onbeantwoord bericht leest. t.mine = t.messages.some((m) => m.type === 'sent'); // Waar gaat een antwoord uit deze draad heen? Twee paden, en ze sluiten // elkaar uit: hangt de draad aan een post, dan antwoord je op het NIEUWSTE // ontvangen bericht erin (dat is de parent van de thread) -- anders is het // een direct bericht aan de tegenpartij. const inkomend = t.messages.filter((m) => m.type !== 'sent'); const laatste = inkomend[inkomend.length - 1]; t.replyTo = { interactionId: (laatste && laatste.interactionId) || null, postSlug: (t.post && t.post.slug) || null, actorUri: (laatste && (laatste.actorUri || laatste.url)) || (t.messages.find((m) => m.to_actor) || {}).to_actor || (() => { try { return JSON.parse((t.messages.find((m) => m.to_actors) || {}).to_actors || '[]')[0] || null; } catch { return null; } })(), }; } return out; } export function buildCreate(base, site, post, opts = {}) { const note = buildNote(base, site, post, opts); return { '@context': AP_CONTEXT, id: note.id + '#create', type: 'Create', actor: actorId(base, site.slug), published: note.published, to: note.to, cc: note.cc, object: note, }; } /** * De outbox: wat deze actor heeft uitgebracht. Posts EN tracks (shaer-0nh, * stap 4). * * WAAROM HIER EN NIET IN EEN BEZORGING. Een kanaal-lezer HAALT de outbox op -- * zo heb ik zelf Funkwhales kanaal uitgelezen. Een Create(Audio) ook naar de * inboxen van volgers duwen zou schade doen: Mastodon neemt Audio aan als * statustype, dus bij een album-post zou dezelfde muziek twee keer in hun * tijdlijn komen -- een keer als bijlage bij de Note, en dan nog N keer los. * De post is het bericht, de outbox is de discografie. * * Door elkaar op datum, nieuwste eerst, zodat de outbox één verhaal vertelt in * plaats van twee lijstjes achter elkaar. * * De tracks komen als ARGUMENT binnen, net als de posts, en worden hier * uitdrukkelijk NIET zelf opgehaald. De route beslist wie wat mag zien -- een * geblokkeerde bezoeker krijgt daar een lege outbox, en een bouwer die stiekem * zijn eigen database bevraagt zou dwars door die deur heen leveren. */ /** * Een PAGINA van de outbox, in SQL (shaer-sk4). * * De outbox mengt twee bronnen: posts en open tracks, gevlochten op datum. Een * offset over die twee kan niet met twee losse queries -- je weet niet hoeveel * van elk er in pagina drie horen. Vandaar een UNION met de datum als sleutel, * daar de LIMIT/OFFSET overheen, en pas dan de rijen zelf ophalen. * * Wat er stond was geen paginering maar een KAP: de route haalde twintig posts * en hield daarvan twintig items over. Alles daarvoor was niet op een volgende * pagina maar helemaal onbereikbaar. * * @param {boolean} fanOnly mag de lezer ook de fans-only posts zien? */ export function outboxSlice(siteId, { fanOnly = false, offset = 0, limit = MAX_OUTBOX } = {}) { const fanClause = fanOnly ? '' : 'AND (p.fan_only IS NULL OR p.fan_only = 0)'; const unie = ` SELECT 'post' AS soort, p.id AS id, COALESCE(p.published_at, p.created_at) AS wanneer FROM posts p WHERE p.site_id = ? AND p.status = 'published' ${fanClause} UNION ALL SELECT 'track', t.id, t.created_at FROM audio_tracks t WHERE t.site_id = ? AND t.fedi_open = 1`; let rijen = [], totaal = 0; try { totaal = db.prepare(`SELECT COUNT(*) n FROM (${unie})`).get(siteId, siteId).n; rijen = db.prepare(`SELECT soort, id FROM (${unie}) ORDER BY wanneer DESC LIMIT ? OFFSET ?`) .all(siteId, siteId, limit, Math.max(0, offset)); } catch { return { posts: [], tracks: [], totaal: 0 }; } const postIds = rijen.filter((r) => r.soort === 'post').map((r) => r.id); const trackIds = rijen.filter((r) => r.soort === 'track').map((r) => r.id); const gaten = (n) => Array.from({ length: n }, () => '?').join(','); const posts = postIds.length ? db.prepare( // fan_only en ap_visibility MOETEN mee. buildNote adresseert hierop, en // zonder deze twee kolommen is post.fan_only altijd undefined: elke // fan-only post ging dan de outbox uit met to: as:Public, terwijl hij // alleen aan vrienden geserveerd wordt. Een volger kreeg dus een // vrienden-post met een publiek etiket erop, en die mag hij dan publiek // boosten. Gevonden tijdens de FEP-1580 end-to-end test (shaer-fuyo). // EN paid + excerpt, om exact dezelfde reden (Barts melding, 15-8). Zonder // `paid` is post.paid hier `undefined`, dan slaat buildNote zijn redactie // over en gaat de VOLLEDIGE tekst van een betaalde post de outbox uit. Zo // kwam een post via een hub-actor gewoon te lezen. `excerpt` moet mee omdat // de teaser daaruit komt; zonder dat veld valt hij terug op de eerste // alinea van precies de tekst die verborgen hoort te blijven. // // Dit is een KOLOMMENLIJST, en die faalt stil: een vergeten kolom is // `undefined` en niet een fout. Wie hier een veld toevoegt waar buildNote // op beslist, moet het HIER ook toevoegen. `SELECT id, slug, title, excerpt, content, cover_image_url, cover_video_url, nsfw, content_warning, c2s_attachments, quote_json, embed_json, published_at, created_at, fan_only, ap_visibility, paid, paid_min_cents FROM posts WHERE id IN (${gaten(postIds.length)})`).all(...postIds) : []; const tracks = trackIds.length ? db.prepare( `SELECT ${TRACK_KOLOMMEN} FROM audio_tracks t JOIN media m ON m.id = t.media_id WHERE t.id IN (${gaten(trackIds.length)})`).all(...trackIds) : []; return { posts, tracks, totaal }; } export function buildOutbox(base, site, posts, tracks = [], { page = false, totalItems, alGesneden = false, rauweInhoud = false } = {}) { const id = `${actorId(base, site.slug)}/outbox`; const wanneer = (x) => Date.parse(x && x.published ? x.published : 0) || 0; const items = [ ...(posts || []).map((p) => buildCreate(base, site, p, { rauweInhoud })), // Eén zoekopdracht voor alle tracks samen, niet per stuk. ...(() => { const posts = (tracks || []).length && site.id ? trackHostPosts(site.id) : null; return (tracks || []).map((r) => buildTrackCreate(base, site, r, { hostPosts: posts })); })(), ] .sort((a, b) => wanneer(b) - wanneer(a)) .slice(0, alGesneden ? Infinity : MAX_OUTBOX); // WAT HIER NOG NIET GEPAGINEERD IS, en dat hoort genoemd (shaer-sk4): deze // lijst is al door de route op twintig rijen afgekapt, dus pagina 2 is leeg. // Echt doorbladeren vraagt een LIMIT/OFFSET in SQL -- en dat is hier lastiger // dan bij volgers, want posts en tracks worden op DATUM door elkaar gevlochten // en komen uit twee tabellen. Dat vraagt een UNION met een offset erover, geen // tweede slice. De vorm klopt nu wel: pagina 2 zegt eerlijk dat hij leeg is en // biedt geen `next` aan, in plaats van pagina 1 nog eens te geven. // GEPAGINEERD, ook al past alles op een pagina (Funkwhale, 11-8). // // Hun serializer weigerde onze outbox met "first: This field is required" en // "last: This field is required". AS2 EIST ze niet -- een collectie mag zijn // items inline dragen -- maar bijna iedereen pagineert, en een lezer die de // paginaweg volgt liep hier dood. Dit is de eerste concrete reden die we // hoorden waarom er niets van ons binnenkwam. // // De items blijven WEL inline op de wortel. Shaer bouwt zijn feed daaruit, en // wie hem vandaag leest hoort er morgen niet voor te hoeven pagineren. Er is // precies een pagina, dus first en last wijzen naar dezelfde. return pagedCollection(id, items, { page, totalItems, alGesneden }); } // Public callers get a count-only collection (privacy). The authenticated // account owner (a C2S bearer scoped to this site) gets the real actor URIs via // `items`, so their own client can build a friends list. export function buildFollowers(base, site, count, items = null, { page = false } = {}) { const id = `${actorId(base, site.slug)}/followers`; // count-only for the public; full for the owner return pagedCollection(id, items || [], { totalItems: items ? items.length : (count || 0), page }); } // The accounts this site follows — count only, mirroring buildFollowers. The spec lists // `following` as a standard actor property; Hubzilla/Friendica + crawlers expect it. export function buildFollowing(base, site, count, items = null, { page = false } = {}) { const id = `${actorId(base, site.slug)}/following`; // count-only for the public; full for the owner return pagedCollection(id, items || [], { totalItems: items ? items.length : (count || 0), page }); } // Pinned posts → the actor's `featured` collection. Mastodon reads this and shows // these as the "Featured" tab (pinned to the profile). Posts come ordered by pin // rank; embedded as full Notes so a remote server doesn't need extra fetches. export function buildFeatured(base, site, posts, { page = false } = {}) { const id = `${actorId(base, site.slug)}/featured`; const items = (posts || []).map((p) => buildNote(base, site, p)); return pagedCollection(id, items, { page }); } // ── Playlist als AP-collectie (shaer-ayc, stap 1 van het Funkwhale-spoor) ── // Een playlist heeft, anders dan een album-als-tekstveld, een id — dus kan hij // een stabiele URI dragen en federeren. De vorm is bewust kaal AS2: een // OrderedCollection van Audio-objecten, dezelfde rijvorm die een post als // attachment meestuurt, zodat elke client die post-audio al speelt dit ook // speelt. // // De poortregel verandert hier NIET: alleen fedi_open-tracks staan erin, met // echte bestands-URL. Een gated track is niet "een rij zonder url" maar // afwezig — wie de collectie leest ziet het open deel en kan niet aftellen // hoeveel er achter de poort staat. totalItems telt daarom ook alleen het // open deel: een eerlijke telling over wat er werkelijk in de collectie staat, // niet over wat wij thuis in de kast hebben. // ── followers store (lazy stmts) ────────────────────────────────── let _insF, _updFDisp, _delF, _listF, _cntF; function fStmts() { if (!_insF) { _insF = db.prepare('INSERT OR IGNORE INTO ap_followers (slug, actor_uri, inbox, shared_inbox, name, handle, icon, created_at) VALUES (?,?,?,?,?,?,?,CURRENT_TIMESTAMP)'); _updFDisp = db.prepare('UPDATE ap_followers SET name = COALESCE(?, name), handle = COALESCE(?, handle), icon = COALESCE(?, icon) WHERE slug = ? AND actor_uri = ?'); _delF = db.prepare('DELETE FROM ap_followers WHERE slug = ? AND actor_uri = ?'); _listF = db.prepare('SELECT inbox, shared_inbox FROM ap_followers WHERE slug = ?'); _cntF = db.prepare('SELECT COUNT(*) n FROM ap_followers WHERE slug = ?'); } return { ins: _insF, del: _delF, list: _listF, cnt: _cntF }; } export function followerCount(slug) { return fStmts().cnt.get(slug).n; } // Followers with delivery health, for the management list. Never-delivered accounts // first, then oldest successful delivery first — i.e. the cleanup candidates on top. export function listFollowers(slug) { return db.prepare( `SELECT id, actor_uri, inbox, shared_inbox, created_at, last_delivery_at, last_error_at FROM ap_followers WHERE slug = ? ORDER BY (last_delivery_at IS NULL) DESC, last_delivery_at ASC, created_at ASC` ).all(slug); } // Manually drop a follower after a check (a still-live account would have to re-follow). /** * Een volger verwijderen, en het hem ook LATEN WETEN (Robin, 21-8). * * Reject(Follow) is het standaardsignaal voor "je volgt me niet meer": de * andere kant ruimt de relatie dan op in plaats van te blijven denken dat hij * volgt. Zonder dit merkte de hub niets -- die bleef als volger in zijn eigen * boeken staan terwijl er nooit meer iets werd bezorgd. * * Verwijderen gaat altijd door; de melding is een gunst en mag mislukken. */ export function removeFollower(slug, id) { const rij = db.prepare('SELECT actor_uri FROM ap_followers WHERE slug = ? AND id = ?').get(slug, id); const info = db.prepare('DELETE FROM ap_followers WHERE slug = ? AND id = ?').run(slug, id); if (info.changes > 0 && rij && rij.actor_uri) meldNietLangerVolger(slug, rij.actor_uri); return info.changes > 0; } /** Reject(Follow) naar een ex-volger; faalt stil, want de relatie is al weg. */ export function meldNietLangerVolger(slug, actorUri) { try { const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); const me = actorId(base, slug); const site = db.prepare('SELECT * FROM sites WHERE slug = ?').get(slug); if (!site) return; const reject = { '@context': AP_CONTEXT, id: `${me}#reject-follow-${Date.now()}-${rid()}`, type: 'Reject', actor: me, to: [actorUri], object: { type: 'Follow', actor: actorUri, object: me }, }; deliverToActor(site, actorUri, reject) .then((r) => console.log('[AP] Reject(Follow)', slug, '→', actorUri, r && r.delivered ? 'bezorgd' : 'niet bezorgd')) .catch(() => {}); } catch { /* nooit blokkerend */ } } // Best cached display for an actor URI, across the caches Klonkt already fills: // followers (now with name/icon), following, interactions, timeline, mentions. // Falls back to a handle derived from the URI. Display info is not sensitive. export function actorDisplay(slug, uri) { const ok = (r) => r && (r.name || r.icon); try { let r = db.prepare('SELECT name, handle, icon FROM ap_followers WHERE slug = ? AND actor_uri = ?').get(slug, uri); if (ok(r)) return { name: r.name, handle: r.handle || deriveHandle(uri), icon: r.icon }; r = db.prepare('SELECT name, handle, icon FROM ap_following WHERE slug = ? AND actor_uri = ?').get(slug, uri); if (ok(r)) return { name: r.name, handle: r.handle || deriveHandle(uri), icon: r.icon }; r = db.prepare('SELECT actor_name AS name, actor_handle AS handle, actor_icon AS icon FROM ap_interactions WHERE actor_uri = ? AND (actor_name IS NOT NULL OR actor_icon IS NOT NULL) ORDER BY created_at DESC LIMIT 1').get(uri); if (ok(r)) return { name: r.name, handle: r.handle || deriveHandle(uri), icon: r.icon }; r = db.prepare('SELECT author_name AS name, author_handle AS handle, author_icon AS icon FROM ap_timeline WHERE author_uri = ? AND (author_name IS NOT NULL OR author_icon IS NOT NULL) LIMIT 1').get(uri); if (ok(r)) return { name: r.name, handle: r.handle || deriveHandle(uri), icon: r.icon }; r = db.prepare('SELECT actor_name AS name, actor_handle AS handle, actor_icon AS icon FROM ap_mentions WHERE actor_uri = ? AND (actor_name IS NOT NULL OR actor_icon IS NOT NULL) ORDER BY created_at DESC LIMIT 1').get(uri); if (ok(r)) return { name: r.name, handle: r.handle || deriveHandle(uri), icon: r.icon }; } catch { /* ignore */ } return { name: null, handle: deriveHandle(uri), icon: null }; } // FEP-9876: does this `Prefer` header ask for enriched (embedded) members? // Pure and testable; the route sets the response headers around it. export function prefersEnriched(preferHeader) { return /(^|[,;\s])return=representation($|[,;\s])/i.test(String(preferHeader || '')); } // AS2 actor reference with display, for the owner C2S followers/following view. // preferredUsername = the local part of the handle; name = the set display name. export function buildActorRef(slug, uri) { const d = actorDisplay(slug, uri); const user = d.handle && d.handle[0] === '@' ? d.handle.slice(1).split('@')[0] : null; const out = { id: uri, type: 'Person' }; if (d.name) out.name = d.name; if (user) out.preferredUsername = user; if (d.icon) out.icon = { type: 'Image', url: d.icon }; return out; } // The site's OWN display info in the same shape as `shaer:author` on timeline // entries. The owner's app reads its own posts from the outbox, which carried // no author info, so every card but your own had a byline (Robins melding, // 30-7: geen header van self op eigen posts). export function selfAuthor(base, site) { const out = { name: site.title || site.slug, handle: `@${site.slug}@${String(base).replace(/^https?:\/\//, '')}`, url: `${base}/${site.slug === site.primary_slug ? '' : 'user/' + encodeURIComponent(site.slug)}`, }; if (site.profile_photo) { out.icon = /^https?:/.test(site.profile_photo) ? site.profile_photo : `${base}${site.profile_photo.startsWith('/') ? '' : '/'}${site.profile_photo}`; } return out; } // Merge who-you-follow (ap_following, rich display) with who-follows-you (ap_followers, // delivery health) into ONE connections list, keyed by actor_uri. Each entry gets a // direction (following →, follower ←, mutual ↔) and, for accounts we deliver to, an // `unreachable` flag (never delivered, or last attempt failed after the last success) so // the view can split dead connections into their own section. Powers the Connect page. export function listConnections(slug) { const byUri = new Map(); for (const f of listFollowing(slug)) { byUri.set(f.actor_uri, { actor_uri: f.actor_uri, name: f.name || null, handle: f.handle || null, icon: f.icon || null, url: f.url || null, auto_boost: f.auto_boost ? 1 : 0, status: f.status || null, following: true, follower: false, last_delivery_at: null, last_error_at: null, follower_id: null, }); } for (const fo of listFollowers(slug)) { const e = byUri.get(fo.actor_uri); if (e) { e.follower = true; e.last_delivery_at = fo.last_delivery_at; e.last_error_at = fo.last_error_at; e.follower_id = fo.id; } else byUri.set(fo.actor_uri, { actor_uri: fo.actor_uri, name: null, handle: null, icon: null, url: null, auto_boost: 0, status: null, following: false, follower: true, last_delivery_at: fo.last_delivery_at, last_error_at: fo.last_error_at, follower_id: fo.id, }); } return [...byUri.values()].map((e) => { e.direction = (e.following && e.follower) ? 'mutual' : (e.following ? 'following' : 'follower'); e.unreachable = e.follower && (!e.last_delivery_at || (!!e.last_error_at && (!e.last_delivery_at || e.last_error_at > e.last_delivery_at))); return e; }); } // ── inbound interactions store (replies / likes / boosts) + our outbound replies ── let _insI, _delLA, _delReply, _listI, _getI, _insO, _listO, _getO; // ── moderation tombstones (ap_rejected_objects) ─────────────────── // A reply the owner removed stays removed: its object URI is tombstoned and // checked at ingest AND by the thread-crawler (else thread-filling would // re-fetch it). Owner moderation acts on the LOCAL copy, so it also works for // private notes that authorize_interaction can't fetch (401/404). let _insRj, _hasRj; function rjStmts() { if (!_insRj) { _insRj = db.prepare('INSERT OR IGNORE INTO ap_rejected_objects (object_uri, post_id, reason) VALUES (?,?,?)'); _hasRj = db.prepare('SELECT 1 FROM ap_rejected_objects WHERE object_uri = ?'); } return { ins: _insRj, has: _hasRj }; } export function isRejectedObject(uri) { if (!uri) return false; try { return !!rjStmts().has.get(String(uri)); } catch { return false; } } // Owner removes an incoming reply: tombstone + delete. Tenancy-scoped: the // interaction's post must belong to the caller's site. export function rejectInteraction(site, interactionId, reason) { if (!site || !site.slug) return { error: 'forbidden' }; const row = iStmts().getI.get(interactionId); if (!row) return { error: 'not_found' }; const owns = db.prepare('SELECT 1 FROM posts WHERE id = ? AND site_id = (SELECT id FROM sites WHERE slug = ?)') .get(row.post_id, site.slug); if (!owns) return { error: 'forbidden' }; if (row.object_uri) { try { rjStmts().ins.run(row.object_uri, row.post_id, reason || 'removed by site owner'); } catch { /* non-fatal */ } } db.prepare('DELETE FROM ap_interactions WHERE id = ?').run(interactionId); console.log('[AP] interaction removed by owner', site.slug, row.object_uri || row.actor_uri); return { ok: true, object_uri: row.object_uri || null, actor_uri: row.actor_uri || null }; } // Stored URIs of an interaction (tenancy-scoped) → feed sendReport for flagging // from the local copy (works for private notes; no remote fetch needed to target). export function interactionReportTarget(site, interactionId) { if (!site || !site.slug) return null; const row = iStmts().getI.get(interactionId); if (!row) return null; const owns = db.prepare('SELECT 1 FROM posts WHERE id = ? AND site_id = (SELECT id FROM sites WHERE slug = ?)') .get(row.post_id, site.slug); if (!owns) return null; return { objectUri: row.object_uri || null, actorUri: row.actor_uri || null }; } // AP addressing → visibility: 'public' | 'unlisted' | 'followers' | 'direct'. // Mastodon-conventie: Public in `to` = public, Public in `cc` = unlisted, een // followers-collectie zonder Public = followers-only, anders direct (DM). Public // kan als volledige URI, 'as:Public' of 'Public' voorkomen (JSON-LD shorthands). export function noteVisibility(o) { const arr = (v) => (Array.isArray(v) ? v : (v ? [v] : [])); const isPub = (u) => u === PUBLIC || u === 'as:Public' || u === 'Public'; const to = arr(o && o.to).map(String); const cc = arr(o && o.cc).map(String); if (to.some(isPub)) return 'public'; if (cc.some(isPub)) return 'unlisted'; if ([...to, ...cc].some((u) => /\/followers\/?$/.test(u))) return 'followers'; return 'direct'; } /** * Does this note belong in the home timeline (de Krant)? * * Only if it is a POST. A direct note is addressed to named people, so it is a * message: a plain DM, a ward's 🛟 help request (FEP-633c 5.2.1) or a * guardian's wave. Those are stored as mentions instead and surface in * Berichten and the Guardian PWA. A reply belongs to its thread, not the feed. */ export function belongsInTimeline(o) { if (!o || !o.id || o.inReplyTo) return false; return noteVisibility(o) !== 'direct'; } function iStmts() { if (!_insI) { _insI = db.prepare('INSERT OR IGNORE INTO ap_interactions (kind, post_id, object_uri, actor_uri, actor_name, actor_handle, actor_url, actor_icon, content, published, parent_uri, visibility, emoji_json, actor_emoji_json, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)'); _delLA = db.prepare('DELETE FROM ap_interactions WHERE kind = ? AND post_id = ? AND actor_uri = ?'); _delReply = db.prepare("DELETE FROM ap_interactions WHERE kind = 'reply' AND object_uri = ?"); _listI = db.prepare('SELECT id, kind, object_uri, parent_uri, actor_uri, actor_name, actor_handle, actor_url, actor_icon, content, published, created_at, acted_boost, acted_like, visibility, emoji_json, actor_emoji_json FROM ap_interactions WHERE post_id = ? ORDER BY created_at ASC'); _getI = db.prepare('SELECT * FROM ap_interactions WHERE id = ?'); _insO = db.prepare('INSERT INTO ap_outbox (id, site_slug, post_id, post_slug, in_reply_to, to_actor, to_handle, content, language, attachments, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)'); _listO = db.prepare('SELECT * FROM ap_outbox WHERE post_id = ? ORDER BY created_at ASC'); _getO = db.prepare('SELECT * FROM ap_outbox WHERE id = ?'); } return { ins: _insI, delLA: _delLA, delReply: _delReply, list: _listI, getI: _getI, insO: _insO, listO: _listO, getO: _getO }; } export function getInteractionById(id) { return iStmts().getI.get(id); } export function setInteractionBoosted(id, on) { db.prepare('UPDATE ap_interactions SET acted_boost = ? WHERE id = ?').run(on ? 1 : 0, id); } export function setInteractionLiked(id, on) { db.prepare('UPDATE ap_interactions SET acted_like = ? WHERE id = ?').run(on ? 1 : 0, id); } // Your like/boost state on a REMOTE post (interact page toggles). export function setMyReaction(slug, uri, kind, on) { if (on) db.prepare('INSERT OR IGNORE INTO ap_my_reactions (site_slug, target_uri, kind) VALUES (?,?,?)').run(slug, uri, kind); else db.prepare('DELETE FROM ap_my_reactions WHERE site_slug = ? AND target_uri = ? AND kind = ?').run(slug, uri, kind); } export function getMyReactions(slug, uri) { const rows = (slug && uri) ? db.prepare('SELECT kind FROM ap_my_reactions WHERE site_slug = ? AND target_uri = ?').all(slug, uri) : []; return { liked: rows.some((r) => r.kind === 'like'), boosted: rows.some((r) => r.kind === 'boost') }; } const localPostExists = (id) => { try { return !!db.prepare('SELECT 1 FROM posts WHERE id = ?').get(id); } catch { return false; } }; // Extract our local post id from a note URL, but only if it's ours (base match). // One host, two spellings (Barts WebFinger-les, 2-8): a URL the client hands // back may carry the punycoded host (every URL parser silently punycodes) // while PUBLIC_BASE_URL carries the typed one. WHATWG URL does the IDNA, so // compare origins in ASCII and never the bytes the client happened to send. function asciiOrigin(u) { try { const x = new URL(String(u)); return `${x.protocol}//${x.host}`.toLowerCase(); } catch { return null; } } function isOwnUrl(u) { const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); if (!base) return false; const a = asciiOrigin(u); return !!a && a === asciiOrigin(base); } function postIdFromNoteUrl(url, base) { const s = String(url || ''); // ASCII origins, not startsWith: xn--zz9h.example IS 🩵.example, and a // byte comparison read our own note as a stranger's. if (base) { const a = asciiOrigin(s); if (!a || a !== asciiOrigin(base)) return null; } const m = s.match(/\/ap\/notes\/([^/?#]+)/); return m ? decodeURIComponent(m[1]) : null; } export function deriveHandle(actorUri) { try { const u = new URL(actorUri); const seg = u.pathname.split('/').filter(Boolean).pop() || ''; return `@${seg}@${u.host}`; } catch { return String(actorUri || ''); } } function actorInfo(doc, actorUri) { let host = ''; try { host = new URL(actorUri).host; } catch { /* keep empty */ } const handle = doc && doc.preferredUsername ? `@${doc.preferredUsername}@${host}` : deriveHandle(actorUri); const icon = doc && doc.icon ? (doc.icon.url || (Array.isArray(doc.icon) && doc.icon[0] && doc.icon[0].url)) : null; const name = (doc && (doc.name || doc.preferredUsername)) || handle; // Een AS2 `url` mag een ARRAY van Links zijn -- onze eigen buildActor doet // dat (profiel + RSS), en een oudere consument stringde die array tot // "[object Object],[object Object]" in de mention-hrefs van een hulpvraag // (Barts vondst, 8-8). pickLink kiest de html-Link; de kale string blijft // de gewone weg, en de actor-id de terugval. const profiel = (doc && Array.isArray(doc.url)) ? ((pickLink(doc.url, (mt) => !mt || /html/i.test(mt)) || {}).href || safeUrl(doc.id || actorUri)) : safeUrl((doc && (doc.url || doc.id)) || actorUri); return { name, handle, url: profiel || null, icon: safeUrl(icon) || null, // FEP-9098 custom emojis in the display name (":shortcode:"), so the byline // renders them. Only computed when the name actually has a shortcode. emojis: /:[A-Za-z0-9_+-]+:/.test(name) ? actorNameEmojis(doc) : undefined, }; } // Map ":shortcode:" → image url from an actor doc's Emoji tags (for a custom- // emoji display name). Undefined when there are none. function actorNameEmojis(doc) { const arr = doc && Array.isArray(doc.tag) ? doc.tag : (doc && doc.tag ? [doc.tag] : []); const out = {}; for (const t of arr) { if (!t || (Array.isArray(t.type) ? t.type[0] : t.type) !== 'Emoji' || typeof t.name !== 'string' || !t.icon) continue; const u = t.icon.url || (Array.isArray(t.icon) && t.icon[0] && t.icon[0].url); if (u) out[t.name] = u; } return Object.keys(out).length ? out : undefined; } // Given an inReplyTo note URL, find which local post the thread belongs to + the // note being replied to (parent), so a reply-to-a-comment can be nested. function findThreadTarget(inReplyTo, base) { if (!inReplyTo) return null; const seg = postIdFromNoteUrl(inReplyTo, base); // our /ap/notes/ segment (if ours) if (seg && localPostExists(seg)) return { post_id: seg, parent_uri: inReplyTo }; if (seg) { try { const o = db.prepare('SELECT post_id FROM ap_outbox WHERE id = ?').get(seg); if (o && o.post_id) return { post_id: o.post_id, parent_uri: inReplyTo }; } catch { /* ignore */ } } try { const row = db.prepare("SELECT post_id FROM ap_interactions WHERE object_uri = ? AND kind = 'reply' LIMIT 1").get(inReplyTo); if (row && row.post_id) return { post_id: row.post_id, parent_uri: inReplyTo }; } catch { /* ignore */ } return null; } // Drop the leading @mention(s) a federated reply carries (the person being replied to), // so a comment reads "dope tekening ouwe" instead of "@jason@jasonhacky.nl dope …". // Keeps a leading

wrapper; handles mention links and plain-text @user@domain. export function stripLeadingMentions(html) { if (!html) return html; let s = String(html); s = s.replace(/^(\s*]*>)?\s*(?:]*>\s*@[^<]+<\/a>[  ]*)+/i, (m, p) => p || ''); s = s.replace(/^(\s*]*>)?\s*(?:@[\w.-]+(?:@[\w.-]+)?[  ]+)+/i, (m, p) => p || ''); return s; } // View-ready threaded view of a post's fediverse activity (inbound replies + // our outbound replies, nested), plus like/boost counts. export function getInteractions(postId, base, site) { const s = iStmts(); // Privacy: a followers-only or direct (DM) reply is addressed to people, not to the // public web, so it must NOT render in the public thread. It still reaches the owner // via notifications (post context + reference included there). Legacy rows without a // visibility value are treated as public. Likes/boosts stay counted (count-only). const rows = s.list.all(postId).filter((r) => r.kind !== 'reply' || !(r.visibility === 'followers' || r.visibility === 'direct')); const baseClean = (base || process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); const postNoteId = baseClean ? `${baseClean}/ap/notes/${postId}` : null; // Our own (outbound) replies show the SITE identity for everyone (not "You"). let host = ''; try { host = new URL(baseClean).host; } catch { /* ignore */ } const siteName = (site && (site.title || site.slug)) || ''; const siteHandle = (site && site.slug && host) ? `@${site.slug}@${host}` : ''; const siteUrl = baseClean ? `${baseClean}/` : ''; const siteIcon = (site && site.profile_photo) || null; // Wat JIJ met deze reacties deed komt uit de tussentabel, niet meer uit // acted_* (shaer-ipb). Eén batch-lookup, want een drukke thread zou anders een // N+1 worden. De sleutel loopt door canonicalReactionUri, precies zoals aan de // schrijfkant -- staat dezelfde note toevallig ook in je tijdlijn, dan is het // één feit en niet twee knoppen die los van elkaar aan kunnen staan. const mijnSleutel = new Map(); for (const r of rows) { if (r.kind === 'reply' && r.object_uri) mijnSleutel.set(r.object_uri, canonicalReactionUri(site && site.slug, r.object_uri)); } const mijn = getReactionsFor(site && site.slug, [...mijnSleutel.values()]); const mijnReactie = (uri) => mijn.get(mijnSleutel.get(uri)) || { liked: false, boosted: false }; const nodes = []; for (const r of rows) { if (r.kind !== 'reply') continue; const ik = mijnReactie(r.object_uri); nodes.push({ noteId: r.object_uri, parent: r.parent_uri || null, mine: false, id: r.id, actor_uri: r.actor_uri, actor_name: r.actor_name, actor_handle: r.actor_handle, actor_url: r.actor_url, actor_icon: r.actor_icon, content: stripLeadingMentions(r.content), created_at: r.published || r.created_at, emoji_json: r.emoji_json, actor_emoji_json: r.actor_emoji_json, // FEP-9098 (thread render) acted_boost: ik.boosted, acted_like: ik.liked, children: [], }); } for (const o of s.listO.all(postId)) { nodes.push({ noteId: baseClean ? `${baseClean}/ap/notes/${o.id}` : o.id, parent: o.in_reply_to || null, mine: true, outboxId: o.id, content: stripLeadingMentions(o.content), created_at: o.created_at, media: (() => { try { return o.attachments ? JSON.parse(o.attachments) : []; } catch { return []; } })(), actor_name: siteName, actor_handle: siteHandle, actor_url: siteUrl, actor_icon: siteIcon, children: [], }); } const byId = new Map(nodes.map((n) => [n.noteId, n])); // Conversation partners per node (u02, the reply editor's mentions bar): the // node's author plus the ancestor authors up the chain. Our own nodes are // skipped (we do not mention ourselves), deduped by actor, capped at 8. for (const n of nodes) { const seen = new Set(); const list = []; let cur = n, guard = 0; while (cur && guard++ < 12 && list.length < 8) { if (!cur.mine && cur.actor_uri && !seen.has(cur.actor_uri)) { seen.add(cur.actor_uri); list.push({ uri: cur.actor_uri, url: cur.actor_url || cur.actor_uri, handle: cur.actor_handle || deriveHandle(cur.actor_uri), }); } cur = cur.parent ? byId.get(cur.parent) : null; } n.participants = list; } const isTop = (n) => !n.parent || n.parent === postNoteId || !byId.has(n.parent); const tops = []; for (const n of nodes) { if (isTop(n)) { tops.push(n); continue; } let anc = n, guard = 0; while (!isTop(anc) && guard++ < 12) anc = byId.get(anc.parent); anc.children.push(n); } const byTime = (a, b) => new Date(a.created_at) - new Date(b.created_at); tops.sort(byTime).forEach((t) => t.children.sort(byTime)); return { thread: tops, likeCount: rows.filter((r) => r.kind === 'like').length, announceCount: rows.filter((r) => r.kind === 'announce').length, total: nodes.length, }; } // ── HTTP Signatures + delivery ──────────────────────────────────── const slugFromActorUrl = (url) => { const m = String(url || '').match(/\/ap\/users\/([^/?#]+)/); return m ? decodeURIComponent(m[1]) : null; }; // Which of OUR sites are named in a note's Mention tags? Only hrefs on our own base count // (an /ap/users/ path on a remote host is someone else's actor), and the slug must be // an existing site. Deduped. export function localMentionSlugs(tags, base) { if (!base) return []; const out = [], seen = new Set(); for (const t of (Array.isArray(tags) ? tags : (tags ? [tags] : []))) { if (!t || t.type !== 'Mention' || typeof t.href !== 'string') continue; if (!t.href.startsWith(base + '/ap/users/')) continue; const slug = slugFromActorUrl(t.href); if (!slug || seen.has(slug)) continue; seen.add(slug); try { if (db.prepare('SELECT 1 FROM sites WHERE slug = ?').get(slug)) out.push(slug); } catch { /* ignore */ } } return out; } // Sign + POST an activity to a remote inbox (draft-cavage HTTP Signatures, RSA-SHA256). export async function deliver(inboxUrl, bodyObj, keyId, privatePem) { const body = JSON.stringify(bodyObj); const u = new URL(inboxUrl); const date = new Date().toUTCString(); const digest = 'SHA-256=' + crypto.createHash('sha256').update(body).digest('base64'); const signingString = `(request-target): post ${u.pathname}\nhost: ${u.host}\ndate: ${date}\ndigest: ${digest}`; const signature = crypto.sign('sha256', Buffer.from(signingString), privatePem).toString('base64'); const sig = `keyId="${keyId}",algorithm="rsa-sha256",headers="(request-target) host date digest",signature="${signature}"`; const r = await safeFetch(inboxUrl, { method: 'POST', headers: { 'Content-Type': 'application/activity+json', Accept: 'application/activity+json', Date: date, Digest: digest, Signature: sig }, body, }); return r.status; } export async function fetchActor(url, opts = {}) { // Authorized fetch (Mastodons secure mode): zo'n instance serveert zijn // actor-document -- en dus zijn publieke sleutel -- alleen aan een ONDERTEKEND // verzoek en antwoordt anders met 401. Zonder sleutel kunnen we een correct // ondertekende Follow van die instance niet verifiëren en wijzen we hem af, // waarna Mastodon het dagenlang blijft proberen. Gemeten op boiert.eu: vier // accounts eindeloos geweigerd, en precies die vier geven 401 op een // onbetekende GET (shaer-afq). // // Geen kip-ei: om ONZE handtekening te controleren haalt de andere kant ons // actor-document op, en dat serveert Klonkt publiek. // // ONBETEKEND EERST, en dat is een veiligheidskeuze en geen optimalisatie. // verifyRequest haalt de keyId-URL op VOORDAT er iets geverifieerd is, en die // URL komt uit een header die iedereen mag sturen. Tekenden we dat verzoek // standaard, dan kan een volslagen onbekende ons een ONDERTEKEND verzoek laten // sturen naar een adres van zijn keuze -- met onze identiteit eronder. Dat is // precies hoe een instance op een blocklist belandt. Ondertekenen doen we dus // pas als het onbetekend niet lukt, en dan alleen voor deze ene URL. let doc = null; try { const r = await safeFetch(url, { headers: { Accept: 'application/activity+json' } }); if (r.ok) { const len = Number(r.headers.get('content-length') || 0); if (len > 2_000_000) return null; // refuse oversized actor docs doc = await r.json(); } } catch { /* val door naar de ondertekende poging */ } // Genoeg? Dan klaar. Sommige instances serveren onbetekend wel een document // maar zonder sleutel; voor een verificatie hebben we daar niets aan, dus die // telt als mislukt. if (doc && (!opts.asSlug || (doc.publicKey && doc.publicKey.publicKeyPem))) return doc; if (!opts.asSlug) return doc; const signed = await signedGetJson(opts.asSlug, url).catch(() => null); return (signed && signed.id) ? signed : doc; } // ── Delivery queue with retries ─────────────────────────────────── // Outbound deliveries are tried immediately; on failure (down server, timeout, // non-2xx) they're queued and retried with backoff so a briefly-offline follower // doesn't silently miss the post. The signing key is NOT stored — the worker // re-derives it from the actor slug at send time. const DELIVERY_MAX_ATTEMPTS = 6; const DELIVERY_BACKOFF_MIN = [1, 5, 15, 60, 180, 360]; let _insDeliv, _dueDeliv, _delDeliv, _bumpDeliv; function deliveryStmts() { if (!_insDeliv) { _insDeliv = db.prepare('INSERT INTO ap_delivery (slug, inbox, body, attempts, next_at) VALUES (?,?,?,0,CURRENT_TIMESTAMP)'); _dueDeliv = db.prepare("SELECT * FROM ap_delivery WHERE datetime(next_at) <= datetime('now') ORDER BY next_at LIMIT 30"); _delDeliv = db.prepare('DELETE FROM ap_delivery WHERE id = ?'); _bumpDeliv = db.prepare('UPDATE ap_delivery SET attempts = ?, next_at = ? WHERE id = ?'); } return { ins: _insDeliv, due: _dueDeliv, del: _delDeliv, bump: _bumpDeliv }; } export function enqueueDelivery(slug, inbox, activity) { if (!slug || !inbox || !activity) return; try { deliveryStmts().ins.run(slug, inbox, JSON.stringify(activity)); } catch { /* ignore */ } } // Record delivery health per follower so the followers list can flag dead accounts. // Keyed by inbox: a shared-inbox POST reaches every follower behind it, so all of them // are marked. A non-follower inbox (inline @mention) simply matches 0 rows. let _fDelivOk, _fDelivErr; function markFollowerDelivery(slug, inbox, ok) { if (!slug || !inbox) return; try { if (!_fDelivOk) { _fDelivOk = db.prepare('UPDATE ap_followers SET last_delivery_at = CURRENT_TIMESTAMP WHERE slug = ? AND (inbox = ? OR shared_inbox = ?)'); _fDelivErr = db.prepare('UPDATE ap_followers SET last_error_at = CURRENT_TIMESTAMP WHERE slug = ? AND (inbox = ? OR shared_inbox = ?)'); } (ok ? _fDelivOk : _fDelivErr).run(slug, inbox, inbox); } catch { /* health tracking is non-fatal */ } } // Deliver now; queue for retry if it fails. export async function deliverWithRetry(slug, inbox, activity, keyId, privPem) { if (!inbox) return; try { const st = await deliver(inbox, activity, keyId, privPem); if (st >= 200 && st < 300) { markFollowerDelivery(slug, inbox, true); return; } } catch { /* queue below */ } enqueueDelivery(slug, inbox, activity); } let _processingDeliv = false; export async function processDeliveryQueue() { if (_processingDeliv) return; // re-entrancy guard: 30 rows × 8s can exceed the 60s tick → no double-delivery _processingDeliv = true; try { let rows; try { rows = deliveryStmts().due.all(); } catch { return; } if (!rows || !rows.length) return; const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); for (const row of rows) { let ok = false; try { const keys = getOrCreateKeys(row.slug); const st = await deliver(row.inbox, JSON.parse(row.body), `${actorId(base, row.slug)}#main-key`, keys.private_pem); ok = st >= 200 && st < 300; } catch { ok = false; } if (ok) { markFollowerDelivery(row.slug, row.inbox, true); deliveryStmts().del.run(row.id); continue; } const attempts = row.attempts + 1; if (attempts >= DELIVERY_MAX_ATTEMPTS) { markFollowerDelivery(row.slug, row.inbox, false); deliveryStmts().del.run(row.id); console.warn('[AP] delivery gave up after', attempts, 'tries →', row.inbox); continue; } // Index the backoff on the CURRENT attempt count (row.attempts) so the first // retry uses the 1-min tier instead of skipping it. const mins = DELIVERY_BACKOFF_MIN[Math.min(row.attempts, DELIVERY_BACKOFF_MIN.length - 1)]; deliveryStmts().bump.run(attempts, new Date(Date.now() + mins * 60000).toISOString(), row.id); } } finally { _processingDeliv = false; } } let _delivTimer = null; export function startDeliveryWorker() { if (_delivTimer) return; _delivTimer = setInterval(() => { processDeliveryQueue().catch(() => {}); }, 60 * 1000); if (_delivTimer.unref) _delivTimer.unref(); } /** Een lokale site om GETs mee te ondertekenen wanneer er geen specifieke is * (de gedeelde inbox). Gecached: dit draait per binnenkomend verzoek. */ let _signSlug; function anySigningSlug() { if (_signSlug !== undefined) return _signSlug; try { const r = db.prepare('SELECT slug FROM sites ORDER BY rowid LIMIT 1').get(); _signSlug = (r && r.slug) || null; } catch { _signSlug = null; } return _signSlug; } // Best-effort verification of an incoming signed request. Returns the sender's // actor doc if the signature checks out, else null. (Not gating yet — MVP.) // Max clock skew for the signed Date header (replay window). Generous default to tolerate // federating servers with drifting clocks; an operator can widen it via env. const SIG_MAX_SKEW_MS = (Number(process.env.AP_SIG_MAX_SKEW_MIN) || 60) * 60 * 1000; export async function verifyRequest(req, asSlug = null) { const sigH = req.headers['signature']; if (!sigH) return null; const p = Object.fromEntries([...sigH.matchAll(/([a-zA-Z]+)="([^"]*)"/g)].map((m) => [m[1], m[2]])); if (!p.keyId || !p.signature) return null; // Onderteken de sleutel-ophaal, anders faalt elke instance met authorized // fetch (shaer-afq). Zonder aangewezen site -- de gedeelde inbox -- tekenen we // als een willekeurige lokale actor: elke Klonkt-actor is een geldige // ondertekenaar, het gaat de andere kant er alleen om DAT er ondertekend is. const actor = await fetchActor(p.keyId.split('#')[0], { asSlug: asSlug || anySigningSlug() }); const pem = actor && actor.publicKey && actor.publicKey.publicKeyPem; if (!pem) return null; // Bind the key to the actor it speaks for. Without this we hand back whatever // `id` the fetched document claims, so anyone could host a document carrying a // VICTIM's id next to their OWN public key, sign with their own private half, // and be believed: the victim's server is never contacted. The caller decides on // `verified.id`, so the identity has to come from where the key was FETCHED, // never from what the document says about itself. // Adds conditions only, and there is no exemption list on purpose: an // "unless it's a known peer" escape hatch is exactly the door this closes. // Note this does not narrow what we accept in practice, since the line above // already requires the embedded publicKey object (an array or a bare URI // reference never worked here). const key = actor.publicKey; try { if (new URL(p.keyId).host !== new URL(actor.id).host) return null; // same origin as the key if (key.id && key.id !== p.keyId) return null; // this key, not a neighbour's if (key.owner && key.owner !== actor.id) return null; // and it belongs to this actor } catch { return null; } // unparseable id or keyId const hs = (p.headers || '(request-target) host date').split(/\s+/); // Behind a reverse proxy the raw Host header is the backend bind (e.g. localhost:3000, when // the proxy doesn't preserve it — Apache .htaccess [P] proxying), but the sender signed the // HTTP-Signature over the PUBLIC host. Try each candidate host (the configured PUBLIC_BASE_URL // host, the proxy's X-Forwarded-Host, and the raw Host) and accept if the signature verifies // against any. An attacker can't forge a match (no private key), so this only rescues the // legitimate proxied case. Also normalise a leading double-slash in the request-target. let _pubHost = null; if (process.env.PUBLIC_BASE_URL) { try { _pubHost = new URL(process.env.PUBLIC_BASE_URL).host; } catch { /* ignore */ } } const _hosts = [...new Set([_pubHost, req.headers['x-forwarded-host'], req.headers['host']].filter(Boolean))]; const _target = `${req.method.toLowerCase()} ${String(req.originalUrl || '').replace(/^\/{2,}/, '/')}`; const _sig = Buffer.from(p.signature, 'base64'); let ok = false; for (const _h of _hosts) { const line = hs.map((x) => x === '(request-target)' ? `(request-target): ${_target}` : x === 'host' ? `host: ${_h}` : `${x}: ${req.headers[x] || ''}`).join('\n'); try { if (crypto.verify('sha256', Buffer.from(line), pem, _sig)) { ok = true; break; } } catch { /* try next host */ } } // Replay defence: the Date header must be signed and recent. A captured signed request // replayed later (or with a swapped body) is rejected. if (ok) { if (!hs.includes('date')) ok = false; else { const t = Date.parse(req.headers['date'] || ''); if (isNaN(t) || Math.abs(Date.now() - t) > SIG_MAX_SKEW_MS) ok = false; } } // Digest is MANDATORY when the request carries a body: without a signed digest the body // isn't covered by the signature and could be swapped on a replay. if (ok && req.rawBody && req.rawBody.length) { if (!hs.includes('digest')) ok = false; else { const exp = 'SHA-256=' + crypto.createHash('sha256').update(req.rawBody).digest('base64'); if (req.headers['digest'] !== exp) ok = false; } } return ok ? actor : null; } // ── Authorized fetch for a single Note (2-8) ───────────────────── // Who may read this post's Note over AP GET? 'public' needs nobody; // friends-only (fan_only, Shaer's DEFAULT) needs a verified follower; // 'direct' is addressed to people and is never served over a GET at all. export function noteAudience(post) { if (!post) return 'direct'; if (post.ap_visibility === 'direct') return 'direct'; if (post.fan_only || post.ap_visibility === 'friends') return 'followers'; return 'public'; } // A follower earns the friends-only Note; a blocked actor gets the same // nothing as a stranger (the standing rule: a blocked actor's signed fetch // earns the empty set, gated server-side at serialisation). export function mayReadNote(site, post, actorUri) { const aud = noteAudience(post); if (aud === 'public') return true; if (aud === 'direct' || !site || !actorUri) return false; // FEP-1580: dezelfde regel als in outboxAudience, en hier net zo hard nodig. // De outbox geeft de LIJST vrij; zonder deze tak strandt de doelinstantie // alsnog op elke losse Note die niet publiek is. if (isMoveTarget(site.slug, actorUri)) return true; try { const blocked = db.prepare("SELECT 1 FROM ap_blocks WHERE slug = ? AND kind = 'actor' AND target = ?").get(site.slug, actorUri); if (blocked) return false; let host = null; try { host = new URL(actorUri).host; } catch { /* geen host, geen domein-block */ } if (host) { const dom = db.prepare("SELECT 1 FROM ap_blocks WHERE slug = ? AND kind = 'domain' AND target = ?").get(site.slug, host); if (dom) return false; } return !!db.prepare('SELECT 1 FROM ap_followers WHERE slug = ? AND actor_uri = ?').get(site.slug, actorUri); } catch { return false; } } // Parse a fediverse poll (an ActivityStreams `Question` — the Mastodon-standard poll form) // into our compact shape. `oneOf` = single choice, `anyOf` = multiple; each option is a Note // with a `name` and a `replies` collection whose `totalItems` is that option's vote count. function parsePoll(o) { if (!o || o.type !== 'Question') return null; const raw = Array.isArray(o.oneOf) ? o.oneOf : (Array.isArray(o.anyOf) ? o.anyOf : null); if (!raw || !raw.length) return null; const options = raw.slice(0, 12).map((opt) => ({ name: String((opt && opt.name) || '').slice(0, 300), count: Math.max(0, Number(opt && opt.replies && opt.replies.totalItems) || 0), })).filter((x) => x.name); if (!options.length) return null; const endTime = o.endTime || (typeof o.closed === 'string' ? o.closed : null); const closed = !!o.closed || (endTime ? Date.parse(endTime) <= Date.now() : false); return { multiple: Array.isArray(o.anyOf), options, endTime, closed, voters: Number(o.votersCount) || null, voted: null }; } // ── Polls WE host (a local post with a poll) ────────────────────── // Parse the poll definition stored on our own post (posts.poll_json). Counts are // NOT stored here — they're derived from the poll_votes ballots so a re-render always // reflects the authoritative tally. export function parseOwnPoll(pollJson) { if (!pollJson) return null; let d; try { d = typeof pollJson === 'string' ? JSON.parse(pollJson) : pollJson; } catch { return null; } if (!d || !Array.isArray(d.options)) return null; const options = d.options.map((o) => ({ name: String((o && o.name != null ? o.name : o) || '').slice(0, 300) })).filter((o) => o.name); if (options.length < 2) return null; const endTime = d.endTime || null; const closed = !!d.closed || (endTime ? Date.parse(endTime) <= Date.now() : false); return { multiple: !!d.multiple, options, endTime, closed }; } // Live tally of a hosted poll from its ballots: per-option counts + unique voters. export function pollTally(postId) { const counts = {}; let voters = 0; try { for (const r of db.prepare('SELECT choice, COUNT(*) AS n FROM poll_votes WHERE post_id = ? GROUP BY choice').all(postId)) counts[r.choice] = r.n; voters = db.prepare('SELECT COUNT(DISTINCT actor_uri) AS n FROM poll_votes WHERE post_id = ?').get(postId).n || 0; } catch { /* table may not exist yet */ } return { counts, voters }; } // Render-ready view of a hosted poll (options with counts + percentages, totals, state). // Voting is fediverse-only, so this is display-only on the site. export function ownPollView(post) { const poll = parseOwnPoll(post && post.poll_json); if (!poll) return null; const { counts, voters } = pollTally(post.id); const total = Object.values(counts).reduce((a, b) => a + b, 0); const denom = poll.multiple ? voters : total; // multiple-choice %: share of voters (can sum >100%) const options = poll.options.map((o) => { const count = counts[o.name] || 0; return { name: o.name, count, pct: denom ? Math.round((count / denom) * 100) : 0 }; }); return { multiple: poll.multiple, options, total, voters, endTime: poll.endTime, closed: poll.closed }; } // Attach the AS2 Question shape to a note built for a hosted poll. Mastodon renders a // status with either media OR a poll (never both), so a poll federates as content + // options with no media attachment. oneOf = single choice, anyOf = multiple. function applyPollToNote(note, postId, poll) { const { counts, voters } = pollTally(postId); const opts = poll.options.map((o) => ({ type: 'Note', name: o.name, replies: { type: 'Collection', totalItems: counts[o.name] || 0 }, })); note.type = 'Question'; note[poll.multiple ? 'anyOf' : 'oneOf'] = opts; if (poll.endTime) note.endTime = new Date(poll.endTime).toISOString(); // Once closed, Mastodon expects a `closed` timestamp (the effective end). if (poll.closed) note.closed = poll.endTime ? new Date(poll.endTime).toISOString() : new Date().toISOString(); note.votersCount = voters; delete note.attachment; // media ATTACHMENTS + a poll are mutually exclusive on Mastodon // Keep note.image: it's the cover, which Mastodon ignores on a Question anyway // (same as on any Note) but Klonkt reads to show the cover in feeds/the Cirkel. // Deleting it stripped the cover off every boosted poll. return note; } // Record an inbound ballot on one of OUR polls. A vote arrives as a Create(Note) whose // `name` is the chosen option and `inReplyTo` is our poll note — the Mastodon-standard // vote form. Returns { handled } — handled=true means it was addressed to a poll (so the // caller must NOT also store it as a reply), false means "not a poll, fall through". function recordPollBallot(postId, actorUri, rawChoice) { const choice = String(rawChoice == null ? '' : rawChoice).slice(0, 300); if (!choice) return { handled: false }; let post; try { post = db.prepare('SELECT poll_json FROM posts WHERE id = ?').get(postId); } catch { return { handled: false }; } const poll = post && parseOwnPoll(post.poll_json); if (!poll) return { handled: false }; // not a poll → let the reply logic handle it if (poll.closed) return { handled: true }; // voting closed → drop if (!poll.options.some((o) => o.name === choice)) return { handled: true }; // unknown option → drop try { // Single choice = one ballot per actor: ignore a later/different vote. Multiple choice // allows one ballot per distinct option (the UNIQUE(post,actor,choice) dedupes repeats). if (!poll.multiple && db.prepare('SELECT 1 FROM poll_votes WHERE post_id = ? AND actor_uri = ? LIMIT 1').get(postId, actorUri)) return { handled: true }; db.prepare('INSERT OR IGNORE INTO poll_votes (post_id, actor_uri, choice) VALUES (?, ?, ?)').run(postId, actorUri, choice); } catch { return { handled: true }; } schedulePollUpdate(postId); return { handled: true }; } // Coalesce a burst of votes into ONE Update(Question) per poll: the first vote schedules a // refresh ~15s out; further votes in that window ride the same pending update (which carries // the accumulated tally). Non-follower voters re-fetch the Question (live tally) themselves. const _pollUpdTimers = new Map(); function schedulePollUpdate(postId) { if (_pollUpdTimers.has(postId)) return; const t = setTimeout(() => { _pollUpdTimers.delete(postId); deliverPollUpdate(postId).catch(() => { /* best-effort */ }); }, 15000); if (t.unref) t.unref(); _pollUpdTimers.set(postId, t); } // Push the fresh poll tally (or closed state) to followers as Update(Question). export async function deliverPollUpdate(postId) { const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); if (!base || !postId) return; let post, site; try { post = db.prepare('SELECT * FROM posts WHERE id = ?').get(postId); if (!post || !post.poll_json) return; site = db.prepare('SELECT * FROM sites WHERE id = ?').get(post.site_id); } catch { return; } if (site) await deliverUpdate(site, post); } // ── Web push to the owner (docs/webpush-design.md, slice 3) ───────── // Fire-and-forget: a notification must never block or break inbox processing. function pushEvent(slug, event) { try { Push.notifySite(slug, event).catch(() => {}); } catch { /* never throw */ } wakeNews(slug); // long-poll waiters (Robins verzoek, 31-7): same moments as push } // ── Long-poll on news (Robins verzoek, 31-7) ───────────────────── // The app holds GET /ap/users/:slug/inbox/wait open; the moment anything // push-worthy lands for that account (a message, a reply, a wave, a help // request) every waiter is woken and the app re-reads its feed. In-process // on purpose: one Klonkt is one process, and a waiter is one callback. const _newsWaiters = new Map(); // slug -> Set export function onNews(slug, cb) { let set = _newsWaiters.get(slug); if (!set) { set = new Set(); _newsWaiters.set(slug, set); } set.add(cb); return () => { set.delete(cb); if (!set.size) _newsWaiters.delete(slug); }; } /** * Wachters op het Guardian-paneel (Barts opdracht, 9-8). * * APART VAN onNews, en dat is met opzet. `news` gaat over de tijdlijn; dit gaat * over alles wat een guardian te VERWERKEN krijgt -- een aanbod, een * volgverzoek, een gate-voorstel, een hulpvraag, een lapse. De guardianship- * module zendt daar al veertien soorten voor uit; die gingen alleen naar push, * en push kiest bewust maar een handvol. Het paneel moet ze allemaal weten. * * Een wachter wordt EEN keer gewekt en daarna vergeten: het antwoord dat volgt * is de nieuwe waarheid, en de client komt terug met een nieuwe wachter. */ const _guardWaiters = new Map(); // slug -> Set export function onGuardian(slug, cb) { let set = _guardWaiters.get(slug); if (!set) { set = new Set(); _guardWaiters.set(slug, set); } set.add(cb); return () => { set.delete(cb); if (!set.size) _guardWaiters.delete(slug); }; } export function wakeGuardian(slug) { const set = _guardWaiters.get(slug); if (!set || !set.size) return; const cbs = [...set]; set.clear(); _guardWaiters.delete(slug); for (const cb of cbs) { try { cb(); } catch { /* een wachter mag de rest nooit breken */ } } } export function wakeNews(slug) { const set = _newsWaiters.get(slug); if (!set || !set.size) return; const cbs = [...set]; set.clear(); _newsWaiters.delete(slug); for (const cb of cbs) { try { cb(); } catch { /* a waiter must never break the rest */ } } } // Path prefix for a site's pages. One instance is one owner, so the site // lives at the root; kept as a function because the push URLs read like // `${pushPrefix(slug)}/messages` all over this file. function pushPrefix() { return ''; } // Notification language: the site's content language (fallback: instance default). function pushLang(slug) { try { const r = db.prepare('SELECT language FROM sites WHERE slug = ?').get(slug); return (r && r.language) || process.env.KLONKT_DEFAULT_LANG || 'nl'; } catch { return 'nl'; } } // Site slug, target URL and title for a post-scoped notification. function pushPostCtx(postId) { try { const r = db.prepare('SELECT p.slug AS post, p.title, s.slug AS site FROM posts p JOIN sites s ON s.id = p.site_id WHERE p.id = ?').get(postId); if (!r) return null; return { site: r.site, title: r.title || r.post, url: `${pushPrefix(r.site)}/${r.post}#fediverse` }; } catch { return null; } } /** * Een DOORGESTUURDE activiteit alsnog verifiëren (shaer-s8k). * * Reageert iemand in een thread, dan stuurt de server van de oorspronkelijke * poster die reactie door naar de deelnemers -- en ondertekent met zijn EIGEN * sleutel. De handtekening klopt dan, maar de ondertekenaar is niet de auteur, * dus de gate hieronder wees hem af. Gevolg: reacties van derden kwamen niet * binnen, zonder dat iemand een fout zag. * * Mastodon lost dit op met een LD-Signature over de payload. Dat vraagt * JSON-LD-canonicalisatie; wij doen het lichter en strenger: we geloven de * bezorgde inhoud NIET en halen het object op bij de bron. * * Vier voorwaarden, en geen ervan is optioneel: * * 1. Alleen Create en Update. Een doorgestuurde Delete is per definitie niet te * dereferencen -- het object is weg -- dus die blijft geweigerd. * 2. De host van de object-id MOET die van de geclaimde actor zijn. Zonder dit * anker wijst een doorsturer je naar een host die hij zelf beheert, waar * attributedTo alles kan beweren. * 3. Het OPGEHAALDE object wordt gebruikt, niet de bezorgde payload. Anders * levert een doorsturer een echt id met verdraaide inhoud. * 4. Mislukt het ophalen, of wijst het object zichzelf niet toe aan de * geclaimde actor, dan blijft het een weigering. Geen twijfelgeval opslaan. */ /** Kennen we deze note? Een eigen post, een eigen outbox-antwoord, een * gecachete post in de tijdlijn, of een reactie die al in een thread van ons * staat. Alle vier zijn een geldige reden dat iemand ons een antwoord daarop * doorstuurt; iets anders is dat niet. */ function knownNoteUri(uri) { if (!uri || typeof uri !== 'string') return false; const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); try { if (base && uri.startsWith(`${base}/ap/notes/`)) { const seg = decodeURIComponent(uri.slice(`${base}/ap/notes/`.length).split(/[?#]/)[0]); if (db.prepare('SELECT 1 FROM ap_outbox WHERE id = ?').get(seg)) return true; if (db.prepare('SELECT 1 FROM posts WHERE id = ?').get(seg)) return true; } if (db.prepare('SELECT 1 FROM ap_timeline WHERE id = ? LIMIT 1').get(uri)) return true; if (db.prepare('SELECT 1 FROM ap_interactions WHERE object_uri = ? LIMIT 1').get(uri)) return true; // Een antwoord dat we al bezorgd kregen van iemand die we volgen (shaer-e9g). if (db.prepare('SELECT 1 FROM ap_seen_notes WHERE uri = ? LIMIT 1').get(uri)) return true; } catch { /* bij twijfel niet ophalen */ } return false; } /** * Onthoud dat we dit bericht al eens bezorgd kregen. * * Alleen de URI. Geen inhoud, niets op het scherm, geen tweede weergave -- dit * beantwoordt uitsluitend de vraag "kennen wij dit bericht?" die knownNoteUri * stelt voordat er iets bij de bron wordt opgehaald. * * De beller bepaalt WIE er onthouden wordt, en dat is de hele veiligheidsvraag: * onthouden we zomaar alles wat iemand aflevert, dan kan een vreemde eerst een * bericht neerleggen en daarna met een doorgestuurd antwoord dáárop ons naar een * adres van zijn keuze sturen. Vandaar dat handleInbox dit alleen doet voor * schrijvers die je zelf volgt. */ const SEEN_NOTES_DAYS = 30; let _seenSinceSnoei = 0; function rememberNoteUri(uri) { if (!uri || typeof uri !== 'string') return; try { db.prepare('INSERT OR IGNORE INTO ap_seen_notes (uri) VALUES (?)').run(uri); // Af en toe opruimen, niet bij het opstarten: een server die weken doorloopt // zou anders nooit snoeien. Doorsturen gebeurt kort na het antwoord, dus wat // ouder is dan een maand beantwoordt geen enkele vraag meer. if (++_seenSinceSnoei >= 500) { _seenSinceSnoei = 0; const r = db.prepare(`DELETE FROM ap_seen_notes WHERE created_at < datetime('now', '-${SEEN_NOTES_DAYS} days')`).run(); if (r.changes) console.log(`[AP] seen notes: ${r.changes} pruned`); } } catch { /* niet fataal */ } } const isFollowedActor = (uri) => { try { return !!db.prepare('SELECT 1 FROM ap_following WHERE actor_uri = ? LIMIT 1').get(uri); } catch { return false; } }; // Mislukte dereferences kort onthouden. Mastodon herhaalt een bezorging // dagenlang; zonder dit doet elke herhaling de fetch opnieuw, ook als die de // vorige twintig keer niets opleverde. Dempt meteen de scherpte van misbruik. const _derefMiss = new Map(); const DEREF_MISS_MS = 30 * 60 * 1000; function derefRecentlyFailed(uri) { const t = _derefMiss.get(uri); if (t === undefined) return false; if (Date.now() - t > DEREF_MISS_MS) { _derefMiss.delete(uri); return false; } return true; } function noteDerefFailure(uri) { if (_derefMiss.size > 500) { // simpele begrenzing: oudste helft eruit const oud = [..._derefMiss.entries()].sort((a, b) => a[1] - b[1]).slice(0, 250); for (const [k] of oud) _derefMiss.delete(k); } _derefMiss.set(uri, Date.now()); } async function dereferenceForwarded(act, claimedActor, type, slugParam) { // Every exit states its reason. Five of the six used to return silently, so a // rejection count could not be told apart from a narrowing that closed too far // — and that is exactly the measurement shaer-drf is waiting for. Bounded by // the signer-mismatch rate (tens per hour), so this is not a noisy log. const skipped = (reason, detail) => { console.log(`[AP] inbox forwarded, skipped (${reason}):`, claimedActor, detail || ''); return null; }; if (type !== 'Create' && type !== 'Update') return skipped('not Create/Update', type); const o = act && act.object; const objId = typeof o === 'string' ? o : (o && o.id); if (!objId || typeof objId !== 'string' || !/^https:\/\//i.test(objId)) return skipped('no https object id', objId || '(none)'); try { if (new URL(objId).host !== new URL(claimedActor).host) return skipped('host anchor', objId); // ankereis } catch { return skipped('unparsable id', objId); } // Alleen dereferencen als het object beweert een antwoord te zijn op iets van // ONS (shaer-drf). Zonder die eis zijn claimedActor en object.id allebei door // de aanvaller gekozen en eist het host-anker alleen dat ze aan elkaar gelijk // zijn -- dan kan iedereen met een werkende actor ons naar elke URL sturen. // Doorsturen bestaat juist omdát wij in de thread zitten, dus deze eis kost // niets aan legitiem verkeer waarvan we de ouder kennen. const parent = typeof o === 'object' && o ? (typeof o.inReplyTo === 'string' ? o.inReplyTo : (o.inReplyTo && o.inReplyTo.id)) : null; if (!knownNoteUri(parent)) return skipped('unknown inReplyTo', parent || '(none)'); if (derefRecentlyFailed(objId)) return skipped('recent failure', objId); // Onbetekend eerst; tekenen alleen als terugval. Anders kan een ander ons een // ONDERTEKEND verzoek naar een adres van zijn keuze laten sturen -- dezelfde // reden als bij fetchActor sinds efe5633. let fetched = await apGetJson(objId).catch(() => null); if (!fetched || fetched.id !== objId) { // The signer used to be slugParam, which is null on the shared inbox — and // that is where forwarded traffic lands, because we advertise a sharedInbox. // signedGetJson falls back to an unsigned GET for a null slug, so a source in // secure mode could never be dereferenced at all. Same fix verifyRequest got // in shaer-afq: any local actor is a valid signer. const asSlug = slugParam || anySigningSlug(); if (asSlug) fetched = await signedGetJson(asSlug, objId).catch(() => null); } const attributed = fetched && (typeof fetched.attributedTo === 'string' ? fetched.attributedTo : (fetched.attributedTo && fetched.attributedTo.id)); if (!fetched || fetched.id !== objId) { noteDerefFailure(objId); return skipped('fetch failed', objId); } if (attributed !== claimedActor) { // Not a transport hiccup: the source itself says someone else wrote this. noteDerefFailure(objId); return skipped('attributedTo mismatch', `${objId} claims ${attributed || '(none)'}`); } return fetched; } // Handle an incoming inbox POST. slugParam = null for the shared /ap/inbox. export async function handleInbox(req, slugParam, preVerified = null) { const act = req.body || {}; const type = act.type; // Real client IP (behind the proxy via `trust proxy`) — logged on dropped/rejected/ // ignored inbox hits so an operator can see who is probing their fediverse inbox. const ip = req.ip || (req.connection && req.connection.remoteAddress) || '?'; const base = (process.env.PUBLIC_BASE_URL || `${req.protocol}://${req.get('host')}`).replace(/\/+$/, ''); // preVerified is the loopback (see deliverToActor): a delivery between two // actors on THIS instance never crosses a socket, so there is no signature to // check — but we do know who signed, because we signed it. Handing that in // keeps everything below identical, including the actor-versus-signer check, // which is exactly the check that must not be skipped for being local. const verified = preVerified || await verifyRequest(req, slugParam).catch(() => null); // ENFORCE HTTP signatures: a data-affecting activity must be signed by the very // actor it claims to be. No valid signature, or signer ≠ actor → reject (no // forged replies/likes/follows/timeline posts). GET/discovery stays open. const claimedActor = typeof act.actor === 'string' ? act.actor : (act.actor && act.actor.id); // Blocked actor/domain → silently drop (202, don't reveal the block). if (claimedActor && isBlockedAny(claimedActor)) { console.log('[AP] inbox dropped (blocked)', claimedActor, 'from', ip); return 202; } const GATED = ['Create', 'Like', 'Announce', 'Follow', 'Delete', 'Undo', 'Accept', 'Reject', 'Add', 'Remove', 'Update', 'Flag', 'Offer', 'Move']; if (GATED.includes(type)) { // Een geldige handtekening van iemand anders dan de auteur is doorsturen, // geen vervalsing. Haal het object dan bij de bron op in plaats van het af // te wijzen; lukt dat niet, dan valt het door naar de weigering hieronder. let forwarded = null; if (verified && claimedActor && verified.id !== claimedActor) { forwarded = await dereferenceForwarded(act, claimedActor, type, slugParam).catch(() => null); if (forwarded) { act.object = forwarded; // de OPGEHAALDE inhoud, niet de bezorgde console.log('[AP] inbox forwarded, verified at the source:', type, claimedActor, 'via', verified.id); } } if (!forwarded && (!verified || !claimedActor || verified.id !== claimedActor)) { // Drie verschillende oorzaken, die eerder allemaal "unsigned/invalid" // heetten: geen handtekening meegestuurd, wel een handtekening maar niet // te verifiëren (meestal een opgeheven account waarvan de sleutel weg is), // of geldig ondertekend door iemand anders. const reden = verified ? '(signer mismatch)' : (req.headers && req.headers.signature) ? '(signature present, unverifiable)' : '(no signature)'; console.warn('[AP] inbox REJECTED (signature)', type, claimedActor || '?', 'from', ip, reden); return 401; } // One answer restores everything (FEP-633c 3.6): any VERIFIED activity // from an actor that guards someone here restores it to active for those // wards and cancels any lapse running against it, before the activity is // even looked at. Signature-gated on purpose: an unverified claim of // being gran must not wake gran up. try { const ev = Guardianship.availability.oneAnswer(claimedActor, Date.now()); if (ev.restored.length) console.log('[AP] guardian restored (one answer, 3.6):', claimedActor, '→', ev.restored.join(', ')); for (const c of ev.cancelledLapses) console.log('[AP] lapse cancelled by an answer from its target:', c.id); } catch { /* availability is never load-bearing for delivery */ } } // FEP-633c §5.3 (modelled on the adoption offer): a gated follow forwarded to // the guardians as an Offer(Follow), their Accept/Reject back to the ward. if ((type === 'Offer' || type === 'Accept' || type === 'Reject') && act['shaer:followApproval'] === true) { if (await handleFollowApprovalInbox(act, slugParam)) { console.log('[AP] follow-approval', type, 'from', claimedActor); return 202; } } // FEP-633c: the adoption handshake. An Offer lands at the local ward; an // Accept/Reject answers an offer a local guardian sent. Anything the // guardianship module does not recognize falls through to the old paths. // An Undo of the guardianship Relationship (§3.2) is handled here too, and it // must be seen BEFORE the generic Undo branch below, which only knows about // Follow/Like/Announce and would swallow it with a 202. if (type === 'Offer' || type === 'Accept' || type === 'Reject' || (type === 'Undo' && Guardianship.parseUndoRelationship(act))) { // Every LOCAL party this activity is addressed to gets its own copy of the // handshake (a ward and a co-guardian may both live here). Gather candidate // local slugs from the inbox owner, the `to` list, and the ward. // MET localSlugOf en niet met slugFromActorUrl. Dat laatste knipt alleen de // staart van een pad af, zonder naar de HOST te kijken -- en deze uri's // komen uit `to` en uit de relatie, dus van de afzender. Een Offer gericht // aan https://elders.example/ap/users/dev leverde zo de slug "dev" op, en // die bestaat hier. Dan draait onze dev de afhandeling van een activiteit // die nooit aan hem geadresseerd was. localSlugOf eist dat de uri met onze // eigen basis begint en dat de site echt bestaat. const cand = new Set(); if (slugParam) cand.add(slugParam); for (const t of (Array.isArray(act.to) ? act.to : (act.to ? [act.to] : []))) { if (typeof t === 'string') { const s = localSlugOf(t); if (s) cand.add(s); } } if (type === 'Offer' || type === 'Undo') { const rel = type === 'Undo' ? Guardianship.parseUndoRelationship(act) : Guardianship.parseRelationship(act.object); if (rel) { const s = localSlugOf(rel.ward); if (s) cand.add(s); } } let consumed = false; for (const slug of cand) { const gsite = db.prepare('SELECT * FROM sites WHERE slug = ?').get(slug); if (gsite && await Guardianship.handleGuardianshipInbox(gsite, act).catch(() => false)) consumed = true; } if (consumed) { console.log('[AP] guardianship', type, 'from', claimedActor); return 202; } } // A moderation report (Flag) about our content — store it for the targeted site's owner // (each Klonkt site is moderated by its own owner). Signature is enforced (GATED). if (type === 'Flag') { const objs = Array.isArray(act.object) ? act.object : (act.object ? [act.object] : []); const objectUris = objs.map((o) => (typeof o === 'string' ? o : (o && o.id))).filter(Boolean); let targetSlug = null; const noteIds = []; for (const u of objectUris) { const s = localSlugOf(u); // one of OURS -- host meegewogen if (s) { targetSlug = targetSlug || s; continue; } const pid = postIdFromNoteUrl(u, base); // one of our notes? if (pid) noteIds.push(pid); } if (!targetSlug && noteIds.length) { try { const r = db.prepare('SELECT s.slug FROM posts p JOIN sites s ON s.id = p.site_id WHERE p.id = ? LIMIT 1').get(noteIds[0]); if (r) targetSlug = r.slug; } catch { /* ignore */ } } if (!targetSlug) return 202; // not about us / can't tell → drop // Flag is GATED, so `verified` is the signer's (reporter's) actor doc already. const ai = actorInfo(verified || null, claimedActor); try { db.prepare('INSERT INTO ap_reports (slug, actor_uri, actor_name, actor_handle, actor_icon, content, objects, created_at) VALUES (?,?,?,?,?,?,?,CURRENT_TIMESTAMP)') .run(targetSlug, claimedActor || null, ai.name, ai.handle, ai.icon, HtmlSanitizerService.toPlainText(act.content || '').slice(0, 3000), JSON.stringify(objectUris.slice(0, 20))); console.log('[AP] report received for', targetSlug, 'from', claimedActor); } catch { /* ignore */ } return 202; } // FEP-7628 (DRAFT): an account moved house. Handled before Follow on purpose: // a Move often arrives seconds before the new actor's re-Follow wave, and the // swap below must not race our own outgoing Follow of the target. if (type === 'Move') { return handleMoveInbox(act, { verifiedActor: claimedActor }); } if (type === 'Follow') { const who = typeof act.actor === 'string' ? act.actor : (act.actor && act.actor.id); // EERST: volgt iemand onze BIBLIOTHEEK in plaats van onze actor? (shaer-0nh) // // Een luisteraar krijgt de muziek en NIET de gewone posts -- wie zich // abonneert op een platenkast heeft niet om de Krant gevraagd. Vandaar een // eigen tabel: zolang ze daar staan kan een postbezorging ze niet per // ongeluk meenemen. // // De bibliotheek is openbaar (alles erin is fedi_open), dus dit accepteert // meteen. Er valt niets goed te keuren, en dan is wachten oneerlijk. const libSlug = libraryOwnerSlug(typeof act.object === 'string' ? act.object : (act.object && act.object.id)); if (who && libSlug) { const remote = await fetchActor(who); if (!remote || !remote.inbox) return 202; const fi = actorInfo(remote, who); luisteraars.voegToe(libSlug, { actorUri: who, inbox: remote.inbox, sharedInbox: (remote.endpoints && remote.endpoints.sharedInbox) || null, name: fi.name, handle: fi.handle, icon: fi.icon, }); const keys = getOrCreateKeys(libSlug); const accept = { '@context': AP_CONTEXT, id: `${actorId(base, libSlug)}#accept-library-${Date.now()}-${rid()}`, type: 'Accept', actor: actorId(base, libSlug), object: act, }; deliver(remote.inbox, accept, `${actorId(base, libSlug)}#main-key`, keys.privatePem) .catch(() => { /* de volger staat er; een mislukte Accept mag dat niet omgooien */ }); console.log('[AP] library follow from', who, '->', libSlug); return 202; } // slugParam is de eigenaar van een per-actor inbox; op de GEDEELDE inbox is // die er niet en werd de slug uit act.object geraden. Zonder hostcontrole // kon een Follow op andermans actor met dezelfde padstaart hier een volger // opleveren. const slug = slugParam || localSlugOf(typeof act.object === 'string' ? act.object : (act.object && act.object.id)); if (!who || !slug) return 400; const remote = await fetchActor(who); if (!remote || !remote.inbox) return 202; // can't reach them → drop quietly const sharedInbox = (remote.endpoints && remote.endpoints.sharedInbox) || null; const fi = actorInfo(remote, who); // cache display for the friends list (shaer-aa3) // FEP-633c §5.3: if the followed actor is a WARD (has guardians), the // follow is gated. A committed guardian's own Follow is auto-accepted // (it needs no gate); anyone else is held pending for guardian approval. // Free actors / normal sites have no guardians → fall through, unchanged. const wardGuardians = Guardianship.listGuardians(slug).map((g) => g.other_uri); if (wardGuardians.length && !wardGuardians.includes(who)) { const followId = (typeof act.id === 'string' && act.id) || `${who}#follow-${Date.now()}-${rid()}`; Guardianship.follows.recordPending(slug, { id: followId, follower: who, inbox: remote.inbox, sharedInbox, name: fi.name, handle: fi.handle, icon: fi.icon, activity: act, }); // FEP-633c §5.3, modelled on the guardian offer: the ward forwards the // gated follow to its guardians for approval. A LOCAL guardian gets a // push and reads /guardian directly; a REMOTE guardian gets an // Offer(Follow) delivered so its instance stores a copy (same distributed // pattern as the adoption offer). On quorum the ward returns Accept(Follow). const wardActor = actorId(base, slug); const wardKeys = getOrCreateKeys(slug); const followObj = { id: followId, type: 'Follow', actor: who, object: wardActor }; // Dormancy evidence (FEP-633c 3.6.2): this decision directly addresses // every guardian. The ONLY admissible evidence is a request like this // one going unanswered; recordRequest itself skips a declared absence. for (const g of wardGuardians) { try { Guardianship.availability.recordRequest(slug, g, followId, Date.now()); } catch { /* never load-bearing */ } } for (const g of wardGuardians) { // Local ONLY when the guardian lives on THIS instance: slugFromActorUrl // ignores the host (an /ap/users/x path on a remote host is someone // else's actor), so also require our base + an existing local site. const gslug = g.startsWith(`${base}/`) ? slugFromActorUrl(g) : null; const isLocal = gslug && db.prepare('SELECT 1 FROM sites WHERE slug = ?').get(gslug); if (isLocal) { const L = pushLang(gslug); // Een volgverzoek is geen mede-voogdij. Deze push leende de tekst van // offer_for_ward en meldde dus een adoptie die niet gebeurde -- met de // volger als onderwerp. Eigen woorden, en allebei de namen erin: wie // er vraagt, en om wie het gaat (shaer-p729). pushEvent(gslug, { type: 'guardian', title: i18nT(L, 'push.n_guard_folin_t'), body: i18nT(L, 'push.n_guard_folin_b', { who: fi.name || fi.handle || i18nT(L, 'notif.someone'), ward: slug }), url: `${pushPrefix(gslug)}/guardian` }); } else { fetchActor(g).then((ga) => { const inbox = ga && ((ga.endpoints && ga.endpoints.sharedInbox) || ga.inbox); if (!inbox) return; const beslissend2 = Guardianship.gated.isDecisive(0, Guardianship.follows.followThreshold(guardians.length)); const offer = { '@context': AP_CONTEXT, id: `${wardActor}#followoffer-${Date.now()}-${rid()}`, type: 'Offer', actor: wardActor, to: [g], object: followObj, 'shaer:followApproval': true, 'shaer:decisive': beslissend2 }; deliverWithRetry(slug, inbox, offer, `${wardActor}#main-key`, wardKeys.private_pem).catch(() => {}); }).catch(() => {}); } } console.log('[AP] Follow', who, '→ ward', slug, '(gated, awaiting guardians)'); return 202; } // De eigenaarspoort (Robins wens, 18-8): met approve_followers aan wordt // een Follow niet automatisch geaccepteerd — hij wacht in dezelfde // wachtrij als een ward-follow, maar hier beslist de EIGENAAR, op // /connect. Zo kan niemand een klonkt zomaar aan een hub of ander // verzamelplatform hangen zonder dat de eigenaar ja heeft gezegd. // Wards vallen hier nooit: de guardianpoort hierboven gaat vóór. const ownerGate = db.prepare('SELECT approve_followers FROM sites WHERE slug = ?').get(slug); if (ownerGate && ownerGate.approve_followers) { const followId = (typeof act.id === 'string' && act.id) || `${who}#follow-${Date.now()}-${rid()}`; Guardianship.follows.recordPending(slug, { id: followId, follower: who, inbox: remote.inbox, sharedInbox, name: fi.name, handle: fi.handle, icon: fi.icon, activity: act, quorum: 'owner', }); const L = pushLang(slug); pushEvent(slug, { type: 'follow', title: i18nT(L, 'push.n_folreq_t'), body: i18nT(L, 'push.n_folreq_b', { who: fi.name || fi.handle || i18nT(L, 'notif.someone') }), url: `${pushPrefix(slug)}/connect`, }); console.log('[AP] Follow', who, '→', slug, '(awaiting owner approval)'); return 202; } fStmts().ins.run(slug, who, remote.inbox, sharedInbox, fi.name, fi.handle, fi.icon); try { _updFDisp.run(fi.name, fi.handle, fi.icon, slug, who); } catch { /* best effort */ } { const L = pushLang(slug); pushEvent(slug, { type: 'follow', title: i18nT(L, 'push.n_follow_t'), body: i18nT(L, 'push.n_follow_b', { who: fi.name || fi.handle || i18nT(L, 'notif.someone') }), url: `${pushPrefix(slug)}/connect` }); } const me = actorId(base, slug); const keys = getOrCreateKeys(slug); const accept = { '@context': AP_CONTEXT, id: `${me}#accept-${Date.now()}-${rid()}`, type: 'Accept', actor: me, object: act }; deliver(remote.inbox, accept, `${me}#main-key`, keys.private_pem).catch((e) => console.warn('[AP] Accept delivery failed:', e.message)); // Auto-backfill: send our recent posts as Create so the instance has our history // (Mastodon doesn't fetch history on follow). ONCE PER REMOTE INSTANCE only — // Mastodon dedupes notes per-instance, so re-filling an instance that already has // a follower of ours is wasted work (and won't re-populate the new follower's // timeline anyway). Deliver to the shared inbox (instance-level) when present. // Sync insert+check (no await between) → no interleave race with concurrent Follows. const instanceFilled = sharedInbox && db.prepare('SELECT 1 FROM ap_followers WHERE slug = ? AND shared_inbox = ? AND actor_uri != ? LIMIT 1') .get(slug, sharedInbox, who); if (!instanceFilled) { backfillNewFollower(base, slug, sharedInbox || remote.inbox).catch(() => { /* best-effort */ }); } console.log('[AP] Follow', who, '→', slug, verified ? '(sig ok)' : '(sig unverified)'); return 202; } // Een luisteraar die weggaat, hoort meteen weg te zijn. if (type === 'Undo' && act.object && act.object.type === 'Follow') { const doel = typeof act.object.object === 'string' ? act.object.object : (act.object.object && act.object.object.id); const libSlug = libraryOwnerSlug(doel); const wie = typeof act.actor === 'string' ? act.actor : (act.actor && act.actor.id); if (libSlug && wie && luisteraars.verwijder(libSlug, wie)) { console.log('[AP] library unfollow from', wie, '->', libSlug); return 202; } } if (type === 'Undo' && act.object) { const who = typeof act.actor === 'string' ? act.actor : (act.actor && act.actor.id); const ot = act.object.type; if (ot === 'Follow') { const obj = act.object.object; const slug = slugParam || slugFromActorUrl(typeof obj === 'string' ? obj : (obj && obj.id)); if (who && slug) { fStmts().del.run(slug, who); console.log('[AP] Unfollow', who, '→', slug); } return 202; } if (ot === 'Like' || ot === 'Announce') { const tgt = act.object.object; const pid = postIdFromNoteUrl(typeof tgt === 'string' ? tgt : (tgt && tgt.id), base); if (who && pid) { iStmts().delLA.run(ot.toLowerCase(), pid, who); console.log('[AP] Undo', ot, who, '→', pid); } return 202; } return 202; } const actorUri = typeof act.actor === 'string' ? act.actor : (act.actor && act.actor.id); const resolveActor = async (uri) => ((verified && verified.id === uri) ? verified : await fetchActor(uri).catch(() => null)); // Our OWN activity is already stored via ap_outbox: don't store it twice. // "Our own" means THIS inbox's owner, not "anyone who happens to live on this // machine". The old reading dropped every activity between two sites on one // instance, so a note from a co-located guardian to its ward was accepted // with a 202 and then quietly thrown away: no mention, no away, no help // request. Neighbours are not us (Robins regel, 29-7: on this machine // everything behaves as if every Klonkt were somewhere else). const isLocalActor = !!(actorUri && slugParam && actorUri === actorId(base, slugParam)); // Inbound reply: a Create whose object replies to one of our notes (post OR comment). if (type === 'Create' && act.object && TIJDLIJN_SOORTEN.has(act.object.type)) { const o = act.object; // A poll ballot: a Note carrying a `name` (the chosen option) inReplyTo one of OUR poll // posts. Record it (deduped per actor) BEFORE the reply logic so a vote is never stored // as a comment. recordPollBallot returns handled=false only if the target isn't a poll. if (o.name && o.inReplyTo && actorUri && !isLocalActor) { const seg = postIdFromNoteUrl(o.inReplyTo, base); if (seg && localPostExists(seg)) { const rec = recordPollBallot(seg, actorUri, o.name); if (rec.handled) { console.log('[AP] poll vote', actorUri, '→', seg); return 202; } } } const tgt = findThreadTarget(o.inReplyTo, base); if (tgt && actorUri && !isLocalActor) { const ai = actorInfo(await resolveActor(actorUri), actorUri); const html = HtmlSanitizerService.sanitize(o.content || ''); if (isRejectedObject(o.id)) { console.log('[AP] reply skipped (tombstoned)', o.id); return 202; } iStmts().ins.run('reply', tgt.post_id, o.id || '', actorUri, ai.name, ai.handle, ai.url, ai.icon, html, o.published || null, tgt.parent_uri, noteVisibility(o), extractEmojiTags(o.tag), emojiJsonOf(ai.emojis)); console.log('[AP] reply', actorUri, '→', tgt.post_id); // A reply is a post too: Berichten renders it the way de Krant renders a // timeline row, so it needs the same media and the same quote/preview card. { const where = 'kind = ? AND post_id = ? AND actor_uri = ? AND object_uri = ?'; const key = ['reply', tgt.post_id, actorUri, o.id || '']; const mj = mediaFromNote(o); if (mj && mj !== '[]') { try { db.prepare(`UPDATE ap_interactions SET media_json = ? WHERE ${where}`).run(mj, ...key); } catch { /* ignore */ } } resolveCard(o).then((c) => { if (!c) return; const col = c.column === 'quote_json' ? 'quote_json' : 'embed_json'; // never a value from the wire try { db.prepare(`UPDATE ap_interactions SET ${col} = ? WHERE ${where}`).run(c.json, ...key); } catch { /* ignore */ } }).catch(() => { /* best-effort */ }); } { // Private (followers/direct) replies push as a DM ping WITHOUT content // (the push service should never carry private text, design decision); // public replies carry a short snippet. const ctx = pushPostCtx(tgt.post_id); const vis = noteVisibility(o); const priv = vis === 'direct' || vis === 'followers'; if (ctx) { const L = pushLang(ctx.site); const who = ai.name || ai.handle || i18nT(L, 'notif.someone'); if (priv) pushEvent(ctx.site, { type: 'dm', title: i18nT(L, 'push.n_dm_t'), body: i18nT(L, 'push.n_dm_b', { who }), url: `${pushPrefix(ctx.site)}/messages` }); else pushEvent(ctx.site, { type: 'reply', title: i18nT(L, 'push.n_reply_t', { title: ctx.title }), body: `${who}: ${HtmlSanitizerService.toPlainText(html).slice(0, 90)}`, url: ctx.url }); } } return 202; } // Home timeline (client): a top-level post from an account we follow. if (actorUri && !isLocalActor && belongsInTimeline(o)) { let subs = []; try { subs = db.prepare('SELECT slug, auto_boost FROM ap_following WHERE actor_uri = ?').all(actorUri); } catch { /* table may not exist yet */ } if (subs.length) { const ai = actorInfo(await resolveActor(actorUri), actorUri); const { html, atts: _atts, url: _url } = timelineFields(o); const media = JSON.stringify(_atts); const poll = parsePoll(o); // a Question (fediverse poll) → cache its options/counts // "Feature" = show in the Cirkel (local only). We do NOT auto-Announce // incoming posts to the fediverse — that flooded followers. Boosting to the // fediverse is only ever a deliberate, manual per-post action (the 🔁 on // the timeline). for (const s of subs) { tlStmts().ins.run(o.id, s.slug, actorUri, ai.name, ai.handle, ai.icon, ai.url, html, _url, o.published || null, media, o.sensitive ? 1 : 0, contentWarning(o)); // FEP-633c §2.2: register the ward hint on the stored object (no action yet). if (Guardianship.objectHasGuardians(o)) { try { db.prepare('UPDATE ap_timeline SET has_guardians = 1 WHERE id = ? AND slug = ?').run(o.id, s.slug); } catch { /* ignore */ } } // FEP-9098: keep the note's custom-emoji tags so the C2S inbox read can serve them. { const ej = extractEmojiTags(o.tag); if (ej) { try { db.prepare('UPDATE ap_timeline SET emoji_json = ? WHERE id = ? AND slug = ?').run(ej, o.id, s.slug); } catch { /* ignore */ } } } storeAuthorEmoji(o.id, s.slug, ai); // custom-emoji display name for the byline // FEP-e232 + FEP-044f: keep the note's object-link/quote tags for the same read. { const lj = extractLinkJson(o); if (lj) { try { db.prepare('UPDATE ap_timeline SET link_json = ? WHERE id = ? AND slug = ?').run(lj, o.id, s.slug); } catch { /* ignore */ } } } if (poll) { try { db.prepare('UPDATE ap_timeline SET poll_json = ? WHERE id = ? AND slug = ?').run(JSON.stringify(poll), o.id, s.slug); } catch { /* ignore */ } } } // FEP-044f embedded quote card: resolve the quoted post out of band so // the inbox response is not blocked on a remote fetch. Best-effort. if (quoteHrefOf(o)) { const slugs = subs.map((s) => s.slug); resolveQuote(o).then((qj) => { if (!qj) return; for (const sl of slugs) { try { db.prepare('UPDATE ap_timeline SET quote_json = ? WHERE id = ? AND slug = ?').run(qj, o.id, sl); } catch { /* ignore */ } } }).catch(() => { /* best-effort */ }); } else { // No fediverse quote: try an EXTERNAL embed (oEmbed / known provider), // thumbnail-only. Also out of band, and stored for everyone; the gate // that decides who may SEE it is applied at serve time (§5.3-style // gated feature, see the inbox read). const slugs = subs.map((s) => s.slug); resolveExternalEmbed(o.content).then((ej) => { if (!ej) return; for (const sl of slugs) { try { db.prepare('UPDATE ap_timeline SET embed_json = ? WHERE id = ? AND slug = ?').run(ej, o.id, sl); } catch { /* ignore */ } } }).catch(() => { /* best-effort */ }); } console.log('[AP] timeline +', actorUri, 'x' + subs.length); } } // Een ANTWOORD van iemand die we volgen: bewaar de URI (shaer-e9g). Zo'n // bericht komt hier gewoon binnen, ondertekend door de schrijver zelf, maar // belongsInTimeline houdt het uit de Krant en daarna raakten we het kwijt. // Kwam er later een doorgestuurd antwoord OP dat bericht, dan kenden we de // ouder niet en wezen we het af -- terwijl we hem wel degelijk hadden gehad. // Er verandert niets aan wat we tonen of van vreemden aannemen: de schrijver // moet iemand zijn die je zelf bent gaan volgen. if (actorUri && !isLocalActor && o.id && o.inReplyTo && noteVisibility(o) !== 'direct' && isFollowedActor(actorUri)) { rememberNoteUri(o.id); } // Mentioned in a post that is NOT a reply to our content (a reply to us already returned // above): store a mention notification for each of our actors named in the Mention tags. // Requires our own base prefix on the tag href — /ap/users/ on a REMOTE host is // someone else's actor, not ours. // Een markering op een hulpvraag (shaer-lgo): een mede-guardian laat weten // dat hij ernaar kijkt, of dat het is afgehandeld. Gewone directe note met // een shaer:-markering, net als de zwaai -- dus die komt hier langs. VOOR de // mention-opslag, want dit is staat en geen bericht om te bewaren; de ward // krijgt hem wel als bericht te lezen, en dat gebeurt hieronder. if (actorUri && !isLocalActor) { const mark = Guardianship.help.parseMarker(o); if (mark) { const ai = actorInfo(await resolveActor(actorUri).catch(() => null), actorUri); Guardianship.help.record(mark.noteUri, actorUri, mark.kind, ai && ai.handle); wakeGuardian(slug); // een mede-guardian pakte iets op: het paneel hoort het meteen console.log('[AP] help', mark.kind, actorUri, '→', mark.noteUri); } } if (actorUri && !isLocalActor && o.id) { const slugs = localMentionSlugs(o.tag, base); if (slugs.length) { const ai = actorInfo(await resolveActor(actorUri), actorUri); const html = HtmlSanitizerService.sanitize(o.content || ''); // FEP-633c 5.2.1: a ward's call for help rides a direct mention; the // flag is stored so the Guardian PWA's message centre can list it. const help = Guardianship.isHelpRequest(o); const wave = Guardianship.isWave(o); const hasG = Guardianship.objectHasGuardians(o); // §2.2 hint, register-only // FEP-633c 3.6.1: a guardian declares itself away to its ward, on the // same direct note the mention below stores (so the kid also reads it // as an ordinary message). Recorded only from an actual guardian of // the addressed ward, and only with an end: an absence without an end // is logged and dropped, never guessed. if (Guardianship.availability.isAway(o)) { const until = Guardianship.availability.parseEndTime(o.endTime); for (const slug of slugs) { const isG = (() => { try { return Guardianship.listGuardians(slug).some((g) => g.other_uri === actorUri); } catch { return false; } })(); if (!isG) continue; if (!until || until <= Date.now()) { console.warn('[AP] away without a (future) end ignored (3.6.1):', actorUri, '→', slug); continue; } Guardianship.availability.declareAway(slug, actorUri, until); console.log('[AP] guardian declared away (3.6.1):', actorUri, '→', slug, 'until', new Date(until).toISOString()); } } // Een kind dat zelf om een poort vraagt (shaer-8ru). Zelfde weg als de // afwezigheidsmelding: een gewone directe note met een shaer:-markering, // per genoemde ontvanger afgehandeld. // // ALLEEN VAN EEN EIGEN WARD. Een verzoek van een vreemde is geen vraag // maar een onbekende die iets over jouw instellingen wil zeggen -- dat // hoort in geen enkele lijst te belanden waar een guardian op afgaat. { const req = Guardianship.gatereq.parseRequest(o); if (req) { for (const slug of slugs) { const mijn = (() => { try { return Guardianship.listWards(slug).some((w) => w.other_uri === actorUri); } catch { return false; } })(); if (!mijn) { console.warn('[AP] gate request from someone who is not our ward, ignored:', actorUri, '→', slug); continue; } Guardianship.gatereq.record(slug, actorUri, req.feature, o.id); wakeGuardian(slug); // het kind vroeg om een poort console.log('[AP] gate request', req.feature, actorUri, '→', slug); } } } for (const slug of slugs) { try { const r = db.prepare(`INSERT OR IGNORE INTO ap_mentions (slug, object_uri, note_url, actor_uri, actor_name, actor_handle, actor_icon, actor_url, content, published, help_request, wave, has_guardians, emoji_json, actor_emoji_json, media_json, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)`) .run(slug, o.id, safeUrl(o.url) || null, actorUri, ai.name, ai.handle, ai.icon, ai.url, html, o.published || null, help ? 1 : 0, wave ? 1 : 0, hasG ? 1 : 0, extractEmojiTags(o.tag), emojiJsonOf(ai.emojis), mediaFromNote(o)); if (r.changes) { // The quote / link-preview card resolves out of band (a remote // fetch), exactly as it does for a timeline post, so the inbox // answer is never blocked on it. resolveCard(o).then((c) => { if (!c) return; const col = c.column === 'quote_json' ? 'quote_json' : 'embed_json'; // never a value from the wire try { db.prepare(`UPDATE ap_mentions SET ${col} = ? WHERE slug = ? AND object_uri = ?`).run(c.json, slug, o.id); } catch { /* ignore */ } }).catch(() => { /* best-effort */ }); console.log('[AP] mention', actorUri, '→', slug, help ? '(help request)' : ''); const vis = noteVisibility(o); const priv = vis === 'direct' || vis === 'followers'; const L = pushLang(slug); const who = ai.name || ai.handle || i18nT(L, 'notif.someone'); // Same privacy rule as replies: private mentions push without content. // A help request pushes as its own alert type, aimed at the // Guardian PWA's message centre. if (help) pushEvent(slug, { type: 'help', title: i18nT(L, 'push.n_help_t'), body: i18nT(L, 'push.n_help_b', { who }), url: '/guardian' }); else if (priv) pushEvent(slug, { type: 'dm', title: i18nT(L, 'push.n_dm_t'), body: i18nT(L, 'push.n_dm_b', { who }), url: `${pushPrefix(slug)}/messages` }); else pushEvent(slug, { type: 'reply', title: i18nT(L, 'push.n_mention_t'), body: `${who}: ${HtmlSanitizerService.toPlainText(html).slice(0, 90)}`, url: `${pushPrefix(slug)}/messages` }); } } catch { /* ignore */ } } } } return 202; } // A remote post we cached was edited upstream → refresh our cached copy. This is the // push-based edit-sync that keeps the Cirkel/timeline fresh without polling (selfHeal // does it on a version bump; this does it live). Scope to the SIGNING actor so B can't // edit A's note (the signature gate guarantees claimedActor == the verified signer). if (type === 'Update' && act.object && (act.object.type === 'Note' || act.object.type === 'Article' || act.object.type === 'Question')) { const o = act.object; if (o.id && claimedActor) { const html = HtmlSanitizerService.sanitize(o.content || ''); const media = mediaFromNote(o); try { // Refresh url too (COALESCE keeps the old one if the Update omits it): a remote slug // rename keeps the same AP id but changes the human url, so without this the cached // post would keep linking to the old, now-dead URL. const r = db.prepare('UPDATE ap_timeline SET content = ?, media_json = ?, nsfw = ?, cw = ?, url = COALESCE(?, url) WHERE id = ? AND author_uri = ?') .run(html, media, o.sensitive ? 1 : 0, contentWarning(o), o.url || null, o.id, claimedActor); if (r.changes) console.log('[AP] timeline update', claimedActor, '→', o.id); // A poll's Update carries the fresh vote counts / closed state. Refresh per-row so each // site keeps its own `voted` state while the counts/closed update to the new totals. const poll = parsePoll(o); if (poll) { const rows = db.prepare('SELECT rowid AS rid, poll_json FROM ap_timeline WHERE id = ? AND author_uri = ?').all(o.id, claimedActor); const upd = db.prepare('UPDATE ap_timeline SET poll_json = ? WHERE rowid = ?'); for (const rw of rows) { let voted = null; try { voted = rw.poll_json ? (JSON.parse(rw.poll_json).voted || null) : null; } catch { /* ignore */ } upd.run(JSON.stringify({ ...poll, voted }), rw.rid); } } } catch { /* ignore */ } // If this note is a cached fediverse reply on one of our posts, refresh its text too. try { db.prepare('UPDATE ap_interactions SET content = ? WHERE object_uri = ? AND actor_uri = ?').run(html, o.id, claimedActor); } catch { /* ignore */ } } return 202; } if (type === 'Like' || type === 'Announce') { const tgt = act.object; const objUrl = typeof tgt === 'string' ? tgt : (tgt && tgt.id); const pid = postIdFromNoteUrl(objUrl, base); if (pid && actorUri && !isLocalActor && localPostExists(pid)) { // A boost/like of a non-public post is dropped, not stored: nobody // outside the audience should even hold it (shaer-tqc hardening). const vp = db.prepare('SELECT fan_only, ap_visibility FROM posts WHERE id = ?').get(pid); if (vp && (vp.fan_only || vp.ap_visibility === 'direct' || vp.ap_visibility === 'friends')) { console.log('[AP] dropped', type, 'on non-public post', pid); return; } const ai = actorInfo(await resolveActor(actorUri), actorUri); iStmts().ins.run(type.toLowerCase(), pid, '', actorUri, ai.name, ai.handle, ai.url, ai.icon, null, null, null, noteVisibility(act), null, emojiJsonOf(ai.emojis)); console.log('[AP]', type === 'Like' ? 'like' : 'boost', actorUri, '→', pid); { const ctx = pushPostCtx(pid); if (ctx) { const L = pushLang(ctx.site); const who = ai.name || ai.handle || i18nT(L, 'notif.someone'); if (type === 'Like') pushEvent(ctx.site, { type: 'like', title: i18nT(L, 'push.n_like_t'), body: i18nT(L, 'push.n_like_b', { who, title: ctx.title }), url: ctx.url }); else pushEvent(ctx.site, { type: 'boost', title: i18nT(L, 'push.n_boost_t'), body: i18nT(L, 'push.n_boost_b', { who, title: ctx.title }), url: ctx.url }); } } } else if (type === 'Announce' && objUrl && actorUri && !isLocalActor) { // A boost FROM an account we follow, of a REMOTE post → show it in the News feed. // We only STORE it for display; we NEVER auto-Announce it onward (anti-feedback-loop: // re-announcing an incoming Announce would cascade boosts across the network). let subs = []; try { subs = db.prepare('SELECT slug FROM ap_following WHERE actor_uri = ?').all(actorUri); } catch { /* table may not exist */ } if (subs.length) { const bn = await fetchNoteAP(objUrl); if (bn && bn !== 404 && (bn.type === 'Note' || bn.type === 'Article') && bn.id) { const origUri = actorUriOf(bn.attributedTo); // Block completeness: even if you follow the booster, drop a boost whose ORIGINAL // author is blocked — otherwise a block is bypassed via someone else's boost. if (origUri && isBlockedAny(origUri)) { console.log('[AP] timeline boost dropped (blocked origin)', origUri, 'via', actorUri); return 202; } const oai = actorInfo(await resolveActor(origUri), origUri); const html = HtmlSanitizerService.sanitize(bn.content || ''); const media = mediaFromNote(bn); const booster = actorInfo(await resolveActor(actorUri), actorUri); for (const s of subs) { // published = now → the boost shows as fresh activity at the top (Mastodon shows // reblogs at reblog-time, not the original's date). INSERT OR IGNORE: if we already // have the note (e.g. we also follow the author), keep it and DON'T relabel it. let inserted = false; try { const r = tlStmts().ins.run(bn.id, s.slug, origUri || '', oai.name, oai.handle, oai.icon, oai.url, html, bn.url || null, new Date().toISOString(), media, bn.sensitive ? 1 : 0, contentWarning(bn)); inserted = r.changes > 0; } catch { /* ignore */ } if (inserted) { try { db.prepare('UPDATE ap_timeline SET reblog_name = ?, reblog_handle = ?, reblog_icon = ?, reblog_emoji_json = ? WHERE slug = ? AND id = ?').run(booster.name, booster.handle, booster.icon, (booster.emojis && Object.keys(booster.emojis).length) ? JSON.stringify(booster.emojis) : null, s.slug, bn.id); } catch { /* ignore */ } } storeAuthorEmoji(bn.id, s.slug, oai); // custom-emoji display name for the byline // A boost carries the same renderable tags as a Create: capture the // note's content emojis (FEP-9098) and object links / quote (FEP-e232/ // 044f) so boosted posts render like any other, not as raw shortcodes. { const ej = extractEmojiTags(bn.tag); if (ej) { try { db.prepare('UPDATE ap_timeline SET emoji_json = ? WHERE id = ? AND slug = ?').run(ej, bn.id, s.slug); } catch { /* ignore */ } } } { const lj = extractLinkJson(bn); if (lj) { try { db.prepare('UPDATE ap_timeline SET link_json = ? WHERE id = ? AND slug = ?').run(lj, bn.id, s.slug); } catch { /* ignore */ } } } } // FEP-044f: resolve the embedded quote card for a boosted post too // (out of band, best-effort, so it does not block the inbox response). if (quoteHrefOf(bn)) { const slugs = subs.map((s) => s.slug); resolveQuote(bn).then((qj) => { if (!qj) return; for (const sl of slugs) { try { db.prepare('UPDATE ap_timeline SET quote_json = ? WHERE id = ? AND slug = ?').run(qj, bn.id, sl); } catch { /* ignore */ } } }).catch(() => { /* best-effort */ }); } console.log('[AP] timeline boost +', actorUri, 'x' + subs.length); } } } return 202; } if (type === 'Delete') { // A remote note was deleted upstream → drop it from replies AND the timeline. // Scope to the SIGNING actor so actor B can't delete actor A's content (the // signature gate guarantees claimedActor == the verified signer here). const oid = typeof act.object === 'string' ? act.object : (act.object && act.object.id); if (oid && claimedActor) { try { db.prepare('DELETE FROM ap_interactions WHERE object_uri = ? AND actor_uri = ?').run(oid, claimedActor); } catch { /* ignore */ } try { db.prepare('DELETE FROM ap_timeline WHERE id = ? AND author_uri = ?').run(oid, claimedActor); } catch { /* ignore */ } // Also clear a boost/like YOU made of this now-deleted remote post (the interact-page // ap_my_reactions state), so it can't stay stuck as "boosted" on a post that's gone. // Guard: only when the deleter owns the note's domain (B mustn't clear your reactions // to A's posts). try { let sameHost = false; try { sameHost = new URL(oid).host === new URL(claimedActor).host; } catch { sameHost = false; } if (sameHost) db.prepare('DELETE FROM ap_my_reactions WHERE target_uri = ?').run(oid); } catch { /* ignore */ } } return 202; } // Accept/Reject of a Follow WE sent (client side). if (type === 'Accept' && act.object) { const fid = typeof act.object === 'string' ? act.object : (act.object && act.object.id); let raak = 0; if (fid) { try { raak = fwStmts().acc.run(fid).changes; } catch { /* ignore */ } } // TERUGVAL, en die is nodig gebleken tegen Funkwhale. Een Accept hoort de // Follow terug te geven die hij beantwoordt, maar Funkwhale verzint er een // EIGEN id voor, in ONZE namespace: // // wij stuurden .../ap/users/dev#follow-1786161977286-bb2de32f // Funkwhale zegt .../ap/users/dev#follows/19fd8b00-8f66-... // // Matchen op follow_id raakt dan niets, en de volgrelatie bleef eeuwig op // 'pending' staan terwijl de logregel 'accepted' riep -- een stille no-op // die pas opviel toen er nooit iets binnenkwam. // // Het paar dat we WEL zeker weten is (deze site, deze actor): de Accept is // handtekening-geverifieerd, en actorUri is de ondertekenaar. Alleen een // rij die nog op pending staat wordt geraakt, dus dit kan niets anders // openzetten dan een follow die wij zelf hebben verstuurd. // // En de slug mag NIET van slugParam afhangen: Funkwhale bezorgt op de // GEDEELDE inbox, en dan is die leeg. Wie wij zijn staat in de ingesloten // Follow -- die hebben wij immers zelf verstuurd, dus `object.actor` is // onze eigen actor-URI. let mij = slugParam; if (!mij && act.object && typeof act.object === 'object') mij = slugFromActorUrl(act.object.actor); if (!raak && mij && actorUri) { try { raak = fwStmts().accByActor.run(mij, actorUri).changes; } catch { /* ignore */ } } // Eerlijk loggen: zonder treffer is er niets geaccepteerd, en dat hoort te // zien te zijn in plaats van als succes voorbij te komen. console.log('[AP] follow', raak ? 'accepted' : 'accept UNMATCHED', actorUri, fid ? '(' + fid + ')' : ''); // The moment a friendship exists is the moment the history comes along // (Robins besluit, 30-7): delivery cannot reach into the past, so the // fresh follower pulls the outbox, signed, and the other side now serves // the friends-only posts too. if (slugParam && actorUri) backfillFromOutbox(slugParam, actorUri).catch(() => { /* best-effort */ }); return 202; } if (type === 'Reject' && act.object) { const who = actorUri; if (who && slugParam) { try { fwStmts().del.run(slugParam, who); } catch { /* ignore */ } } return 202; } // Zeg ook WAT er viel. Een kale "Create (ignored)" verbergt het verschil // tussen een soort die we bewust overslaan en een die we niet kennen -- en // dat verschil was precies de vraag bij Funkwhale, dat Create(Audio) stuurt // waar deze inbox alleen Note, Article en Question aanneemt. const objType = act.object && typeof act.object === 'object' ? act.object.type : (typeof act.object === 'string' ? '' : null); console.log('[AP] inbox', type || 'unknown', objType ? '(' + objType + ')' : '', '→', slugParam || 'shared', 'from', ip, 'by', claimedActor || '?', '(ignored)'); return 202; } // ── Op slot na een verhuizing (FEP-7628) ────────────────────────── // // Een verhuisd account serveert `movedTo` en is daarmee dood verklaard. Toch kon // je er gewoon op posten, volgen, liken en reageren, en dat federeerde vrolijk // de wereld in. Drie dingen gaan daar mis: // // - Nieuwe posts krijgen een object-URI op een adres dat je hebt opgezegd. Die // URI's overleven het domein niet, en de reacties erop ook niet. // - Je volgers zijn al verhuisd, dus je post in het niets terwijl het lijkt of // je post. // - Een server die je movedTo ziet EN tegelijk verse activiteit van dat adres // krijgt, krijgt tegenstrijdige signalen over de verhuizing. // // Daarom staat de poort op de UITGAANDE kant en niet op de knoppen: een // C2S-client (Shaer) praat rechtstreeks met deze functies en zou anders zo langs // een verborgen knop lopen. De UI volgt de poort, niet andersom. // // WAT DICHT GAAT: posten, reageren, volgen, liken, boosten, stemmen, en een // tweede verhuizing. // WAT OPEN BLIJFT: alles wat de wegwijzer draagt (de actor, webfinger, je // bestaande posts, de outbox), alles inkomend (reacties op oude posts blijven // binnenkomen en leesbaar), je eigen beheer (archief exporteren, volglijst // downloaden), en ontvolgen -- opruimen mag altijd. // Rapporteren blijft OOK open: dat is een veiligheidsklep, geen inhoud maken. // // OMKEERBAAR: `moved_to` leegmaken heft het slot op. Een verhuizing kan mislukken // en dan moet je terug kunnen. export function movedLock(site) { const to = site && site.moved_to && /^https?:\/\//i.test(String(site.moved_to)) ? String(site.moved_to) : null; return to ? { locked: true, movedTo: to } : { locked: false, movedTo: null }; } /** Weigering in de vorm die de aanroepers al kennen: een object met `error`. */ function movedRefusal(site, wat) { const l = movedLock(site); if (!l.locked) return null; console.warn('[AP] geweigerd, dit account is verhuisd:', wat, '→', l.movedTo); return { error: 'moved', movedTo: l.movedTo }; } // Deliver a new post as Create(Note) to all followers' inboxes (fire-and-forget). // Needs PUBLIC_BASE_URL (absolute URLs); no-op without followers or base. export async function deliverCreate(site, post) { if (movedLock(site).locked) { console.warn('[AP] Create niet bezorgd, account verhuisd:', site && site.slug); return; } const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); if (!base || !site || !site.slug) return; // Resolve inline @user@host mentions → link them in the note + collect their inboxes, so a // mentioned person is notified even if they don't follow us (Mastodon-standard mention). const mres = await resolveMentionsInText(base, post.content || ''); let post2 = mres.inboxes.length ? { ...post, content: mres.html } : post; // FEP-044f: does this post quote a fediverse object? Resolve it once, here, // and remember it on the post, so buildNote (sync, also used by the outbox) // never has to fetch. The quoted author's inbox joins the delivery set: that // IS the notification. const quoteInboxes = []; // EERST BAKKEN, dan pas linken zoeken (shaer-k3f, gevonden op het toestel): // firstExternalUrl leest -ankers, en de web-editor bakt die er bij // het opslaan al in -- maar een post uit de APP is platte tekst waarin de // URL nog geen anker is. Zonder deze bak zag het C2S-pad dus nooit een link // en kreeg een app-post nooit een kaart, terwijl de preview hem net wel // beloofd had. const gebakken = bakePostContent(post2.content || ''); if (post2.quote_uri === undefined || post2.quote_uri === null) { const q = await resolveOwnQuote(gebakken); if (q) { try { db.prepare('UPDATE posts SET quote_uri = ?, quote_actor = ? WHERE id = ?').run(q.uri, q.actor || null, post.id); } catch { /* ignore */ } post2 = { ...post2, quote_uri: q.uri, quote_actor: q.actor || null }; } } // De kaart op de eigen post (shaer-k3f), langs dezelfde pijplijn als een // binnenkomende: een fediverse-quote wordt een quote-snapshot, anders // probeert de link een externe kaart. VOOR de vroege return hieronder, want // ook een post zonder volgers hoort zijn kaart te krijgen -- de app leest // hem uit de outbox, niet uit een bezorging. Best-effort en eenmalig: wat // hier niet lukt blijft een kale link, precies wat het was. if (!post2.quote_json && !post2.embed_json) { try { if (post2.quote_uri) { const qj = await resolveQuoteByUri(post2.quote_uri); if (qj) { db.prepare('UPDATE posts SET quote_json = ? WHERE id = ?').run(qj, post.id); post2 = { ...post2, quote_json: qj }; } } else { const ej = await resolveExternalEmbed(gebakken); if (ej) { db.prepare('UPDATE posts SET embed_json = ? WHERE id = ?').run(ej, post.id); post2 = { ...post2, embed_json: ej }; } } } catch { /* een kaart is nooit een blokkade voor de post zelf */ } } if (post2.quote_actor) { const a = await fetchActor(post2.quote_actor).catch(() => null); const inbox = a && ((a.endpoints && a.endpoints.sharedInbox) || a.inbox); if (inbox) quoteInboxes.push(inbox); } const followers = fStmts().list.all(site.slug); const inboxes = [...new Set([...followers.map((f) => f.shared_inbox || f.inbox), ...mres.inboxes, ...quoteInboxes].filter(Boolean))]; if (!inboxes.length) return; // no followers, no one mentioned, no one quoted const keys = getOrCreateKeys(site.slug); const keyId = `${actorId(base, site.slug)}#main-key`; const create = buildCreate(base, site, post2); for (const inbox of inboxes) deliverWithRetry(site.slug, inbox, create, keyId, keys.private_pem); } // On a new Follow, send that follower our most recent posts as Create so their // timeline shows our history (Mastodon does not backfill on follow). Oldest-first // so they sort into the follower's timeline at their original dates. async function backfillNewFollower(base, slug, inbox) { if (!base || !slug || !inbox) return; const site = db.prepare('SELECT * FROM sites WHERE slug = ?').get(slug); if (!site) return; // Deze lijst filterde op fan_only maar NIET op paid, en haalde `paid` ook niet // op -- dus stond post.paid op undefined, sloeg buildNote zijn redactie over, // en duwden we bij ELKE nieuwe volger twintig posts de deur uit met de // volledige tekst van de betaalde erbij. Een push, dus onherroepelijk: het // staat daarna in hun inbox. Zelfde reden voor ap_visibility, dat hier // helemaal ontbrak: een friends- of direct-post hoort niet in een backfill. // (Barts melding, 15 augustus 2026.) const recent = db.prepare( `SELECT id, slug, title, excerpt, content, cover_image_url, cover_video_url, nsfw, content_warning, c2s_attachments, published_at, created_at, fan_only, ap_visibility, paid, paid_min_cents FROM posts WHERE site_id = ? AND status = 'published' AND (fan_only IS NULL OR fan_only = 0) AND IFNULL(ap_visibility, 'public') = 'public' ORDER BY COALESCE(published_at, created_at) DESC LIMIT 20` ).all(site.id).reverse(); if (!recent.length) return; const keys = getOrCreateKeys(slug); const keyId = `${actorId(base, slug)}#main-key`; for (const p of recent) { try { await deliver(inbox, buildCreate(base, site, p), keyId, keys.private_pem); } catch { /* best-effort */ } await new Promise((r) => setTimeout(r, 150)); } console.log('[AP] backfilled', recent.length, 'posts to new follower of', slug); } // Tell followers a post is gone (Delete + Tombstone) so it's removed from their feeds. export async function deliverDelete(site, post) { const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); if (!base || !site || !site.slug || !post || !post.id) return; const followers = fStmts().list.all(site.slug); if (!followers.length) return; const inboxes = [...new Set(followers.map((f) => f.shared_inbox || f.inbox).filter(Boolean))]; const keys = getOrCreateKeys(site.slug); const me = actorId(base, site.slug); const nid = noteId(base, post.id); const del = { '@context': AP_CONTEXT, id: `${nid}#delete-${Date.now()}-${rid()}`, type: 'Delete', actor: me, to: [PUBLIC], object: { id: nid, type: 'Tombstone' }, }; for (const inbox of inboxes) deliverWithRetry(site.slug, inbox, del, `${me}#main-key`, keys.private_pem); } // Tell followers an already-published post changed (Update + edited Note) so // Mastodon refreshes the cached copy (e.g. after fixing content). export async function deliverUpdate(site, post) { const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); if (!base || !site || !site.slug || !post || !post.id) return; const mres = await resolveMentionsInText(base, post.content || ''); // link mentions + collect inboxes const post2 = mres.inboxes.length ? { ...post, content: mres.html } : post; const followers = fStmts().list.all(site.slug); const inboxes = [...new Set([...followers.map((f) => f.shared_inbox || f.inbox), ...mres.inboxes].filter(Boolean))]; if (!inboxes.length) return; const keys = getOrCreateKeys(site.slug); const me = actorId(base, site.slug); const note = buildNote(base, site, post2); note.updated = new Date().toISOString(); const update = { '@context': AP_CONTEXT, id: `${noteId(base, post.id)}#update-${Date.now()}-${rid()}`, type: 'Update', actor: me, to: [PUBLIC], cc: note.cc, object: note, }; for (const inbox of inboxes) deliverWithRetry(site.slug, inbox, update, `${me}#main-key`, keys.private_pem); } // Tell followers the ACTOR changed (Update + Person) so Mastodon re-processes the // account AND re-fetches the featured (pinned) collection — there is no standard // "featured changed" activity, so this is how a pin/unpin propagates promptly. export async function deliverActorUpdate(site) { const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); if (!base || !site || !site.slug) return; const followers = fStmts().list.all(site.slug); if (!followers.length) return; const inboxes = [...new Set(followers.map((f) => f.shared_inbox || f.inbox).filter(Boolean))]; const keys = getOrCreateKeys(site.slug); const me = actorId(base, site.slug); const update = { '@context': AP_CONTEXT, id: `${me}#update-${Date.now()}-${rid()}`, type: 'Update', actor: me, to: [PUBLIC], cc: [`${me}/followers`], object: buildActor(base, site), }; for (const inbox of inboxes) deliverWithRetry(site.slug, inbox, update, `${me}#main-key`, keys.private_pem); } // Reliably set the pinned order on followers' instances via Add/Remove activities // (how Mastodon itself federates pins) — pushed to the inbox + processed immediately, // unlike the featured COLLECTION which Mastodon caches with sticky StatusPins. // Mastodon's Add skips an already-pinned status, so we REMOVE every pin first, wait, // then ADD in rank-DESCENDING order (rank 1 added LAST → newest StatusPin → shown first, // because Mastodon displays pins newest-first). `alsoRemove` = ids to unpin too. // Serialize pin-resyncs per site: two concurrent /save calls would otherwise interleave // their Remove -> wait -> Add sequences and scramble the StatusPin order on Mastodon. A // resync already in flight for a site coalesces later requests into ONE rerun after it // finishes (accumulating their extra unpins), so rapid saves don't pile up N full resyncs. const _pinResync = new Map(); // slug -> { promise, pending, pendingRemove:Set, site } export function resyncFeaturedPins(site, alsoRemove = []) { if (!site || !site.slug) return Promise.resolve(); const slug = site.slug; const running = _pinResync.get(slug); if (running) { running.pending = true; running.site = site; // use the latest site object on the rerun for (const id of alsoRemove) running.pendingRemove.add(id); return running.promise; } const state = { promise: null, pending: false, pendingRemove: new Set(), site }; state.promise = (async () => { let extra = alsoRemove; for (;;) { try { await doResyncFeaturedPins(state.site, extra); } catch (e) { console.warn('[AP] pin resync failed:', e.message); } if (!state.pending) break; state.pending = false; extra = [...state.pendingRemove]; state.pendingRemove = new Set(); } _pinResync.delete(slug); })(); _pinResync.set(slug, state); return state.promise; } // The actual resync work — do NOT call directly; go through resyncFeaturedPins() above so // it stays serialized per site. async function doResyncFeaturedPins(site, alsoRemove = []) { const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); if (!base || !site || !site.slug) return; const followers = fStmts().list.all(site.slug); if (!followers.length) return; const inboxes = [...new Set(followers.map((f) => f.shared_inbox || f.inbox).filter(Boolean))]; const keys = getOrCreateKeys(site.slug); const me = actorId(base, site.slug); const keyId = `${me}#main-key`; const featured = `${me}/featured`; const note = (id) => noteId(base, id); const pinned = db.prepare( `SELECT id FROM posts WHERE site_id = ? AND status = 'published' AND (fan_only IS NULL OR fan_only = 0) AND pinned IS NOT NULL AND pinned > 0 ORDER BY pinned DESC, COALESCE(published_at, created_at) ASC LIMIT 20` ).all(site.id); const removeIds = [...new Set([...pinned.map((p) => p.id), ...alsoRemove])]; // 1. Remove every current pin so Mastodon can recreate them in order. for (const id of removeIds) { const rm = { '@context': AP_CONTEXT, id: `${me}#rm-${id}-${Date.now()}-${rid()}`, type: 'Remove', actor: me, object: note(id), target: featured, to: [PUBLIC] }; for (const inbox of inboxes) deliver(inbox, rm, keyId, keys.private_pem).catch(() => { /* best-effort */ }); } if (!pinned.length) { console.log('[AP] unpinned all featured for', site.slug); return; } await new Promise((r) => setTimeout(r, 5000)); // let the Removes land first // 2. Add in rank-DESC order, gaps so each StatusPin gets an increasing created_at. for (const p of pinned) { const add = { '@context': AP_CONTEXT, id: `${me}#add-${p.id}-${Date.now()}-${rid()}`, type: 'Add', actor: me, object: note(p.id), target: featured, to: [PUBLIC], cc: [`${me}/followers`] }; for (const inbox of inboxes) deliver(inbox, add, keyId, keys.private_pem).catch(() => { /* best-effort */ }); await new Promise((r) => setTimeout(r, 2000)); } console.log('[AP] resynced', pinned.length, 'featured pins for', site.slug); } // ── outbound replies (Klonkt → fediverse) ───────────────────────── const escHtml = (s) => String(s || '').replace(/[<>&]/g, (c) => ({ '<': '<', '>': '>', '&': '&' }[c])); const toISO = (v) => { if (!v) return new Date().toISOString(); const s = String(v); const d = new Date(/[TZ]/.test(s) ? s : s.replace(' ', 'T') + 'Z'); return isNaN(d) ? new Date().toISOString() : d.toISOString(); }; // Build one of OUR outbound reply Notes from an ap_outbox row. // Turn #hashtags in reply text into Mastodon-style hashtag links (clickable + federated). function linkHashtags(base, html) { // Prefix: start / whitespace / '>' / opening bracket — "(#tag" is a tag too. NO quote // chars in this class: a quote precedes attribute values (alt="#…"), which must not match. return String(html || '').replace(/(^|[\s>([{])#([\p{L}\p{M}\p{N}_]+)/gu, (m, pre, tag) => `${pre}`); } // Auto-link bare http(s) URLs in already-safe HTML (federated copies). Splits on existing // so a linked URL is never wrapped twice; requires start/whitespace/'>' before the // URL so attribute values (src="https://…") never match. Trailing sentence punctuation stays // outside the link (Mastodon-style). function linkUrls(html) { const parts = String(html || '').split(/(]*>[\s\S]*?<\/a>)/gi); for (let i = 0; i < parts.length; i++) { if (/^([{])(https?:\/\/[^\s<]+?)([.,;:!?)\]»]*)(?=$|[\s<])/g, (m, pre, url, trail) => `${pre}${url}${trail}`); } return parts.join(''); } // Linkify inline #hashtags and bare URLs in BODY html for on-site DISPLAY, using the // EXACT same rules as the federated copy (linkHashtags/linkUrls), so the website and the // Mastodon copy agree instead of the website showing raw text. Idempotent: existing // (editor links, embeds, shortcode buttons) are split out and left untouched, so // nothing is double-wrapped. Pass base='' → root-relative /tag/ links. export function linkifyBody(base, html) { const withTags = String(html || '') .split(/(]*>[\s\S]*?<\/a>)/gi) .map((seg) => (/^@([^<]+)<\/a>/gi; let m; while ((m = re.exec(content || ''))) { const href = m[1]; if (seen.has(href)) continue; seen.add(href); tags.push({ type: 'Mention', href, name: '@' + m[2] }); } return tags; } // Resolve inline @user@domain mentions in reply/post text → link them (href = actor URI) // and collect the mentioned actors' inboxes so they get notified. Best-effort per mention. async function resolveMentionsInText(base, html) { const inboxes = []; const handles = new Set(); // Prefix also allows opening brackets — "(@user@host + me)" is a mention too (real-world // miss: a bracketed mention federated as plain text and its target was never notified). const re = /(^|[\s>([{])@([\p{L}\p{M}\p{N}_.-]+@[\p{L}\p{M}\p{N}.-]+)/gu; let m; while ((m = re.exec(html || ''))) handles.add(m[2]); let out = String(html || ''); for (const h of handles) { let actorUri = null; try { actorUri = await webfingerResolve('@' + h); } catch { actorUri = null; } if (!actorUri) continue; const actor = await fetchActor(actorUri).catch(() => null); const inbox = actor && ((actor.endpoints && actor.endpoints.sharedInbox) || actor.inbox); if (inbox) inboxes.push(inbox); const profileUrl = actorInfo(actor, actorUri).url || actorUri; // human profile page → the link href const esc = h.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); out = out.replace(new RegExp('(^|[\\s>([{])@' + esc + '(?![\\p{L}\\p{M}\\p{N}_.-])', 'gu'), (full, pre) => `${pre}@${h}`); } return { html: out, inboxes }; } export function buildReplyNote(base, site, row) { // Thin delegate: replies are built by buildNote (the single Note entry point) in reply mode. return buildNote(base, site, row, { isReply: true }); } // The account's own outbound notes (replies and direct messages) as AS2 // Notes, newest first. The C2S inbox read serves these alongside the // timeline: without them your own reply existed everywhere EXCEPT in your // own app (Robins melding, 30-7: "replyen werkt nog niet"; het antwoord // stond op de server maar de app kreeg het nooit terug, dus je probeerde // het opnieuw en liep in de duplicate-guard). export function getSentNotes(base, site, limit = 60) { return db.prepare('SELECT * FROM ap_outbox WHERE site_slug = ? ORDER BY created_at DESC LIMIT ?') .all(site.slug, limit) .map((row) => buildReplyNote(base, site, row)); } // Resolve one of our outbound reply Notes by id (for /ap/notes/:id fallback). export function getOutboxNote(base, id) { const row = iStmts().getO.get(id); if (!row) return null; const site = db.prepare('SELECT * FROM sites WHERE slug = ?').get(row.site_slug); if (!site) return null; return buildReplyNote(base, site, row); } // ── ActivityPub Client-to-Server: ingest an activity POSTed to the outbox ── // The C2S counterpart of handleInbox: a native/web client (Shaer) posts an // activity here and we translate it onto the SAME delivery machinery the web UI // uses (deliverReply / sendInteraction / followActor / deliverCreate). Returns // { status, id?, url?, error? }. Auth + site-ownership are checked by the route. const c2sIdOf = (x) => (typeof x === 'string' ? x : (x && (x.id || x.href))) || null; export async function ingestOutboxActivity(site, user, activity) { const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); if (!base || !site || !activity || typeof activity !== 'object') return { status: 400, error: 'invalid_activity' }; // AP §6: a client MAY POST a bare object; the server wraps it in a Create. let type = activity.type; let object = activity.object; if (type === 'Note' || type === 'Article') { object = activity; type = 'Create'; } if (Array.isArray(type)) type = type.find((t) => typeof t === 'string'); // FEP-633c: the adoption handshake (Offer/Accept/Reject on a guardianship // Relationship) belongs to the guardianship module; anything else falls // through to the switch below. if (type === 'Offer' || type === 'Accept' || type === 'Reject') { const g = await Guardianship.handleGuardianshipOutbox(site, activity).catch(() => null); if (g) return g; } // Een gate-voorstel uit de app (5.6, shaer-8ru): een Offer van een // shaer:GatedSetting, de vorm die 5.6 al beschrijft. // // HIER, EN GEEN `case` IN DE SWITCH. Dat was hij eerst, en die claimde ELKE // Offer: wat geen gate-voorstel was kreeg 400 unsupported_offer -- ook de // adoptie-handshake, en straks elke Offer-vorm die we nog toevoegen. Barts // honderd aanbiedingen liepen er meteen op stuk. Alleen claimen wat je // herkent, en de rest laten doorlopen. if (type === 'Offer') { const gs = Guardianship.gated.parseGatedSetting(activity.object); if (gs) { const uit = proposeGate(site, gs.ward, gs.feature, gs.value); return uit.status === 200 ? { ...uit, status: 201, id: uit.offerId } : uit; } } try { switch (type) { case 'Create': { if (!object || typeof object !== 'object') return { status: 400, error: 'missing_object' }; // Innamepoorten (shaer-ahy.1, 8-8): wat de ward niet mag versturen // wordt HIER geweigerd, niet in de app verstopt -- een knop die de // client alleen verbergt is geen poort. De reddingsboei gaat ALTIJD // voor: een hulpvraag aan de guardians mag door elke dichte deur heen, // anders sluit een messages-poort precies het kanaal af dat het kind // veilig houdt. { const isWard = (() => { try { return Guardianship.listGuardians(site.slug).length > 0; } catch { return false; } })(); const isHelp = object['shaer:helpRequest'] === true || object.helpRequest === true; // Een poortverzoek van het kind zelf (shaer-8ru) gaat langs de // messages-poort. Dat lijkt een gat en is het niet: het verzoek draagt // ALLEEN de naam van de feature, geen vrije tekst, dus er ontstaat geen // kanaal om omheen die poort te praten. Zonder deze uitzondering kan // een kind met berichten dicht nergens meer om vragen -- en dan is de // hele weg dood op precies het moment dat hij nodig is. const isGateReq = !!Guardianship.gatereq.parseRequest(object); const direct = c2sVisibility(object) === 'direct'; if (!isHelp && !isGateReq) { if (direct && !Guardianship.wardGateAllowed(site.gate_messages, isWard)) { return { status: 403, error: 'gated_messages' }; } if (!direct && !object.inReplyTo && !Guardianship.wardGateAllowed(site.gate_compose, isWard)) { return { status: 403, error: 'gated_compose' }; } // Meedoen aan een gesprek is ook iets (Bart, 8-8). Hier stond de // aanname dat een antwoord geen eigen podium is en dus onder compose // door mocht. Dat is teruggedraaid: antwoorden heeft een EIGEN poort, // los van compose in beide richtingen -- je kunt willen dat een kind // meepraat zonder podium, en ook andersom. // // Geldt ook voor een DIRECT antwoord, bovenop de messages-poort: een // privé-antwoord is allebei, en dan mag allebei hem tegenhouden. if (object.inReplyTo && !Guardianship.wardGateAllowed(site.gate_replies, isWard)) { return { status: 403, error: 'gated_replies' }; } } } // Client sends `source` (plain/markdown) + `content` (HTML). deliverReply // re-escapes, so it needs plain text; a top-level post keeps sanitized HTML. const plain = (object.source && object.source.content) || HtmlSanitizerService.toPlainText(object.content || ''); // A picture (or a recording) can be the whole message: media-only // notes pass here; c2sCreatePost validates the attachments themselves. if (!plain.trim() && !object.content && !(Array.isArray(object.attachment) && object.attachment.length)) { return { status: 400, error: 'empty_note' }; } // Direct (private mention, shaer-tqc): NOT a post. Delivered over the // outbox machinery to the addressed inboxes only; shows under Messages. if (c2sVisibility(object) === 'direct') { const arr = (v) => (Array.isArray(v) ? v : (v ? [v] : [])).filter((x) => typeof x === 'string'); const recipients = [...new Set([...arr(object.to), ...arr(object.cc)])] .filter((u) => /^https?:\/\//i.test(u) && !/\/followers\/?$/.test(u) && u !== PUBLIC); if (!recipients.length) return { status: 400, error: 'no_recipients' }; // AS2 attachments (e.g. the help-buoy capture, uploaded via // uploadMedia): normalize our own absolute /media/ URLs to relative // so the deliverReply-style validation applies unchanged. const atts = (Array.isArray(object.attachment) ? object.attachment : []) .map((a) => a && typeof a === 'object' ? { url: String(a.url || '').replace(new RegExp('^' + base.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')), ''), mediaType: String(a.mediaType || ''), name: String(a.name || '').slice(0, 120), } : null) .filter(Boolean); const help = object['shaer:helpRequest'] === true || object.helpRequest === true; // FEP-633c 3.6.1: a guardian here declaring itself away to its // wards. An away without a (future) end fails loudly, exactly as // the daemon refuses it: stored quietly it would be a nominal // guardian holding a seat. let awayUntil = null; if (Guardianship.availability.isAway(object)) { awayUntil = Guardianship.availability.parseEndTime(object.endTime); if (!awayUntil || awayUntil <= Date.now()) return { status: 400, error: 'away_needs_an_end' }; // No local shortcut here: the note below reaches a ward on this // instance through the loopback, and its inbox handler applies the // absence like it does for a ward anywhere else. One path. } const gateReq = Guardianship.gatereq.parseRequest(object); // Een hulpvraag oppikken of afsluiten vanuit de app (5.2.1, shaer-lgo). // De markering IS al een gewone directe note met een shaer:-eigenschap, // dus hier hoeft niets nieuws bij: de app stuurt precies wat de PWA // stuurt, en het gaat over dezelfde bezorging naar de mede-guardians. // // We boeken hem ook LOKAAL. Zonder dat zou de guardian die de knop // indrukt zijn eigen markering pas zien als hij bij zichzelf // terugkomt -- en die weg bestaat niet. const mark = Guardianship.help.parseMarker(object); if (mark) { const base2 = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); // Met de VOLLEDIGE handle. Hier stond `@${site.slug}` -- zonder host, // dus een derde vorm naast de kale URI van de PWA-route en de echte // handle die een binnengekomen markering draagt. Drie spellingen van // dezelfde naam, en "door wie" was de hele vraag van shaer-lgo. const mij = actorId(base2, site.slug); Guardianship.help.record(mark.noteUri, mij, mark.kind, deriveHandle(mij)); } const r = await deliverDirectNote(site, { recipients, text: plain, language: object.language || null, inReplyTo: typeof object.inReplyTo === 'string' ? object.inReplyTo : null, attachments: atts, helpRequest: help, awayUntil, gateRequest: gateReq && gateReq.feature, helpMark: mark }); if (!r || !r.id) return { status: 502, error: 'direct_failed' }; return { status: 201, id: r.id, url: `${base}/ap/notes/${r.id}` }; } if (object.inReplyTo) { const parent = await resolveRemoteNote(c2sIdOf(object.inReplyTo), { asSlug: site.slug }).catch(() => null); if (!parent) return { status: 502, error: 'cannot_resolve_inReplyTo' }; // The attachments ride along (Robins melding, 30-7: "502 // reply_failed" op een reply met een foto): deliverReply validates // them itself (own /media only, image|audio|video, max 4) and a // media-only reply is a valid reply there. Dropping them here made // a photo reply arrive naked, and a photo-ONLY reply fail outright. const atts = (Array.isArray(object.attachment) ? object.attachment : []) .map((a) => a && typeof a === 'object' ? { url: String(a.url || '').replace(new RegExp('^' + base.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')), ''), mediaType: String(a.mediaType || ''), name: String(a.name || '').slice(0, 120), } : null) .filter(Boolean); // Honour the client's visibility for the reply: 'friends' (followers- // only, the Shaer detail-view Reply) drops Public; anything else stays // quiet-public. 'direct' was already handled above. const r = await deliverReply(site, { postId: parent.localPostId || '', postSlug: null, parent, text: plain, html: object.content || null, attachments: atts, language: object.language || null, visibility: c2sVisibility(object), }); if (!r || !r.id) return { status: 502, error: 'reply_failed' }; return { status: 201, id: r.id, url: `${base}/ap/notes/${r.id}` }; } return await c2sCreatePost(base, site, user, object); } // ── Gelezen tot hier (shaer-frontend-3tx) ─────────────────── // // AS2 kent Read: 'the actor has read the object'. Geen shaer:seen // verzinnen, en geen zetbare stand: dit is een GEBEURTENIS, dus twee // toestellen kunnen elkaar niet terugzetten. Blijft lokaal -- een // leesbevestiging heeft in de fediverse niets te zoeken. case 'Read': { const targetUri = c2sIdOf(object); if (!targetUri) return { status: 400, error: 'missing_object' }; const uit = markRead(site.slug, targetUri); // Kennen we die note niet, dan is er niets gelezen om te onthouden. // Geen fout: een client mag best een oud bericht aanwijzen. return { status: uit ? 200 : 202 }; } case 'Like': case 'Announce': { const targetUri = c2sIdOf(object); if (!targetUri) return { status: 400, error: 'missing_object' }; // A non-public local note cannot be boosted or liked into the open // (shaer-tqc hardening; the Mastodon 422 equivalent). const localPid = postIdFromNoteUrl(targetUri, base); if (localPid) { const p = db.prepare('SELECT fan_only, ap_visibility FROM posts WHERE id = ?').get(localPid); if (p && (p.fan_only || p.ap_visibility === 'direct' || p.ap_visibility === 'friends')) { return { status: 403, error: 'not_public' }; } } const note = await resolveRemoteNote(targetUri, { asSlug: site.slug }).catch(() => null); const objUri = (note && note.object_uri) || targetUri; const authorUri = note && note.actor_uri; const kind = type === 'Announce' ? 'boost' : 'like'; await sendInteraction(site, kind, objUri, authorUri); // Eén schrijfpad (shaer-9e9): tussentabel + afgeleide vlag in één keer. // De note gaat mee zodat een boost de post je tijdlijn in trekt. try { setReaction(site.slug, targetUri, kind, true, { flagUri: objUri, note: type === 'Announce' ? note : null }); } catch { /* non-fatal: een reactie mag nooit de bezorging blokkeren */ } // Een Like uit een app moet ook in ap_timeline.liked landen, want dat // is wat de C2S-tijdlijn als shaer:liked teruggeeft. Zonder dit werd // de reactie wel opgeslagen (setMyReaction, de webroute leest die), // maar kreeg de app altijd liked:false terug: het hartje sprong bij de // eerste herlaadbeurt uit, en un-liken kon niet meer -- de app bood // alleen nog "Like" aan en stuurde bij elke tik een nieuwe Like. // Anders dan bij een boost geen upsert: een like hoort een post niet // in je tijdlijn te trekken, dus staat de post er niet in, dan is dit // terecht een no-op. return { status: 202, url: objUri }; } case 'Follow': { const actorUri = c2sIdOf(object); if (!actorUri) return { status: 400, error: 'missing_object' }; // FEP-633c §5.3 outbound (shaer-p729): a ward asks its guardians first. // A held request is a THIRD outcome — not sent, not failed — and it // travels to the app as one, so Shaer can show "waiting" instead of a // tile that already looks followed. const held = await gateOutgoingFollow(site, actorUri); if (held) { return { status: 202, url: actorUri, id: held.id, state: held.status === 'denied' ? 'refused_by_guardian' : 'awaiting_guardian', }; } // The error REACHES the app (Robins melding, 31-7): swallowing it // made a failed follow look exactly like a successful one. const r = await followActor(site, actorUri); if (r && r.error) return { status: 502, error: 'follow_failed', detail: r.error }; return { status: 202, url: actorUri }; } // Shaer "in Orbit" = a real Block (FEP-c648 client side): lands in // ap_blocks, shows in the Block tab, and purges the actor's cached // content. Client-side filtering becomes a cache of this state. case 'Block': { const targetUri = c2sIdOf(object); if (!targetUri) return { status: 400, error: 'missing_object' }; const r = await blockTarget(site, targetUri); if (r && r.error) return { status: 400, error: r.error }; return { status: 202, url: targetUri }; } case 'Undo': { const inner = object && typeof object === 'object' ? object : null; let innerType = inner && inner.type; if (Array.isArray(innerType)) innerType = innerType.find((t) => typeof t === 'string'); const innerTarget = c2sIdOf(inner && inner.object); if (innerType === 'Follow') { await unfollowActor(site, innerTarget); return { status: 202, url: innerTarget }; } if (innerType === 'Block') { if (!innerTarget) return { status: 400, error: 'missing_object' }; unblock(site, innerTarget).catch(() => {}); // release from Orbit return { status: 202, url: innerTarget }; } if (innerType === 'Like' || innerType === 'Announce') { const kind = innerType === 'Announce' ? 'unboost' : 'unlike'; const note = await resolveRemoteNote(innerTarget, { asSlug: site.slug }).catch(() => null); const objUri = (note && note.object_uri) || innerTarget; await sendInteraction(site, kind, objUri, note && note.actor_uri); try { setReaction(site.slug, innerTarget, innerType === 'Announce' ? 'boost' : 'like', false, { flagUri: objUri }); } catch { /* non-fatal */ } return { status: 202, url: objUri }; } return { status: 400, error: 'unsupported_undo' }; } // Delete your OWN note (Robins verzoek, 30-7: long-press delete in de // app). Scope stays narrow: this account's posts and outbound replies, // nothing else. The web delete route is the model: Tombstone to the // followers first, then the cascade, so nobody keeps a live copy of a // post the child took back. case 'Delete': { const targetUri = c2sIdOf(object); if (!targetUri) return { status: 400, error: 'missing_object' }; const pid = postIdFromNoteUrl(targetUri, base); if (pid) { const post = db.prepare('SELECT * FROM posts WHERE id = ?').get(pid); if (post) { if (post.site_id !== site.id) return { status: 403, error: 'not_your_note' }; if (post.status === 'published') deliverDelete(site, post).catch(() => { /* best-effort */ }); db.transaction(() => { db.prepare('DELETE FROM comments WHERE post_id = ?').run(post.id); try { db.prepare('DELETE FROM posts_fts WHERE post_id = ?').run(post.id); } catch { /* FTS optional */ } db.prepare('DELETE FROM posts WHERE id = ?').run(post.id); })(); return { status: 202, url: targetUri }; } // Same /ap/notes/ namespace: one of our outbound replies/messages. // deliverOutboxDelete checks the site itself and tombstones too. if (await deliverOutboxDelete(site, pid)) return { status: 202, url: targetUri }; } return { status: 404, error: 'not_your_note' }; } // Update of arbitrary objects needs the post-edit pipeline; tracked // separately (klonkt-demo-c2s-del). Reject clearly rather than half-doing it. default: return { status: 400, error: 'unsupported_type', detail: String(type || 'none') }; } } catch (e) { console.warn('[AP] C2S ingest failed:', e && e.message); return { status: 500, error: 'ingest_error' }; } } // Create a top-level microblog post from a C2S Note and federate it. Minimal // sibling of the /posts/create route: sanitized HTML content, no title/cover. async function c2sCreatePost(base, site, user, object) { const html = HtmlSanitizerService.sanitize(object.content || (object.source && object.source.content) || ''); // Media on a top-level post (shaer-j3uh/-oqxk/-df3i): same rules as // deliverReply — only our OWN uploads, image/audio/video, max 4. They used // to be silently dropped here, so a photo post from the app arrived naked. const media = (Array.isArray(object.attachment) ? object.attachment : []) .filter((a) => a && typeof a.url === 'string' && /^\/media\/[\w./-]+$/.test(a.url) && /^(image|audio|video)\//.test(String(a.mediaType || ''))) .slice(0, 4) .map((a) => { const entry = { url: a.url, mediaType: String(a.mediaType), name: String(a.name || '').slice(0, 120) }; // The poster the upload leg made, when it did: a video's still frame // (shaer-zowq, .poster.jpg) or an audio's waveform (Robins vraag 30-7, // .poster.png). Rides along so the tag, the federated attachment and // the apps all have something to show instead of a bare box. const posterExt = entry.mediaType.startsWith('video/') ? '.poster.jpg' : entry.mediaType.startsWith('audio/') ? '.poster.png' : null; if (posterExt) { try { const mediaRoot = path.resolve(process.env.MEDIA_PATH || './storage/media'); const rel = entry.url.replace(/^\/media\//, ''); if (fs.existsSync(path.join(mediaRoot, rel + posterExt))) entry.poster = entry.url + posterExt; } catch { /* no poster is fine */ } } return entry; }); if (!html.trim() && !media.length) return { status: 400, error: 'empty_note' }; // The web reads the post's content, so the media goes IN it (we build these // tags ourselves from validated paths, after the sanitizer). buildNote // strips back out into AS2 attachments; audio/video tags stay for the // web player and federate via c2s_attachments below. const esc = (t) => String(t).replace(/&/g, '&').replace(/"/g, '"').replace(/ { if (a.mediaType.startsWith('image/')) return `

${esc(a.name)}

`; // data-poster: