| 1 | /**
|
|---|
| 2 | * ActivityPub — public endpoints (Phase 1: discover + fetch).
|
|---|
| 3 | *
|
|---|
| 4 | * GET /.well-known/webfinger?resource=acct:<slug>@<host>
|
|---|
| 5 | * GET /ap/users/:slug actor (content-negotiated: AP-JSON vs redirect to HTML profile)
|
|---|
| 6 | * GET /ap/users/:slug/outbox OrderedCollection of Create(Note)
|
|---|
| 7 | * GET /ap/users/:slug/followers count-only OrderedCollection
|
|---|
| 8 | * GET /ap/users/:slug/featured pinned posts (Mastodon "Featured" tab)
|
|---|
| 9 | * GET /ap/notes/:id a single Note
|
|---|
| 10 | * POST /ap/users/:slug/inbox, /ap/inbox → 202 (Follow/Accept + signature verify: next step)
|
|---|
| 11 | *
|
|---|
| 12 | * Mounted before resolveSite; resolves the site by slug itself.
|
|---|
| 13 | */
|
|---|
| 14 | import express from 'express';
|
|---|
| 15 | import { readFileSync } from 'fs';
|
|---|
| 16 | import db from '../config/database.js';
|
|---|
| 17 | import AP from '../services/ActivityPubService.js';
|
|---|
| 18 | import { apReadLimiter, apInboxLimiter } from '../middleware/rate-limit.js';
|
|---|
| 19 | import { apEnabled } from '../services/SettingsService.js';
|
|---|
| 20 | import OAuth from '../services/OAuthService.js';
|
|---|
| 21 | import * as Guardianship from '../services/guardianship/index.js';
|
|---|
| 22 | import { getPrimarySite } from '../middleware/site.js';
|
|---|
| 23 | import multer from 'multer';
|
|---|
| 24 | import path from 'path';
|
|---|
| 25 | import fs from 'fs';
|
|---|
| 26 | import { randomUUID } from 'crypto';
|
|---|
| 27 | import { mediaDir } from '../config/paths.js';
|
|---|
| 28 |
|
|---|
| 29 | const router = express.Router();
|
|---|
| 30 | // The whole fediverse layer can be turned off (solo "no federation" mode):
|
|---|
| 31 | // then /ap/*, WebFinger and NodeInfo are simply gone — the site is undiscoverable
|
|---|
| 32 | // and unfederatable. CRITICAL: this router is mounted at root (app.use(apRoutes)), so a
|
|---|
| 33 | // blanket res.status(404) here ran for EVERY request and 404'd the whole site when AP was
|
|---|
| 34 | // off. Use next('router') to SKIP this router entirely and let the normal routes handle it
|
|---|
| 35 | // (the /ap/* paths then fall through to the app's normal 404, which is correct).
|
|---|
| 36 | router.use((req, res, next) => { if (!apEnabled()) return next('router'); next(); });
|
|---|
| 37 | // Generous per-IP baseline over all /ap/* (reads). The inbox POST gets an
|
|---|
| 38 | // additional, tighter cap inline (it triggers outbound fetches).
|
|---|
| 39 | router.use(apReadLimiter);
|
|---|
| 40 | let _ver = '1.0.0';
|
|---|
| 41 | try { _ver = JSON.parse(readFileSync(new URL('../../package.json', import.meta.url))).version || _ver; } catch { /* keep default */ }
|
|---|
| 42 |
|
|---|
| 43 | const baseUrl = (req) => (process.env.PUBLIC_BASE_URL || `${req.protocol}://${req.get('host')}`).replace(/\/+$/, '');
|
|---|
| 44 | const hostOf = (req) => { try { return new URL(baseUrl(req)).host; } catch { return req.get('host'); } };
|
|---|
| 45 | const publicSite = (slug) => db.prepare('SELECT * FROM sites WHERE slug = ? AND (is_public IS NULL OR is_public = 1)').get(slug);
|
|---|
| 46 | // The primary site, via the one source of truth in middleware/site.js — which
|
|---|
| 47 | // falls back to the oldest site when nothing carries the is_primary flag. This
|
|---|
| 48 | // route used to keep its own is_primary-only copy, so a fresh instance whose
|
|---|
| 49 | // site was never flagged served its HTML at / (that resolver falls back) while
|
|---|
| 50 | // WebFinger and the actor route insisted it had no primary at all.
|
|---|
| 51 | const primarySlug = () => { const s = getPrimarySite(); return s && s.slug; };
|
|---|
| 52 | // A hostname as a human types it and as DNS stores it are the same host:
|
|---|
| 53 | // `🩵.is.wildenvrij.nl` IS `xn--zz9h.is.wildenvrij.nl`. WHATWG URL does the IDNA,
|
|---|
| 54 | // so compare the ASCII form and never the bytes the client happened to send.
|
|---|
| 55 | const asciiHost = (h) => {
|
|---|
| 56 | try { return new URL(`https://${h}`).host.toLowerCase(); } catch { return String(h).trim().toLowerCase(); }
|
|---|
| 57 | };
|
|---|
| 58 |
|
|---|
| 59 | // ── WebFinger ─────────────────────────────────────────────────────
|
|---|
| 60 | router.get('/.well-known/webfinger', (req, res) => {
|
|---|
| 61 | const m = String(req.query.resource || '').match(/^acct:([^@]+)@(.+)$/i);
|
|---|
| 62 | if (!m) return res.status(400).type('text/plain').send('bad resource');
|
|---|
| 63 | const user = m[1];
|
|---|
| 64 | let site = publicSite(user);
|
|---|
| 65 | // `acct:<host>@<host>` asks for this server's primary actor — the convention
|
|---|
| 66 | // Shaer's Handle relies on so a Ward is reachable without knowing anyone's
|
|---|
| 67 | // slug. Typing `🩵.is.wildenvrij.nl`, pasting `https://🩵.is.wildenvrij.nl`
|
|---|
| 68 | // (which the client's URL parser silently punycodes) and sending the xn--
|
|---|
| 69 | // form by hand are three spellings of one address; all arrive here with the
|
|---|
| 70 | // host sitting in the user position, and all must find the same actor.
|
|---|
| 71 | if (!site && asciiHost(user) === asciiHost(hostOf(req))) {
|
|---|
| 72 | const slug = primarySlug();
|
|---|
| 73 | if (slug) site = publicSite(slug);
|
|---|
| 74 | }
|
|---|
| 75 | if (!site) return res.status(404).end();
|
|---|
| 76 | res.type('application/jrd+json; charset=utf-8');
|
|---|
| 77 | res.set('Cache-Control', 'public, max-age=300');
|
|---|
| 78 | const actorUri = AP.actorId(baseUrl(req), site.slug);
|
|---|
| 79 | const profileUrl = baseUrl(req) + (site.slug === primarySlug() ? '/' : `/user/${encodeURIComponent(site.slug)}`);
|
|---|
| 80 | res.send(JSON.stringify({
|
|---|
| 81 | subject: `acct:${site.slug}@${hostOf(req)}`,
|
|---|
| 82 | aliases: [actorUri, profileUrl],
|
|---|
| 83 | links: [
|
|---|
| 84 | { rel: 'self', type: 'application/activity+json', href: actorUri },
|
|---|
| 85 | { rel: 'http://webfinger.net/rel/profile-page', type: 'text/html', href: profileUrl },
|
|---|
| 86 | ],
|
|---|
| 87 | }));
|
|---|
| 88 | });
|
|---|
| 89 |
|
|---|
| 90 | // ── Actor ─────────────────────────────────────────────────────────
|
|---|
| 91 | router.get('/ap/users/:slug', (req, res) => {
|
|---|
| 92 | const site = publicSite(req.params.slug);
|
|---|
| 93 | if (!site) return res.status(404).end();
|
|---|
| 94 | if (!AP.apWants(req)) {
|
|---|
| 95 | // A browser hit the AP actor URL → send them to the human profile.
|
|---|
| 96 | const human = site.slug === primarySlug() ? '/' : `/user/${encodeURIComponent(site.slug)}`;
|
|---|
| 97 | return res.redirect(302, baseUrl(req) + human);
|
|---|
| 98 | }
|
|---|
| 99 | site.primary_slug = primarySlug();
|
|---|
| 100 | AP.sendAP(res, AP.buildActor(baseUrl(req), site));
|
|---|
| 101 | });
|
|---|
| 102 |
|
|---|
| 103 | // ── Outbox ────────────────────────────────────────────────────────
|
|---|
| 104 | router.get('/ap/users/:slug/outbox', async (req, res) => {
|
|---|
| 105 | const site = publicSite(req.params.slug);
|
|---|
| 106 | if (!site) return res.status(404).end();
|
|---|
| 107 | // Authorized fetch (30-7): who is asking decides what they see.
|
|---|
| 108 | // - the owner's own app (bearer) and a verified accepted follower or
|
|---|
| 109 | // guardian get the friends-only history too, so a NEW friend's backfill
|
|---|
| 110 | // brings the past along (Robins besluit: vrienden krijgen de
|
|---|
| 111 | // geschiedenis mee);
|
|---|
| 112 | // - a verified caller this instance BLOCKS gets an EMPTY collection, not
|
|---|
| 113 | // even the public set: a block is a closed door, and a signed fetch is
|
|---|
| 114 | // the caller knocking with their name on it;
|
|---|
| 115 | // - everyone else gets the public collection, exactly as before.
|
|---|
| 116 | const bearer = OAuth.verifyBearer(req.headers.authorization);
|
|---|
| 117 | let verifiedActor = null;
|
|---|
| 118 | if (!bearer && req.headers['signature']) {
|
|---|
| 119 | const verified = await AP.verifyRequest(req).catch(() => null);
|
|---|
| 120 | verifiedActor = verified && verified.id;
|
|---|
| 121 | }
|
|---|
| 122 | const audience = AP.outboxAudience(req.params.slug, {
|
|---|
| 123 | bearerSlug: bearer ? bearer.site.slug : null,
|
|---|
| 124 | verifiedActor,
|
|---|
| 125 | });
|
|---|
| 126 | if (audience === 'blocked') {
|
|---|
| 127 | return AP.sendAP(res, AP.buildOutbox(baseUrl(req), site, []), 'private, no-store');
|
|---|
| 128 | }
|
|---|
| 129 | const fanClause = audience === 'friend' ? '' : "AND (fan_only IS NULL OR fan_only = 0)";
|
|---|
| 130 | const posts = db.prepare(
|
|---|
| 131 | `SELECT id, slug, title, content, cover_image_url, cover_video_url, nsfw, content_warning, c2s_attachments, published_at, created_at
|
|---|
| 132 | FROM posts WHERE site_id = ? AND status = 'published' ${fanClause}
|
|---|
| 133 | ORDER BY COALESCE(published_at, created_at) DESC LIMIT 20`
|
|---|
| 134 | ).all(site.id);
|
|---|
| 135 | const ob = AP.buildOutbox(baseUrl(req), site, posts);
|
|---|
| 136 | if (audience === 'friend') {
|
|---|
| 137 | // The owner's app builds its feed from this leg, and every note here is
|
|---|
| 138 | // by the site itself: give it the same `shaer:author` byline the timeline
|
|---|
| 139 | // entries carry, so your own cards get a header too (avatar + name).
|
|---|
| 140 | const me = AP.selfAuthor(baseUrl(req), site);
|
|---|
| 141 | for (const it of ob.orderedItems) {
|
|---|
| 142 | if (it && it.object && typeof it.object === 'object') it.object['shaer:author'] = me;
|
|---|
| 143 | }
|
|---|
| 144 | }
|
|---|
| 145 | AP.sendAP(res, ob, audience === 'friend' ? 'private, no-store' : undefined);
|
|---|
| 146 | });
|
|---|
| 147 |
|
|---|
| 148 | // ── Follow-QR (Robins verzoek, 31-7) ──────────────────────────────
|
|---|
| 149 | // The QR carries an HTTPS url, not the share: scheme: camera apps (Google
|
|---|
| 150 | // Lens voorop) treat unknown schemes as plain text and only offer to OPEN
|
|---|
| 151 | // https links (Robins melding, 31-7). The url lands on the interstitial
|
|---|
| 152 | // below, whose one big button fires the share: scheme — from a browser the
|
|---|
| 153 | // custom scheme DOES work (BROWSABLE intent-filter; Safari prompts).
|
|---|
| 154 | // Public on purpose: it encodes only the public handle, and the app's plain
|
|---|
| 155 | // image loaders carry no bearer.
|
|---|
| 156 | router.get('/ap/users/:slug/follow-qr.png', async (req, res) => {
|
|---|
| 157 | const site = db.prepare('SELECT slug FROM sites WHERE slug = ?').get(req.params.slug);
|
|---|
| 158 | if (!site) return res.status(404).end();
|
|---|
| 159 | try {
|
|---|
| 160 | const { default: QRCode } = await import('qrcode');
|
|---|
| 161 | const png = await QRCode.toBuffer(`${baseUrl(req)}/ap/users/${encodeURIComponent(site.slug)}/follow`, { width: 600, margin: 1 });
|
|---|
| 162 | res.set('Content-Type', 'image/png');
|
|---|
| 163 | res.set('Cache-Control', 'public, max-age=86400');
|
|---|
| 164 | res.send(png);
|
|---|
| 165 | } catch (e) {
|
|---|
| 166 | console.warn('[AP] follow-qr failed:', e && e.message);
|
|---|
| 167 | res.status(500).end();
|
|---|
| 168 | }
|
|---|
| 169 | });
|
|---|
| 170 |
|
|---|
| 171 | // The interstitial the QR opens: one big button into Shaer, and the handle
|
|---|
| 172 | // in plain sight for whoever has no Shaer (yet).
|
|---|
| 173 | router.get('/ap/users/:slug/follow', (req, res) => {
|
|---|
| 174 | const site = db.prepare('SELECT slug, title FROM sites WHERE slug = ?').get(req.params.slug);
|
|---|
| 175 | if (!site) return res.status(404).end();
|
|---|
| 176 | const host = new URL(baseUrl(req)).host;
|
|---|
| 177 | const esc = (t) => String(t).replace(/[<>&"]/g, (c) => ({ '<': '<', '>': '>', '&': '&', '"': '"' }[c]));
|
|---|
| 178 | const handle = `@${site.slug}@${host}`;
|
|---|
| 179 | const name = esc(site.title || site.slug);
|
|---|
| 180 | res.set('Cache-Control', 'public, max-age=3600');
|
|---|
| 181 | res.send(`<!doctype html><html lang="en"><head><meta charset="utf-8">
|
|---|
| 182 | <meta name="viewport" content="width=device-width, initial-scale=1">
|
|---|
| 183 | <title>Follow ${name}</title>
|
|---|
| 184 | <style>
|
|---|
| 185 | body { font-family: system-ui, sans-serif; margin: 0; min-height: 100vh; display: flex; align-items: center; justify-content: center;
|
|---|
| 186 | background: linear-gradient(160deg, #5A32E6, #2a1a5e); color: #fff; text-align: center; }
|
|---|
| 187 | main { padding: 32px; max-width: 420px; }
|
|---|
| 188 | h1 { font-size: 1.5rem; margin: 0 0 .4rem; }
|
|---|
| 189 | .handle { opacity: .85; font-family: ui-monospace, monospace; word-break: break-all; }
|
|---|
| 190 | a.go { display: block; margin: 28px auto 14px; padding: 16px 28px; border-radius: 999px; background: #fff; color: #2a1a5e;
|
|---|
| 191 | font-weight: 700; font-size: 1.15rem; text-decoration: none; }
|
|---|
| 192 | p.small { font-size: .85rem; opacity: .75; line-height: 1.5; }
|
|---|
| 193 | </style></head><body><main>
|
|---|
| 194 | <h1>Follow ${name}</h1>
|
|---|
| 195 | <div class="handle">${esc(handle)}</div>
|
|---|
| 196 | <a class="go" href="share:social/follow/AP/${esc(handle)}">Open in Shaer</a>
|
|---|
| 197 | <p class="small">No Shaer? Any fediverse app can follow ${esc(handle)}.</p>
|
|---|
| 198 | </main></body></html>`);
|
|---|
| 199 | });
|
|---|
| 200 |
|
|---|
| 201 | // ── Long-poll (owner only, Robins verzoek 31-7) ───────────────────
|
|---|
| 202 | // Hold the request until something push-worthy lands for this account, then
|
|---|
| 203 | // answer 200 (news: re-read your feed) or 204 after ~25s (nothing: re-arm).
|
|---|
| 204 | // The thread in the app stays live without interval polling.
|
|---|
| 205 | router.get('/ap/users/:slug/inbox/wait', (req, res) => {
|
|---|
| 206 | const auth = OAuth.verifyBearer(req.headers.authorization);
|
|---|
| 207 | if (!auth || auth.site.slug !== req.params.slug) return res.status(403).end();
|
|---|
| 208 | let settled = false;
|
|---|
| 209 | const done = (code) => {
|
|---|
| 210 | if (settled) return;
|
|---|
| 211 | settled = true;
|
|---|
| 212 | clearTimeout(timer);
|
|---|
| 213 | off();
|
|---|
| 214 | if (!res.headersSent) res.status(code).end();
|
|---|
| 215 | };
|
|---|
| 216 | const off = AP.onNews(auth.site.slug, () => done(200));
|
|---|
| 217 | const timer = setTimeout(() => done(204), 25_000);
|
|---|
| 218 | req.on('close', () => done(204));
|
|---|
| 219 | });
|
|---|
| 220 |
|
|---|
| 221 | // ── Blocked collection (owner only, AP §5.6) ──────────────────────
|
|---|
| 222 | // The server blocklist is the source of truth for Shaer's "in Orbit":
|
|---|
| 223 | // clients read it here instead of keeping their own state. Actor-kind
|
|---|
| 224 | // blocks only (domain blocks are instance policy, not an Orbit member).
|
|---|
| 225 | router.get('/ap/users/:slug/blocked', (req, res) => {
|
|---|
| 226 | const auth = OAuth.verifyBearer(req.headers.authorization);
|
|---|
| 227 | if (!auth || auth.site.slug !== req.params.slug) return res.status(403).end();
|
|---|
| 228 | const base = baseUrl(req);
|
|---|
| 229 | const items = AP.listBlocks(auth.site.slug)
|
|---|
| 230 | .filter((b) => b.kind === 'actor')
|
|---|
| 231 | .map((b) => b.target);
|
|---|
| 232 | AP.sendAP(res, {
|
|---|
| 233 | '@context': AP.AP_CONTEXT,
|
|---|
| 234 | id: `${base}/ap/users/${auth.site.slug}/blocked`,
|
|---|
| 235 | type: 'OrderedCollection',
|
|---|
| 236 | totalItems: items.length,
|
|---|
| 237 | orderedItems: items,
|
|---|
| 238 | });
|
|---|
| 239 | });
|
|---|
| 240 |
|
|---|
| 241 | // ── Guardian queues (owner only, FEP-633c, shaer:queues) ──────────
|
|---|
| 242 | // The dashboard collections the Shaer clients read: pending adoption offers,
|
|---|
| 243 | // gated follows (empty in Klonkt for now) and the guardian's wards. Same
|
|---|
| 244 | // contract as the Shaer test daemon.
|
|---|
| 245 | function queueRoute(name, build) {
|
|---|
| 246 | router.get(`/ap/users/:slug/queues/${name}`, (req, res) => {
|
|---|
| 247 | const auth = OAuth.verifyBearer(req.headers.authorization);
|
|---|
| 248 | if (!auth || auth.site.slug !== req.params.slug) return res.status(403).end();
|
|---|
| 249 | const base = baseUrl(req);
|
|---|
| 250 | const me = `${base}/ap/users/${auth.site.slug}`;
|
|---|
| 251 | AP.sendAP(res, { '@context': AP.AP_CONTEXT, ...build(`${me}/queues/${name}`, auth.site.slug, me) });
|
|---|
| 252 | });
|
|---|
| 253 | }
|
|---|
| 254 | queueRoute('offers', (id, slug, me) => Guardianship.offersCollection(id, slug, me));
|
|---|
| 255 | queueRoute('follows', (id, slug, me) => Guardianship.followsCollection(id, slug, me));
|
|---|
| 256 | // §5.3 turned around (shaer-p729): what this ward has asked to follow, still
|
|---|
| 257 | // waiting on its guardians. Owner-only like the rest — who a child wants to
|
|---|
| 258 | // follow is nobody else's business.
|
|---|
| 259 | queueRoute('outgoing-follows', (id, slug, me) => Guardianship.outgoingFollowsCollection(id, slug, me));
|
|---|
| 260 | queueRoute('wards', (id, slug) => Guardianship.wardsCollection(id, slug));
|
|---|
| 261 | // Availability (FEP-633c 3.6.1) is never public: the ward reads its
|
|---|
| 262 | // guardians' real states here and nowhere else.
|
|---|
| 263 | queueRoute('guardians', (id, slug) => Guardianship.guardiansCollection(id, slug));
|
|---|
| 264 |
|
|---|
| 265 | // ── Inbox read (owner only, AP C2S) ───────────────────────────────
|
|---|
| 266 | // GET on the inbox is part of ActivityPub C2S: the account owner (a bearer
|
|---|
| 267 | // scoped to this site) reads recent inbound posts (the timeline: accounts
|
|---|
| 268 | // they follow) as Create(Note) items, so an app (Shaer) can build a unified
|
|---|
| 269 | // feed. Anyone else gets 403; the inbox stays write-only for the public.
|
|---|
| 270 | router.get('/ap/users/:slug/inbox', async (req, res) => {
|
|---|
| 271 | const auth = OAuth.verifyBearer(req.headers.authorization);
|
|---|
| 272 | if (!auth || auth.site.slug !== req.params.slug) return res.status(403).end();
|
|---|
| 273 | const base = baseUrl(req);
|
|---|
| 274 | // Wachten is een UITBREIDING van deze lezing, geen tweede endpoint (shaer-n05).
|
|---|
| 275 | // Geef `since` (de shaer:cursor van je vorige antwoord) en `wait` mee, en het
|
|---|
| 276 | // antwoord blijft hangen tot er iets is of de tijd om is. Zonder die twee
|
|---|
| 277 | // gedraagt de route zich exact zoals altijd.
|
|---|
| 278 | //
|
|---|
| 279 | // Bewust hetzelfde antwoord in plaats van een "er is nieuws"-seintje: dan
|
|---|
| 280 | // hoeft er niets nieuws geparsed te worden, is er geen tweede beschrijving van
|
|---|
| 281 | // de kaartvorm die uit de pas kan lopen, en scheelt het de client een tweede
|
|---|
| 282 | // ronde.
|
|---|
| 283 | const wachtS = Math.min(Math.max(parseInt(req.query.wait, 10) || 0, 0), 50);
|
|---|
| 284 | if (req.query.since && wachtS > 0) {
|
|---|
| 285 | const afbreken = new AbortController();
|
|---|
| 286 | res.on('close', () => afbreken.abort()); // client hing op: niet doorgaan met wachten
|
|---|
| 287 | const uit = await AP.waitForFeedChange(auth.site.slug, {
|
|---|
| 288 | since: String(req.query.since), waitMs: wachtS * 1000, signal: afbreken.signal,
|
|---|
| 289 | });
|
|---|
| 290 | if (res.writableEnded || afbreken.signal.aborted) return undefined;
|
|---|
| 291 | // Niets veranderd? Dan een LEEG antwoord (Barts punt): de hele collectie
|
|---|
| 292 | // terugsturen terwijl er niets gebeurd is, is elke 25 seconden een tijdlijn
|
|---|
| 293 | // over de mobiele verbinding voor niets. Met 304 kost stilte niets en kost
|
|---|
| 294 | // nieuws nog steeds maar één rondje -- beter dan een apart seintje-endpoint,
|
|---|
| 295 | // dat voor nieuws twee rondjes nodig heeft.
|
|---|
| 296 | //
|
|---|
| 297 | // De '0'-uitzondering is geen franje. Ontbreekt ap_feed_state (een instance
|
|---|
| 298 | // die de migratie nog niet draaide), dan geeft feedCursor altijd '0' terug,
|
|---|
| 299 | // en zou een client hier eeuwig 304 krijgen en nooit meer inhoud zien. Bij
|
|---|
| 300 | // een lege merksteen sturen we dus gewoon de collectie.
|
|---|
| 301 | if (!uit.changed && uit.cursor !== '0') {
|
|---|
| 302 | res.set('Vary', 'Authorization');
|
|---|
| 303 | return res.status(304).end();
|
|---|
| 304 | }
|
|---|
| 305 | }
|
|---|
| 306 | // Gated feature (FEP-633c): may this account see EXTERNAL embeds? A ward's
|
|---|
| 307 | // world outside the fediverse is the guardians' call. The gate is applied
|
|---|
| 308 | // here, at serialisation: a blocked embed is never sent, because an embed the
|
|---|
| 309 | // client merely hides has still been delivered to the device.
|
|---|
| 310 | const isWard = (() => { try { return Guardianship.listGuardians(auth.site.slug).length > 0; } catch { return false; } })();
|
|---|
| 311 | const embedsAllowed = Guardianship.externalEmbedsAllowed(auth.site.external_embeds, isWard);
|
|---|
| 312 | // The heavier sibling (5.6): may a third party's PLAYER run inside the app,
|
|---|
| 313 | // and may a link hand the child over to a browser? Both are the guardians'
|
|---|
| 314 | // call, both default to off for a ward, and both need the preview gate open
|
|---|
| 315 | // first: you cannot play, or follow, what you may not see. Served here so
|
|---|
| 316 | // the app knows what it may offer instead of guessing.
|
|---|
| 317 | const playbackAllowed = embedsAllowed
|
|---|
| 318 | && Guardianship.externalPlaybackAllowed(auth.site.external_playback, isWard);
|
|---|
| 319 | const rows = AP.getTimeline(auth.site.slug, 60);
|
|---|
| 320 | // Eén query voor de hele pagina (shaer-9e9 fase 2): shaer:liked komt uit de
|
|---|
| 321 | // tussentabel, de bron van waarheid, en niet meer uit de afgeleide kolom op
|
|---|
| 322 | // ap_timeline. Per rij vragen zou hier een N+1 opleveren.
|
|---|
| 323 | const reacties = AP.getReactionsFor(auth.site.slug, rows.map((t) => t.id));
|
|---|
| 324 | const posts = rows.map((t) => ({
|
|---|
| 325 | id: `${t.id}#create`,
|
|---|
| 326 | type: 'Create',
|
|---|
| 327 | actor: t.author_uri,
|
|---|
| 328 | published: t.published || t.created_at || undefined,
|
|---|
| 329 | object: {
|
|---|
| 330 | id: t.id,
|
|---|
| 331 | type: 'Note',
|
|---|
| 332 | attributedTo: t.author_uri,
|
|---|
| 333 | content: t.content,
|
|---|
| 334 | url: t.url || undefined,
|
|---|
| 335 | published: t.published || t.created_at || undefined,
|
|---|
| 336 | sensitive: !!t.nsfw,
|
|---|
| 337 | summary: t.cw || undefined,
|
|---|
| 338 | // Friends' media travels along (media_json → AS2 attachment), so the
|
|---|
| 339 | // client renders their images/audio like own outbox posts.
|
|---|
| 340 | attachment: AP.timelineAttachments(t.media_json),
|
|---|
| 341 | // The note's preserved tags, so the client can render them: FEP-9098
|
|---|
| 342 | // Emoji tags (:shortcode: → image) and FEP-e232 Link tags (quotes /
|
|---|
| 343 | // inline object references). Combined into one `tag` array; omitted
|
|---|
| 344 | // when the note has neither.
|
|---|
| 345 | tag: (() => {
|
|---|
| 346 | const tags = [...(AP.timelineEmojis(t.emoji_json) || []), ...(AP.timelineObjectLinks(t.link_json) || [])];
|
|---|
| 347 | return tags.length ? tags : undefined;
|
|---|
| 348 | })(),
|
|---|
| 349 | // FEP-044f: the resolved quoted post (author + content), so the client
|
|---|
| 350 | // renders an embedded quote card instead of a bare link. Omitted when the
|
|---|
| 351 | // note has no quote or the quoted post could not be resolved.
|
|---|
| 352 | 'shaer:quote': AP.timelineQuote(t.quote_json),
|
|---|
| 353 | // The post author's display info (name / @handle / avatar), so every card
|
|---|
| 354 | // gets a byline header like the quote card. attributedTo stays the bare
|
|---|
| 355 | // actor URI; this is the resolved presentation Klonkt already stored.
|
|---|
| 356 | 'shaer:author': (t.author_name || t.author_handle || t.author_icon) ? {
|
|---|
| 357 | name: t.author_name || undefined, handle: t.author_handle || undefined,
|
|---|
| 358 | icon: t.author_icon || undefined, url: t.author_url || undefined,
|
|---|
| 359 | // FEP-9098: emojis in the display name (":shortcode:"), if any.
|
|---|
| 360 | emojis: (() => { try { return t.author_emoji_json ? JSON.parse(t.author_emoji_json) : undefined; } catch { return undefined; } })(),
|
|---|
| 361 | } : undefined,
|
|---|
| 362 | // When a followed account boosted this, who did ("X boosted"). Omitted for
|
|---|
| 363 | // ordinary posts.
|
|---|
| 364 | 'shaer:booster': (t.reblog_name || t.reblog_handle || t.reblog_icon) ? {
|
|---|
| 365 | name: t.reblog_name || undefined, handle: t.reblog_handle || undefined,
|
|---|
| 366 | icon: t.reblog_icon || undefined,
|
|---|
| 367 | // FEP-9098: emojis in the booster's display name (":shortcode:"), if any.
|
|---|
| 368 | emojis: (() => { try { return t.reblog_emoji_json ? JSON.parse(t.reblog_emoji_json) : undefined; } catch { return undefined; } })(),
|
|---|
| 369 | } : undefined,
|
|---|
| 370 | // Whether THIS account already liked/boosted the note, so the app's
|
|---|
| 371 | // detail-view buttons show the current state (and can toggle/undo).
|
|---|
| 372 | 'shaer:liked': !!(reacties.get(t.id) || {}).liked,
|
|---|
| 373 | 'shaer:boosted': !!(reacties.get(t.id) || {}).boosted,
|
|---|
| 374 | // An external (non-fediverse) embed, thumbnail-only and never an iframe.
|
|---|
| 375 | // Omitted entirely when the gate is closed (see above).
|
|---|
| 376 | // Carries shaer:playerUrl only when the playback gate is open too.
|
|---|
| 377 | 'shaer:embed': embedsAllowed ? AP.timelineEmbed(t.embed_json, { playback: playbackAllowed }) : undefined,
|
|---|
| 378 | },
|
|---|
| 379 | }));
|
|---|
| 380 | // The direct notes addressed to this account: a plain DM, a guardian's wave
|
|---|
| 381 | // (§5), a ward's 🛟 help request (§5.2.1). Those are messages, not posts, so
|
|---|
| 382 | // they are not in the timeline; without them the app's Berichten shows only
|
|---|
| 383 | // what you said yourself. Same shape as a post, so one parser handles both.
|
|---|
| 384 | const me = AP.actorId(base, auth.site.slug);
|
|---|
| 385 | const myHandle = (() => { try { return `@${auth.site.slug}@${new URL(base).host}`; } catch { return `@${auth.site.slug}`; } })();
|
|---|
| 386 | const messages = AP.getDirectMessages(auth.site.slug, 60).map((m) => ({
|
|---|
| 387 | id: `${m.object_uri}#create`,
|
|---|
| 388 | type: 'Create',
|
|---|
| 389 | actor: m.actor_uri,
|
|---|
| 390 | published: AP.isoStamp(m.published || m.created_at),
|
|---|
| 391 | object: {
|
|---|
| 392 | id: m.object_uri,
|
|---|
| 393 | type: 'Note',
|
|---|
| 394 | attributedTo: m.actor_uri,
|
|---|
| 395 | content: AP.stripLeadingMentions(m.content),
|
|---|
| 396 | url: m.note_url || undefined,
|
|---|
| 397 | published: AP.isoStamp(m.published || m.created_at),
|
|---|
| 398 | // Addressed to us and to nobody we know of: the other recipients of a
|
|---|
| 399 | // note to several people are not ours to see, so we serve what we know.
|
|---|
| 400 | to: [me],
|
|---|
| 401 | // The Mention is how the client recognises itself as the addressee and
|
|---|
| 402 | // groups the note into a conversation. No FEP-e232 link tags here: a
|
|---|
| 403 | // mention row keeps the resolved quote, not the raw tags.
|
|---|
| 404 | tag: [{ type: 'Mention', href: me, name: myHandle }, ...(AP.timelineEmojis(m.emoji_json) || [])],
|
|---|
| 405 | attachment: AP.timelineAttachments(m.media_json),
|
|---|
| 406 | // FEP-633c: what kind of message this is. The wave is a gentle nudge from
|
|---|
| 407 | // a guardian; the help request is the buoy. Both render differently.
|
|---|
| 408 | 'shaer:wave': m.wave ? true : undefined,
|
|---|
| 409 | 'shaer:helpRequest': m.help_request ? true : undefined,
|
|---|
| 410 | 'shaer:quote': AP.timelineQuote(m.quote_json),
|
|---|
| 411 | 'shaer:author': (m.actor_name || m.actor_handle || m.actor_icon) ? {
|
|---|
| 412 | name: m.actor_name || undefined, handle: m.actor_handle || undefined,
|
|---|
| 413 | icon: m.actor_icon || undefined, url: m.actor_url || undefined,
|
|---|
| 414 | emojis: (() => { try { return m.actor_emoji_json ? JSON.parse(m.actor_emoji_json) : undefined; } catch { return undefined; } })(),
|
|---|
| 415 | } : undefined,
|
|---|
| 416 | 'shaer:embed': embedsAllowed ? AP.timelineEmbed(m.embed_json, { playback: playbackAllowed }) : undefined,
|
|---|
| 417 | },
|
|---|
| 418 | }));
|
|---|
| 419 | // Inbound REPLIES on your own posts: stored as interactions (the web's
|
|---|
| 420 | // comment machinery), never as mentions, so this read missed them and a
|
|---|
| 421 | // friend's reply arrived everywhere except in your app (Robins melding,
|
|---|
| 422 | // 30-7). Same shape as the other legs; media/quotes ride the stored JSON.
|
|---|
| 423 | const replies = AP.getReplyMessages(auth.site.slug, 60).map((m) => ({
|
|---|
| 424 | id: `${m.object_uri}#create`,
|
|---|
| 425 | type: 'Create',
|
|---|
| 426 | actor: m.actor_uri,
|
|---|
| 427 | published: AP.isoStamp(m.published || m.created_at),
|
|---|
| 428 | object: {
|
|---|
| 429 | id: m.object_uri,
|
|---|
| 430 | type: 'Note',
|
|---|
| 431 | attributedTo: m.actor_uri,
|
|---|
| 432 | content: AP.stripLeadingMentions(m.content),
|
|---|
| 433 | inReplyTo: m.parent_uri || `${base}/ap/notes/${m.post_id}`,
|
|---|
| 434 | published: AP.isoStamp(m.published || m.created_at),
|
|---|
| 435 | to: [me],
|
|---|
| 436 | tag: [{ type: 'Mention', href: me, name: myHandle }, ...(AP.timelineEmojis(m.emoji_json) || [])],
|
|---|
| 437 | attachment: AP.timelineAttachments(m.media_json),
|
|---|
| 438 | 'shaer:quote': AP.timelineQuote(m.quote_json),
|
|---|
| 439 | 'shaer:author': (m.actor_name || m.actor_handle || m.actor_icon) ? {
|
|---|
| 440 | name: m.actor_name || undefined, handle: m.actor_handle || undefined,
|
|---|
| 441 | icon: m.actor_icon || undefined, url: m.actor_url || undefined,
|
|---|
| 442 | emojis: (() => { try { return m.actor_emoji_json ? JSON.parse(m.actor_emoji_json) : undefined; } catch { return undefined; } })(),
|
|---|
| 443 | } : undefined,
|
|---|
| 444 | 'shaer:embed': embedsAllowed ? AP.timelineEmbed(m.embed_json, { playback: playbackAllowed }) : undefined,
|
|---|
| 445 | },
|
|---|
| 446 | }));
|
|---|
| 447 | // Your OWN sent notes (replies and direct messages, ap_outbox): without
|
|---|
| 448 | // them a reply existed everywhere except in your own app, Messages showed
|
|---|
| 449 | // half a conversation, and a retry ran into the duplicate guard (Robins
|
|---|
| 450 | // melding, 30-7). Served like the other legs: same shape, one parser.
|
|---|
| 451 | const mine = AP.selfAuthor(base, auth.site);
|
|---|
| 452 | const sent = AP.getSentNotes(base, auth.site, 60).map((n) => ({
|
|---|
| 453 | id: `${n.id}#create`,
|
|---|
| 454 | type: 'Create',
|
|---|
| 455 | actor: me,
|
|---|
| 456 | published: n.published,
|
|---|
| 457 | // The leading mention anchor is addressing, not prose (the DM leg strips
|
|---|
| 458 | // it the same way); the Mention tags built from the full content stay.
|
|---|
| 459 | object: { ...n, content: AP.stripLeadingMentions(n.content), 'shaer:author': mine },
|
|---|
| 460 | }));
|
|---|
| 461 | // Newest first over all legs, so the app can keep treating this as one feed.
|
|---|
| 462 | const items = [...posts, ...messages, ...replies, ...sent].sort((a, b) => String(b.published || '').localeCompare(String(a.published || '')));
|
|---|
| 463 | AP.sendAP(res, {
|
|---|
| 464 | '@context': AP.AP_CONTEXT,
|
|---|
| 465 | id: `${base}/ap/users/${auth.site.slug}/inbox`,
|
|---|
| 466 | type: 'OrderedCollection',
|
|---|
| 467 | // What this account may do with what is in here (FEP-633c 5.6). Owner-only
|
|---|
| 468 | // by construction, and never on the public actor document: it says
|
|---|
| 469 | // something about a child, and only the child and its guardians need it.
|
|---|
| 470 | 'shaer:capabilities': {
|
|---|
| 471 | 'shaer:externalEmbeds': embedsAllowed,
|
|---|
| 472 | 'shaer:externalPlayback': playbackAllowed,
|
|---|
| 473 | // Leaving the app is the same decision as playing inside it: with the
|
|---|
| 474 | // gate shut a link is shown but not followed, so the door is closed too
|
|---|
| 475 | // and not just the picture over it.
|
|---|
| 476 | 'shaer:externalLinks': playbackAllowed,
|
|---|
| 477 | },
|
|---|
| 478 | // Het merk van wat hierin zit. Geef hem terug als `since` om op het
|
|---|
| 479 | // volgende te wachten. NA het samenstellen bepaald, zodat hij precies dekt
|
|---|
| 480 | // wat je in handen hebt en niet iets dat er ondertussen bij kwam.
|
|---|
| 481 | 'shaer:cursor': AP.feedCursor(auth.site.slug),
|
|---|
| 482 | totalItems: items.length,
|
|---|
| 483 | orderedItems: items,
|
|---|
| 484 | });
|
|---|
| 485 | return undefined;
|
|---|
| 486 | });
|
|---|
| 487 |
|
|---|
| 488 | // ── uploadMedia (owner only, AP C2S) ──────────────────────────────
|
|---|
| 489 | // The actor advertises endpoints.uploadMedia; this implements it. A bearer
|
|---|
| 490 | // scoped to this site uploads one image/audio/video (multipart field "file",
|
|---|
| 491 | // AP convention) into the same store the reply editor uses, and gets back
|
|---|
| 492 | // { url, mediaType, name } to attach on a note (e.g. the help-buoy capture).
|
|---|
| 493 | const AP_MEDIA_DIR = mediaDir('REPLY_MEDIA_PATH', 'reply-media');
|
|---|
| 494 | fs.mkdirSync(AP_MEDIA_DIR, { recursive: true });
|
|---|
| 495 | const AP_MEDIA_EXT = new Set(['.jpg', '.jpeg', '.png', '.webp', '.gif', '.mp3', '.m4a', '.ogg', '.opus', '.flac', '.wav', '.mp4', '.webm', '.mov']);
|
|---|
| 496 | const apMediaUpload = multer({
|
|---|
| 497 | storage: multer.diskStorage({
|
|---|
| 498 | destination: (req, file, cb) => cb(null, AP_MEDIA_DIR),
|
|---|
| 499 | filename: (req, file, cb) => cb(null, `${randomUUID()}${path.extname(file.originalname || '').toLowerCase()}`),
|
|---|
| 500 | }),
|
|---|
| 501 | limits: { fileSize: 32 * 1024 * 1024 },
|
|---|
| 502 | fileFilter: (req, file, cb) => {
|
|---|
| 503 | const ext = path.extname(file.originalname || '').toLowerCase();
|
|---|
| 504 | if (!AP_MEDIA_EXT.has(ext)) return cb(new Error('Media must be an image, audio or video file'));
|
|---|
| 505 | cb(null, true);
|
|---|
| 506 | },
|
|---|
| 507 | });
|
|---|
| 508 | router.post('/ap/users/:slug/uploadMedia', (req, res) => {
|
|---|
| 509 | const auth = OAuth.verifyBearer(req.headers.authorization);
|
|---|
| 510 | if (!auth || auth.site.slug !== req.params.slug) return res.status(403).end();
|
|---|
| 511 | apMediaUpload.single('file')(req, res, (err) => {
|
|---|
| 512 | if (err) return res.status(400).json({ error: err.message });
|
|---|
| 513 | if (!req.file) return res.status(400).json({ error: 'No file' });
|
|---|
| 514 | const mime = String(req.file.mimetype || '');
|
|---|
| 515 | if (!/^(image|audio|video)\//.test(mime)) {
|
|---|
| 516 | try { fs.unlinkSync(req.file.path); } catch { /* best effort */ }
|
|---|
| 517 | return res.status(400).json({ error: 'Media must be an image, audio or video file' });
|
|---|
| 518 | }
|
|---|
| 519 | // A video gets a poster frame next to it (shaer-zowq), best-effort and
|
|---|
| 520 | // out of band: ffmpeg pulls one frame at 1s into <name>.poster.jpg. On a
|
|---|
| 521 | // machine without ffmpeg nothing happens and nothing breaks; the clients
|
|---|
| 522 | // fall back to extracting a frame natively.
|
|---|
| 523 | if (mime.startsWith('video/')) {
|
|---|
| 524 | // The bundled static build (ffmpeg-static) does the work, exactly like
|
|---|
| 525 | // VideoCoverService and AudioTranscoder already do: Klonkt SHIPS its
|
|---|
| 526 | // ffmpeg (Robins opmerking, 30-7), so nothing needs installing on any
|
|---|
| 527 | // machine. Soft dependency + best-effort: absent stays silent, and
|
|---|
| 528 | // FFMPEG_PATH can still override for an operator who wants a newer one.
|
|---|
| 529 | Promise.all([import('child_process'), import('ffmpeg-static')]).then(([{ execFile }, ff]) => {
|
|---|
| 530 | const bin = process.env.FFMPEG_PATH || ff.default;
|
|---|
| 531 | if (!bin) return;
|
|---|
| 532 | const poster = req.file.path + '.poster.jpg';
|
|---|
| 533 | execFile(bin, ['-hide_banner', '-loglevel', 'error', '-y', '-ss', '1', '-i', req.file.path, '-frames:v', '1', '-vf', "scale='min(640,iw)':-2", poster],
|
|---|
| 534 | { timeout: 30000 }, (e) => { if (e && e.code !== 'ENOENT') console.warn('[media] poster failed:', e.message); });
|
|---|
| 535 | }).catch(() => { /* never blocks the upload */ });
|
|---|
| 536 | }
|
|---|
| 537 | // Audio gets the same courtesy (Robins vraag, 30-7: vrolijk de kale
|
|---|
| 538 | // audio-tegel op): ffmpeg draws the waveform into <name>.poster.png.
|
|---|
| 539 | // White on transparent, so the tile's own gradient stays the backdrop
|
|---|
| 540 | // and every audio post keeps its own hue. The shape is bars, not the
|
|---|
| 541 | // raw hairy wave (Robins tweede vraag): peak and average sampled into
|
|---|
| 542 | // 57 columns (soft tip over bright core), blown up nearest-neighbor to
|
|---|
| 543 | // 14px bars, and drawgrid ERASES 5px gaps (c=black@0 + replace=1 writes
|
|---|
| 544 | // transparent pixels; h=2*ih keeps horizontal grid lines out of frame).
|
|---|
| 545 | if (mime.startsWith('audio/')) {
|
|---|
| 546 | Promise.all([import('child_process'), import('ffmpeg-static')]).then(([{ execFile }, ff]) => {
|
|---|
| 547 | const bin = process.env.FFMPEG_PATH || ff.default;
|
|---|
| 548 | if (!bin) return;
|
|---|
| 549 | const poster = req.file.path + '.poster.png';
|
|---|
| 550 | const graph = '[0:a]aformat=channel_layouts=mono,asplit[a][b];'
|
|---|
| 551 | + '[a]showwavespic=s=57x256:colors=white@0.5:filter=peak:scale=sqrt:draw=full[pk];'
|
|---|
| 552 | + '[b]showwavespic=s=57x256:colors=white:filter=average:scale=sqrt:draw=full[av];'
|
|---|
| 553 | + '[pk][av]overlay=format=auto,scale=798:256:flags=neighbor,drawgrid=w=14:h=2*ih:t=5:c=black@0:replace=1';
|
|---|
| 554 | execFile(bin, ['-hide_banner', '-loglevel', 'error', '-y', '-i', req.file.path, '-filter_complex', graph, '-frames:v', '1', poster],
|
|---|
| 555 | { timeout: 30000 }, (e) => { if (e && e.code !== 'ENOENT') console.warn('[media] waveform failed:', e.message); });
|
|---|
| 556 | }).catch(() => { /* never blocks the upload */ });
|
|---|
| 557 | }
|
|---|
| 558 | res.status(201).json({
|
|---|
| 559 | url: '/media/reply-media/' + req.file.filename,
|
|---|
| 560 | mediaType: mime,
|
|---|
| 561 | name: String(req.file.originalname || '').slice(0, 120),
|
|---|
| 562 | });
|
|---|
| 563 | });
|
|---|
| 564 | });
|
|---|
| 565 |
|
|---|
| 566 | // ── Followers (count-only public, full for the owner) ─────────────
|
|---|
| 567 | // A C2S bearer scoped to this site (the account owner) gets the real actor
|
|---|
| 568 | // URIs so their own client can build a friends list; everyone else gets the
|
|---|
| 569 | // count only (privacy).
|
|---|
| 570 | // FEP-9876: enrichment is opt-in via `Prefer: return=representation` (RFC 7240).
|
|---|
| 571 | // Returns true and sets the response headers when the owner asked for it.
|
|---|
| 572 | function wantsEnriched(req, res) {
|
|---|
| 573 | res.set('Vary', 'Prefer'); // enriched and bare are two representations
|
|---|
| 574 | if (AP.prefersEnriched(req.get('Prefer'))) {
|
|---|
| 575 | res.set('Preference-Applied', 'return=representation');
|
|---|
| 576 | return true;
|
|---|
| 577 | }
|
|---|
| 578 | return false;
|
|---|
| 579 | }
|
|---|
| 580 |
|
|---|
| 581 | router.get('/ap/users/:slug/followers', (req, res) => {
|
|---|
| 582 | const auth = OAuth.verifyBearer(req.headers.authorization);
|
|---|
| 583 | const owner = auth && auth.site.slug === req.params.slug;
|
|---|
| 584 | const site = owner ? auth.site : publicSite(req.params.slug);
|
|---|
| 585 | if (!site) return res.status(404).end();
|
|---|
| 586 | if (owner) {
|
|---|
| 587 | const uris = db.prepare('SELECT actor_uri FROM ap_followers WHERE slug = ? ORDER BY created_at').all(site.slug).map((r) => r.actor_uri);
|
|---|
| 588 | // Default = bare references; enrich only when the client asks (FEP-9876).
|
|---|
| 589 | const items = wantsEnriched(req, res) ? uris.map((u) => AP.buildActorRef(site.slug, u)) : uris;
|
|---|
| 590 | return AP.sendAP(res, AP.buildFollowers(baseUrl(req), site, items.length, items));
|
|---|
| 591 | }
|
|---|
| 592 | const n = db.prepare('SELECT COUNT(*) n FROM ap_followers WHERE slug = ?').get(site.slug).n;
|
|---|
| 593 | AP.sendAP(res, AP.buildFollowers(baseUrl(req), site, n));
|
|---|
| 594 | });
|
|---|
| 595 |
|
|---|
| 596 | // ── Following (count-only public, full for the owner) ─────────────
|
|---|
| 597 | router.get('/ap/users/:slug/following', (req, res) => {
|
|---|
| 598 | const auth = OAuth.verifyBearer(req.headers.authorization);
|
|---|
| 599 | const owner = auth && auth.site.slug === req.params.slug;
|
|---|
| 600 | const site = owner ? auth.site : publicSite(req.params.slug);
|
|---|
| 601 | if (!site) return res.status(404).end();
|
|---|
| 602 | if (owner) {
|
|---|
| 603 | const enrich = wantsEnriched(req, res); // FEP-9876 opt-in
|
|---|
| 604 | let items = [];
|
|---|
| 605 | try {
|
|---|
| 606 | const uris = db.prepare("SELECT actor_uri FROM ap_following WHERE slug = ? AND status = 'accepted' ORDER BY created_at").all(site.slug).map((r) => r.actor_uri);
|
|---|
| 607 | items = enrich ? uris.map((u) => AP.buildActorRef(site.slug, u)) : uris;
|
|---|
| 608 | } catch { /* table may not exist */ }
|
|---|
| 609 | return AP.sendAP(res, AP.buildFollowing(baseUrl(req), site, items.length, items));
|
|---|
| 610 | }
|
|---|
| 611 | let n = 0;
|
|---|
| 612 | try { n = db.prepare("SELECT COUNT(*) n FROM ap_following WHERE slug = ? AND status = 'accepted'").get(site.slug).n; } catch { /* table may not exist */ }
|
|---|
| 613 | AP.sendAP(res, AP.buildFollowing(baseUrl(req), site, n));
|
|---|
| 614 | });
|
|---|
| 615 |
|
|---|
| 616 | // ── Featured (pinned posts → Mastodon "Featured" tab) ─────────────
|
|---|
| 617 | router.get('/ap/users/:slug/featured', (req, res) => {
|
|---|
| 618 | const site = publicSite(req.params.slug);
|
|---|
| 619 | if (!site) return res.status(404).end();
|
|---|
| 620 | // NB: Mastodon DISPLAYS the featured collection in REVERSE (pins shown
|
|---|
| 621 | // last-processed-first). So we emit it reversed (lowest pin priority first,
|
|---|
| 622 | // rank 1 last) → Mastodon flips it back to pin-rank ascending on the profile.
|
|---|
| 623 | const posts = db.prepare(
|
|---|
| 624 | `SELECT id, slug, title, content, cover_image_url, cover_video_url, nsfw, content_warning, c2s_attachments, published_at, created_at
|
|---|
| 625 | FROM posts WHERE site_id = ? AND status = 'published' AND (fan_only IS NULL OR fan_only = 0)
|
|---|
| 626 | AND pinned IS NOT NULL AND pinned > 0
|
|---|
| 627 | ORDER BY pinned DESC, COALESCE(published_at, created_at) ASC LIMIT 20`
|
|---|
| 628 | ).all(site.id);
|
|---|
| 629 | AP.sendAP(res, AP.buildFeatured(baseUrl(req), site, posts));
|
|---|
| 630 | });
|
|---|
| 631 |
|
|---|
| 632 | // ── Playlist als dereferenceerbare AP-collectie (shaer-ayc) ───────
|
|---|
| 633 | // De eerste stap van het Funkwhale-spoor: een playlist heeft een id, dus een
|
|---|
| 634 | // stabiele URI. Alleen het fedi_open-deel staat erin (de poort is per bestand
|
|---|
| 635 | // en eenrichtings; zie setAudioFediOpen in routes/posts.js) — een collectie
|
|---|
| 636 | // zonder open tracks bestaat wel maar is leeg, want de playlist zelf is niet
|
|---|
| 637 | // geheim, alleen de bestanden erachter.
|
|---|
| 638 | // De lijst van alle playlist-collecties (shaer-ayc, stap 2). De actor wijst
|
|---|
| 639 | // hierheen via AS2 `streams`. Kaal standaard; verrijkte stubs op verzoek
|
|---|
| 640 | // (FEP-9876), dezelfde conventie als followers/following.
|
|---|
| 641 | router.get('/ap/users/:slug/playlists', (req, res) => {
|
|---|
| 642 | const site = publicSite(req.params.slug);
|
|---|
| 643 | if (!site) return res.status(404).end();
|
|---|
| 644 | AP.sendAP(res, AP.listPlaylistsAP(baseUrl(req), site, wantsEnriched(req, res)));
|
|---|
| 645 | });
|
|---|
| 646 |
|
|---|
| 647 | router.get('/ap/users/:slug/playlists/:id', (req, res) => {
|
|---|
| 648 | const site = publicSite(req.params.slug);
|
|---|
| 649 | if (!site) return res.status(404).end();
|
|---|
| 650 | const pl = db.prepare('SELECT id, title, artist, year, cover_url, kind FROM playlists WHERE id = ? AND site_id = ?')
|
|---|
| 651 | .get(req.params.id, site.id);
|
|---|
| 652 | if (!pl) return res.status(404).end();
|
|---|
| 653 | AP.sendAP(res, AP.buildPlaylistCollection(baseUrl(req), site, pl, AP.playlistOpenTracks(pl.id)));
|
|---|
| 654 | });
|
|---|
| 655 |
|
|---|
| 656 | // ── Note ──────────────────────────────────────────────────────────
|
|---|
| 657 | router.get('/ap/notes/:id', async (req, res) => {
|
|---|
| 658 | // No fan_only filter in the SELECT anymore: a friends-only post is not
|
|---|
| 659 | // absent, it is GATED. The old route hid it from EVERYONE, also from the
|
|---|
| 660 | // follower whose friendship earns it — so the signed resolution the reply
|
|---|
| 661 | // path performs knocked on a door that could never open, and every reply
|
|---|
| 662 | // to a friends-only post (Shaer's default!) died in
|
|---|
| 663 | // cannot_resolve_inReplyTo. Strangers still get the exact same 404, so a
|
|---|
| 664 | // note's existence stays as private as before.
|
|---|
| 665 | const post = db.prepare(
|
|---|
| 666 | "SELECT * FROM posts WHERE id = ? AND status = 'published'"
|
|---|
| 667 | ).get(req.params.id);
|
|---|
| 668 | if (post && AP.noteAudience(post) !== 'public') {
|
|---|
| 669 | // The whole gate in a try: this is the only async route in this file,
|
|---|
| 670 | // and Express 4 does not catch an async rejection — the request would
|
|---|
| 671 | // hang forever instead of failing (which is exactly how the missing
|
|---|
| 672 | // default-export entry manifested while building this). Any error here
|
|---|
| 673 | // reads as "not authorized", never as silence.
|
|---|
| 674 | try {
|
|---|
| 675 | if (AP.noteAudience(post) === 'direct') return res.status(404).end();
|
|---|
| 676 | const gsite = db.prepare('SELECT * FROM sites WHERE id = ?').get(post.site_id);
|
|---|
| 677 | const actor = await AP.verifyRequest(req).catch(() => null);
|
|---|
| 678 | if (!actor || !AP.mayReadNote(gsite, post, actor.id)) return res.status(404).end();
|
|---|
| 679 | } catch { return res.status(404).end(); }
|
|---|
| 680 | }
|
|---|
| 681 | if (!post) {
|
|---|
| 682 | // Could be one of OUR outbound replies (ap_outbox), not a post.
|
|---|
| 683 | const note = AP.getOutboxNote(baseUrl(req), req.params.id);
|
|---|
| 684 | if (!note) return res.status(404).end();
|
|---|
| 685 | if (!AP.apWants(req)) {
|
|---|
| 686 | // A browser hit a reply's AP URL → send them to the source it replies to
|
|---|
| 687 | // (where the post + its reactions live), falling back to the site home.
|
|---|
| 688 | const src = (typeof note.inReplyTo === 'string' && /^https?:\/\//i.test(note.inReplyTo))
|
|---|
| 689 | ? note.inReplyTo : (baseUrl(req) + '/');
|
|---|
| 690 | return res.redirect(302, src);
|
|---|
| 691 | }
|
|---|
| 692 | return AP.sendAP(res, { '@context': AP.AP_CONTEXT, ...note });
|
|---|
| 693 | }
|
|---|
| 694 | const site = db.prepare('SELECT * FROM sites WHERE id = ?').get(post.site_id);
|
|---|
| 695 | if (!site) return res.status(404).end();
|
|---|
| 696 | const note = AP.buildNote(baseUrl(req), site, post);
|
|---|
| 697 | if (!AP.apWants(req)) {
|
|---|
| 698 | // A browser hit a post's AP note URL → send them to the human post page
|
|---|
| 699 | // (which shows the post + its "from the fediverse" reactions).
|
|---|
| 700 | return res.redirect(302, note.url || (baseUrl(req) + '/'));
|
|---|
| 701 | }
|
|---|
| 702 | AP.sendAP(res, { '@context': AP.AP_CONTEXT, ...note });
|
|---|
| 703 | });
|
|---|
| 704 |
|
|---|
| 705 | // ── Replies collection ── lets remote servers fetch a post's whole thread.
|
|---|
| 706 | router.get('/ap/notes/:id/replies', (req, res) => {
|
|---|
| 707 | const base = baseUrl(req);
|
|---|
| 708 | const items = AP.getReplyUris(base, req.params.id);
|
|---|
| 709 | AP.sendAP(res, {
|
|---|
| 710 | '@context': AP.AP_CONTEXT,
|
|---|
| 711 | id: `${base}/ap/notes/${req.params.id}/replies`,
|
|---|
| 712 | type: 'OrderedCollection',
|
|---|
| 713 | totalItems: items.length,
|
|---|
| 714 | orderedItems: items,
|
|---|
| 715 | });
|
|---|
| 716 | });
|
|---|
| 717 |
|
|---|
| 718 | // ── NodeInfo ── standard instance metadata so fediverse tools recognise Klonkt.
|
|---|
| 719 | router.get('/.well-known/nodeinfo', (req, res) => {
|
|---|
| 720 | res.type('application/json');
|
|---|
| 721 | res.set('Cache-Control', 'public, max-age=3600');
|
|---|
| 722 | res.send(JSON.stringify({ links: [{ rel: 'http://nodeinfo.diaspora.software/ns/schema/2.1', href: `${baseUrl(req)}/nodeinfo/2.1` }] }));
|
|---|
| 723 | });
|
|---|
| 724 | router.get('/nodeinfo/2.1', (req, res) => {
|
|---|
| 725 | let users = 0; let posts = 0;
|
|---|
| 726 | // "users" = public AP actors (sites), not the admin/member account rows.
|
|---|
| 727 | try { users = db.prepare('SELECT COUNT(*) c FROM sites WHERE (is_public IS NULL OR is_public = 1)').get().c; } catch { /* */ }
|
|---|
| 728 | try { posts = db.prepare("SELECT COUNT(*) c FROM posts WHERE status = 'published'").get().c; } catch { /* */ }
|
|---|
| 729 | res.type('application/json; charset=utf-8');
|
|---|
| 730 | res.set('Cache-Control', 'public, max-age=600');
|
|---|
| 731 | res.send(JSON.stringify({
|
|---|
| 732 | version: '2.1',
|
|---|
| 733 | software: { name: 'klonkt', version: _ver, repository: 'https://github.com/roboburr/klonkt' },
|
|---|
| 734 | protocols: ['activitypub'],
|
|---|
| 735 | services: { inbound: [], outbound: [] },
|
|---|
| 736 | openRegistrations: false,
|
|---|
| 737 | usage: { users: { total: users }, localPosts: posts },
|
|---|
| 738 | metadata: { nodeName: 'Klonkt' },
|
|---|
| 739 | }));
|
|---|
| 740 | });
|
|---|
| 741 |
|
|---|
| 742 | // ── Inbox — Follow→Accept, Undo Follow (best-effort signature verify) ──
|
|---|
| 743 | const apJson = express.json({
|
|---|
| 744 | type: ['application/activity+json', 'application/ld+json', 'application/json'],
|
|---|
| 745 | limit: '1mb',
|
|---|
| 746 | verify: (req, _res, buf) => { req.rawBody = buf; }, // raw body for digest verification
|
|---|
| 747 | });
|
|---|
| 748 | router.post(['/ap/users/:slug/inbox', '/ap/inbox'], apInboxLimiter, apJson, async (req, res) => {
|
|---|
| 749 | try { return res.status(await AP.handleInbox(req, req.params.slug || null) || 202).end(); }
|
|---|
| 750 | catch (e) { console.warn('[AP inbox] error:', e.message); return res.status(202).end(); }
|
|---|
| 751 | });
|
|---|
| 752 |
|
|---|
| 753 | // ── Outbox POST: ActivityPub Client-to-Server ─────────────────────
|
|---|
| 754 | // A bearer-authenticated client (Shaer) POSTs an activity; we translate it onto
|
|---|
| 755 | // the normal delivery machinery. The token is scoped to one user+site (OAuth
|
|---|
| 756 | // consent), so it must match the slug in the URL. (Declared after apJson, which
|
|---|
| 757 | // this shares with the inbox handler.)
|
|---|
| 758 | router.post('/ap/users/:slug/outbox', apInboxLimiter, apJson, async (req, res) => {
|
|---|
| 759 | const auth = OAuth.verifyBearer(req.headers.authorization);
|
|---|
| 760 | if (!auth) { res.set('WWW-Authenticate', 'Bearer'); return res.status(401).json({ error: 'invalid_token' }); }
|
|---|
| 761 | if (auth.site.slug !== req.params.slug) return res.status(403).json({ error: 'wrong_site', detail: 'token is scoped to a different site' });
|
|---|
| 762 | if (auth.user.readonly) return res.status(403).json({ error: 'read_only_account' });
|
|---|
| 763 |
|
|---|
| 764 | const out = await AP.ingestOutboxActivity(auth.site, auth.user, req.body);
|
|---|
| 765 | if (out.error) return res.status(out.status || 400).json({ error: out.error, detail: out.detail });
|
|---|
| 766 | // 201 Created → Location header (AP spec); 202 Accepted for side-effect verbs.
|
|---|
| 767 | if (out.status === 201 && out.url) res.set('Location', out.url);
|
|---|
| 768 | // `state` carries a third outcome the app must be able to tell apart from a
|
|---|
| 769 | // plain success: a ward's follow held for its guardians (§5.3, shaer-p729).
|
|---|
| 770 | return res.status(out.status || 202).json({ ok: true, id: out.id, url: out.url, ...(out.state ? { state: out.state } : {}) });
|
|---|
| 771 | });
|
|---|
| 772 |
|
|---|
| 773 | export default router;
|
|---|