source: Klonkt/src/services/ActivityPubService.js@ c16e0a5

main
Last change on this file since c16e0a5 was c16e0a5, checked in by Robin Genis <roboburr@…>, 3 months ago

feat(activitypub): inbound interactions — replies, likes, boosts (Phase 2)

Inbox now stores incoming Create(reply to our note), Like and Announce (boost),
plus Undo(Like/Announce) and Delete(reply). New ap_interactions table; the post
page shows a 'From the fediverse' section (counts + replies with avatar/handle,
sanitized). Actor name/icon resolved best-effort; reply HTML sanitized.

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

  • Property mode set to 100644
File size: 18.7 KB
RevLine 
[6bd25d1]1/**
2 * ActivityPubService — Klonkt as a real ActivityPub actor (fediverse bridge).
3 *
4 * Phase 1 (this file): the PUBLISH/discoverable side.
5 * - per-site RSA keypair (Mastodon-compatible HTTP Signatures; separate from
6 * the Ed25519 keys used by the lighter Cirkels v1)
7 * - builders for the Actor document, Note objects and the Outbox collection
8 * - apWants(): HTTP content-negotiation helper (activity+json vs HTML)
9 *
10 * The interactive side (inbox: Follow/Accept, signature verify, delivery to
11 * followers) lands in the next step and is tested live against Mastodon.
12 *
13 * AP actor URLs live under /ap/* so they never clash with the human pages:
14 * actor = <base>/ap/users/<slug>
15 * inbox = <actor>/inbox outbox = <actor>/outbox
16 * note = <base>/ap/notes/<postId>
17 */
18import crypto from 'crypto';
19import db from '../config/database.js';
[c16e0a5]20import HtmlSanitizerService from './HtmlSanitizerService.js';
[6bd25d1]21
22const PUBLIC = 'https://www.w3.org/ns/activitystreams#Public';
23const MAX_OUTBOX = 20;
24
25// ── RSA keys per actor (lazy, cached in DB) ───────────────────────
26// Prepared lazily (NOT at module load) — the ap_keys table is created in
27// initializeDatabase(), which runs after this module is imported.
28let _sel, _ins;
29function keyStmts() {
30 if (!_sel) {
31 _sel = db.prepare('SELECT public_pem, private_pem FROM ap_keys WHERE slug = ?');
32 _ins = db.prepare('INSERT OR IGNORE INTO ap_keys (slug, public_pem, private_pem, created_at) VALUES (?,?,?,CURRENT_TIMESTAMP)');
33 }
34 return { sel: _sel, ins: _ins };
35}
36
37export function getOrCreateKeys(slug) {
38 const { sel, ins } = keyStmts();
39 const row = sel.get(slug);
40 if (row) return row;
41 const { publicKey, privateKey } = crypto.generateKeyPairSync('rsa', {
42 modulusLength: 2048,
43 publicKeyEncoding: { type: 'spki', format: 'pem' },
44 privateKeyEncoding: { type: 'pkcs8', format: 'pem' },
45 });
46 ins.run(slug, publicKey, privateKey);
47 return sel.get(slug) || { public_pem: publicKey, private_pem: privateKey };
48}
49
50// ── content negotiation ───────────────────────────────────────────
51// True when the caller wants ActivityPub JSON rather than the HTML page.
52export function apWants(req) {
53 const a = String(req.headers.accept || '').toLowerCase();
54 return a.includes('application/activity+json') ||
55 (a.includes('application/ld+json') && a.includes('activitystreams'));
56}
57
58const AP_CONTENT_TYPE = 'application/activity+json; charset=utf-8';
59export function sendAP(res, obj) {
60 res.type(AP_CONTENT_TYPE);
61 res.set('Cache-Control', 'public, max-age=120');
62 res.send(JSON.stringify(obj));
63}
64
65// ── document builders ─────────────────────────────────────────────
66export function actorId(base, slug) { return `${base}/ap/users/${encodeURIComponent(slug)}`; }
67export function noteId(base, postId) { return `${base}/ap/notes/${encodeURIComponent(postId)}`; }
68
69export function buildActor(base, site) {
70 const id = actorId(base, site.slug);
71 const keys = getOrCreateKeys(site.slug);
72 const actor = {
73 '@context': ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'],
74 id,
75 type: 'Person',
76 preferredUsername: site.slug,
77 name: site.title || site.slug,
78 summary: site.tagline || site.description || '',
79 url: `${base}/${site.slug === site.primary_slug ? '' : 'user/' + encodeURIComponent(site.slug)}`,
80 manuallyApprovesFollowers: false,
81 discoverable: true,
82 inbox: `${id}/inbox`,
83 outbox: `${id}/outbox`,
84 followers: `${id}/followers`,
85 endpoints: { sharedInbox: `${base}/ap/inbox` },
86 publicKey: {
87 id: `${id}#main-key`,
88 owner: id,
89 publicKeyPem: keys.public_pem,
90 },
91 };
92 if (site.profile_photo) {
93 const u = /^https?:/.test(site.profile_photo) ? site.profile_photo : `${base}${site.profile_photo.startsWith('/') ? '' : '/'}${site.profile_photo}`;
94 actor.icon = { type: 'Image', url: u };
95 }
96 return actor;
97}
98
99// A single post as an AS2 Note (the object), and as a Create activity (for outbox/delivery).
100export function buildNote(base, site, post) {
101 const id = noteId(base, post.id);
102 const aId = actorId(base, site.slug);
103 const human = `${base}/${encodeURIComponent(post.slug)}`;
[065452a]104 // Mastodon ignores a Note's `name`, so put the title INTO the content (bold
105 // first line) — the standard blog→fediverse convention. post.content is
106 // already sanitized HTML; the title is plain text, so escape it.
107 const escTitle = String(post.title || '').replace(/[<>&]/g, (c) => ({ '<': '&lt;', '>': '&gt;', '&': '&amp;' }[c]));
108 const titleHtml = post.title ? `<p><strong>${escTitle}</strong></p>` : '';
[5a93ac0]109
110 // Images travel as AP `attachment` (Mastodon strips <img> from content). Collect
111 // the cover + any inline <img>, make absolute, then strip <img> from the content
112 // to avoid duplicate rendering on clients that DO keep them.
113 const abs = (u) => !u ? null : (/^https?:/i.test(u) ? u : `${base}${u.startsWith('/') ? '' : '/'}${u}`);
114 const mediaType = (u) => {
115 const e = ((u || '').split('?')[0].match(/\.(\w+)$/) || [])[1];
116 return ({ jpg: 'image/jpeg', jpeg: 'image/jpeg', png: 'image/png', gif: 'image/gif', webp: 'image/webp', avif: 'image/avif' })[(e || '').toLowerCase()] || 'image/jpeg';
117 };
118 const urls = [];
119 if (post.cover_image_url) urls.push(abs(post.cover_image_url));
120 let body = post.content || '';
121 for (const m of body.matchAll(/<img\b[^>]*\bsrc="([^"]+)"[^>]*>/gi)) urls.push(abs(m[1]));
122 body = body.replace(/<img\b[^>]*>/gi, '');
123 const seen = new Set();
124 const attachment = urls.filter(Boolean)
125 .filter((u) => { if (seen.has(u)) return false; seen.add(u); return true; })
126 .map((u) => ({ type: 'Document', mediaType: mediaType(u), url: u }));
127
128 const note = {
[6bd25d1]129 id,
130 type: 'Note',
131 attributedTo: aId,
[5a93ac0]132 content: titleHtml + body,
[6bd25d1]133 url: human,
134 published: new Date(post.published_at || post.created_at || Date.now()).toISOString(),
135 to: [PUBLIC],
136 cc: [`${aId}/followers`],
137 tag: Array.isArray(post.tags) ? post.tags.map((t) => ({ type: 'Hashtag', name: '#' + String(t).replace(/\s+/g, '') })) : [],
138 };
[5a93ac0]139 if (attachment.length) note.attachment = attachment;
140 return note;
[6bd25d1]141}
142
143export function buildCreate(base, site, post) {
144 const note = buildNote(base, site, post);
145 return {
146 '@context': 'https://www.w3.org/ns/activitystreams',
147 id: note.id + '#create',
148 type: 'Create',
149 actor: actorId(base, site.slug),
150 published: note.published,
151 to: note.to,
152 cc: note.cc,
153 object: note,
154 };
155}
156
157export function buildOutbox(base, site, posts) {
158 const id = `${actorId(base, site.slug)}/outbox`;
159 const items = (posts || []).slice(0, MAX_OUTBOX).map((p) => buildCreate(base, site, p));
160 return {
161 '@context': 'https://www.w3.org/ns/activitystreams',
162 id,
163 type: 'OrderedCollection',
164 totalItems: items.length,
165 orderedItems: items,
166 };
167}
168
169export function buildFollowers(base, site, count) {
170 const id = `${actorId(base, site.slug)}/followers`;
171 return {
172 '@context': 'https://www.w3.org/ns/activitystreams',
173 id,
174 type: 'OrderedCollection',
175 totalItems: count || 0,
176 orderedItems: [], // hidden for privacy; count only
177 };
178}
179
[5bf63b7]180// ── followers store (lazy stmts) ──────────────────────────────────
181let _insF, _delF, _listF, _cntF;
182function fStmts() {
183 if (!_insF) {
184 _insF = db.prepare('INSERT OR IGNORE INTO ap_followers (slug, actor_uri, inbox, shared_inbox, created_at) VALUES (?,?,?,?,CURRENT_TIMESTAMP)');
185 _delF = db.prepare('DELETE FROM ap_followers WHERE slug = ? AND actor_uri = ?');
186 _listF = db.prepare('SELECT inbox, shared_inbox FROM ap_followers WHERE slug = ?');
187 _cntF = db.prepare('SELECT COUNT(*) n FROM ap_followers WHERE slug = ?');
188 }
189 return { ins: _insF, del: _delF, list: _listF, cnt: _cntF };
190}
191export function followerCount(slug) { return fStmts().cnt.get(slug).n; }
192
[c16e0a5]193// ── inbound interactions store (replies / likes / boosts), lazy stmts ──
194let _insI, _delLA, _delReply, _listI;
195function iStmts() {
196 if (!_insI) {
197 _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, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)');
198 _delLA = db.prepare('DELETE FROM ap_interactions WHERE kind = ? AND post_id = ? AND actor_uri = ?');
199 _delReply = db.prepare("DELETE FROM ap_interactions WHERE kind = 'reply' AND object_uri = ?");
200 _listI = db.prepare('SELECT kind, actor_uri, actor_name, actor_handle, actor_url, actor_icon, content, published, created_at FROM ap_interactions WHERE post_id = ? ORDER BY created_at ASC');
201 }
202 return { ins: _insI, delLA: _delLA, delReply: _delReply, list: _listI };
203}
204
205const localPostExists = (id) => { try { return !!db.prepare('SELECT 1 FROM posts WHERE id = ?').get(id); } catch { return false; } };
206// Extract our local post id from a note URL, but only if it's ours (base match).
207function postIdFromNoteUrl(url, base) {
208 const s = String(url || '');
209 if (base && !s.startsWith(base)) return null;
210 const m = s.match(/\/ap\/notes\/([^/?#]+)/);
211 return m ? decodeURIComponent(m[1]) : null;
212}
213function deriveHandle(actorUri) {
214 try { const u = new URL(actorUri); const seg = u.pathname.split('/').filter(Boolean).pop() || ''; return `@${seg}@${u.host}`; } catch { return String(actorUri || ''); }
215}
216function actorInfo(doc, actorUri) {
217 let host = ''; try { host = new URL(actorUri).host; } catch { /* keep empty */ }
218 const handle = doc && doc.preferredUsername ? `@${doc.preferredUsername}@${host}` : deriveHandle(actorUri);
219 const icon = doc && doc.icon ? (doc.icon.url || (Array.isArray(doc.icon) && doc.icon[0] && doc.icon[0].url)) : null;
220 return {
221 name: (doc && (doc.name || doc.preferredUsername)) || handle,
222 handle,
223 url: (doc && (doc.url || doc.id)) || actorUri,
224 icon: icon || null,
225 };
226}
227
228// Stored, view-ready summary of a post's inbound fediverse activity.
229export function getInteractions(postId) {
230 const rows = iStmts().list.all(postId);
231 return {
232 replies: rows.filter((r) => r.kind === 'reply'),
233 likeCount: rows.filter((r) => r.kind === 'like').length,
234 announceCount: rows.filter((r) => r.kind === 'announce').length,
235 total: rows.length,
236 };
237}
238
[5bf63b7]239// ── HTTP Signatures + delivery ────────────────────────────────────
240const slugFromActorUrl = (url) => { const m = String(url || '').match(/\/ap\/users\/([^/?#]+)/); return m ? decodeURIComponent(m[1]) : null; };
241
242// Sign + POST an activity to a remote inbox (draft-cavage HTTP Signatures, RSA-SHA256).
243export async function deliver(inboxUrl, bodyObj, keyId, privatePem) {
244 const body = JSON.stringify(bodyObj);
245 const u = new URL(inboxUrl);
246 const date = new Date().toUTCString();
247 const digest = 'SHA-256=' + crypto.createHash('sha256').update(body).digest('base64');
248 const signingString = `(request-target): post ${u.pathname}\nhost: ${u.host}\ndate: ${date}\ndigest: ${digest}`;
249 const signature = crypto.sign('sha256', Buffer.from(signingString), privatePem).toString('base64');
250 const sig = `keyId="${keyId}",algorithm="rsa-sha256",headers="(request-target) host date digest",signature="${signature}"`;
251 const r = await fetch(inboxUrl, {
252 method: 'POST',
253 headers: { 'Content-Type': 'application/activity+json', Accept: 'application/activity+json', Date: date, Digest: digest, Signature: sig },
254 body,
255 signal: AbortSignal.timeout(8000),
256 });
257 return r.status;
258}
259
260export async function fetchActor(url) {
261 try {
262 const r = await fetch(url, { headers: { Accept: 'application/activity+json' }, redirect: 'follow', signal: AbortSignal.timeout(8000) });
263 if (!r.ok) return null;
264 return await r.json();
265 } catch { return null; }
266}
267
268// Best-effort verification of an incoming signed request. Returns the sender's
269// actor doc if the signature checks out, else null. (Not gating yet — MVP.)
270export async function verifyRequest(req) {
271 const sigH = req.headers['signature'];
272 if (!sigH) return null;
273 const p = Object.fromEntries([...sigH.matchAll(/([a-zA-Z]+)="([^"]*)"/g)].map((m) => [m[1], m[2]]));
274 if (!p.keyId || !p.signature) return null;
275 const actor = await fetchActor(p.keyId.split('#')[0]);
276 const pem = actor && actor.publicKey && actor.publicKey.publicKeyPem;
277 if (!pem) return null;
278 const hs = (p.headers || '(request-target) host date').split(/\s+/);
279 const line = hs.map((h) => h === '(request-target)'
280 ? `(request-target): ${req.method.toLowerCase()} ${req.originalUrl}`
281 : `${h}: ${req.headers[h] || ''}`).join('\n');
282 let ok = false;
283 try { ok = crypto.verify('sha256', Buffer.from(line), pem, Buffer.from(p.signature, 'base64')); } catch { ok = false; }
284 if (ok && hs.includes('digest') && req.rawBody) {
285 const exp = 'SHA-256=' + crypto.createHash('sha256').update(req.rawBody).digest('base64');
286 if (req.headers['digest'] !== exp) ok = false;
287 }
288 return ok ? actor : null;
289}
290
291// Handle an incoming inbox POST. slugParam = null for the shared /ap/inbox.
292export async function handleInbox(req, slugParam) {
293 const act = req.body || {};
294 const type = act.type;
295 const base = (process.env.PUBLIC_BASE_URL || `${req.protocol}://${req.get('host')}`).replace(/\/+$/, '');
296 const verified = await verifyRequest(req).catch(() => null); // best-effort; not gating (MVP)
297
298 if (type === 'Follow') {
299 const who = typeof act.actor === 'string' ? act.actor : (act.actor && act.actor.id);
300 const slug = slugParam || slugFromActorUrl(typeof act.object === 'string' ? act.object : (act.object && act.object.id));
301 if (!who || !slug) return 400;
302 const remote = await fetchActor(who);
303 if (!remote || !remote.inbox) return 202; // can't reach them → drop quietly
304 fStmts().ins.run(slug, who, remote.inbox, (remote.endpoints && remote.endpoints.sharedInbox) || null);
305 const me = actorId(base, slug);
306 const keys = getOrCreateKeys(slug);
307 const accept = { '@context': 'https://www.w3.org/ns/activitystreams', id: `${me}#accept-${Date.now()}`, type: 'Accept', actor: me, object: act };
308 deliver(remote.inbox, accept, `${me}#main-key`, keys.private_pem).catch((e) => console.warn('[AP] Accept delivery failed:', e.message));
309 console.log('[AP] Follow', who, '→', slug, verified ? '(sig ok)' : '(sig unverified)');
310 return 202;
311 }
[c16e0a5]312 if (type === 'Undo' && act.object) {
[5bf63b7]313 const who = typeof act.actor === 'string' ? act.actor : (act.actor && act.actor.id);
[c16e0a5]314 const ot = act.object.type;
315 if (ot === 'Follow') {
316 const obj = act.object.object;
317 const slug = slugParam || slugFromActorUrl(typeof obj === 'string' ? obj : (obj && obj.id));
318 if (who && slug) { fStmts().del.run(slug, who); console.log('[AP] Unfollow', who, '→', slug); }
319 return 202;
320 }
321 if (ot === 'Like' || ot === 'Announce') {
322 const tgt = act.object.object;
323 const pid = postIdFromNoteUrl(typeof tgt === 'string' ? tgt : (tgt && tgt.id), base);
324 if (who && pid) { iStmts().delLA.run(ot.toLowerCase(), pid, who); console.log('[AP] Undo', ot, who, '→', pid); }
325 return 202;
326 }
327 return 202;
328 }
329
330 const actorUri = typeof act.actor === 'string' ? act.actor : (act.actor && act.actor.id);
331 const resolveActor = async (uri) => ((verified && verified.id === uri) ? verified : await fetchActor(uri).catch(() => null));
332
333 // Inbound reply: a Create whose object replies to one of our notes.
334 if (type === 'Create' && act.object && (act.object.type === 'Note' || act.object.type === 'Article')) {
335 const o = act.object;
336 const pid = postIdFromNoteUrl(o.inReplyTo, base);
337 if (pid && actorUri && localPostExists(pid)) {
338 const ai = actorInfo(await resolveActor(actorUri), actorUri);
339 const html = HtmlSanitizerService.sanitize(o.content || '');
340 iStmts().ins.run('reply', pid, o.id || '', actorUri, ai.name, ai.handle, ai.url, ai.icon, html, o.published || null);
341 console.log('[AP] reply', actorUri, '→', pid);
342 }
[5bf63b7]343 return 202;
344 }
[c16e0a5]345 if (type === 'Like' || type === 'Announce') {
346 const tgt = act.object;
347 const pid = postIdFromNoteUrl(typeof tgt === 'string' ? tgt : (tgt && tgt.id), base);
348 if (pid && actorUri && localPostExists(pid)) {
349 const ai = actorInfo(await resolveActor(actorUri), actorUri);
350 iStmts().ins.run(type.toLowerCase(), pid, '', actorUri, ai.name, ai.handle, ai.url, ai.icon, null, null);
351 console.log('[AP]', type === 'Like' ? 'like' : 'boost', actorUri, '→', pid);
352 }
353 return 202;
354 }
355 if (type === 'Delete') {
356 // A remote reply was deleted upstream → drop it if we stored it.
357 const oid = typeof act.object === 'string' ? act.object : (act.object && act.object.id);
358 if (oid) iStmts().delReply.run(oid);
359 return 202;
360 }
361
[5bf63b7]362 console.log('[AP] inbox', type || 'unknown', '→', slugParam || 'shared', '(ignored)');
363 return 202;
364}
365
366// Deliver a new post as Create(Note) to all followers' inboxes (fire-and-forget).
367// Needs PUBLIC_BASE_URL (absolute URLs); no-op without followers or base.
368export async function deliverCreate(site, post) {
369 const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
370 if (!base || !site || !site.slug) return;
371 const followers = fStmts().list.all(site.slug);
372 if (!followers.length) return;
373 const inboxes = [...new Set(followers.map((f) => f.shared_inbox || f.inbox).filter(Boolean))];
374 const keys = getOrCreateKeys(site.slug);
375 const keyId = `${actorId(base, site.slug)}#main-key`;
376 const create = buildCreate(base, site, post);
377 for (const inbox of inboxes) deliver(inbox, create, keyId, keys.private_pem).catch(() => { /* best-effort */ });
378}
379
[eb852c5]380// Tell followers a post is gone (Delete + Tombstone) so it's removed from their feeds.
381export async function deliverDelete(site, post) {
382 const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
383 if (!base || !site || !site.slug || !post || !post.id) return;
384 const followers = fStmts().list.all(site.slug);
385 if (!followers.length) return;
386 const inboxes = [...new Set(followers.map((f) => f.shared_inbox || f.inbox).filter(Boolean))];
387 const keys = getOrCreateKeys(site.slug);
388 const me = actorId(base, site.slug);
389 const nid = noteId(base, post.id);
390 const del = {
391 '@context': 'https://www.w3.org/ns/activitystreams',
392 id: `${nid}#delete-${Date.now()}`,
393 type: 'Delete',
394 actor: me,
395 to: [PUBLIC],
396 object: { id: nid, type: 'Tombstone' },
397 };
398 for (const inbox of inboxes) deliver(inbox, del, `${me}#main-key`, keys.private_pem).catch(() => { /* best-effort */ });
399}
400
[6bd25d1]401export default {
402 getOrCreateKeys, apWants, sendAP, actorId, noteId,
403 buildActor, buildNote, buildCreate, buildOutbox, buildFollowers,
[eb852c5]404 followerCount, deliver, fetchActor, verifyRequest, handleInbox, deliverCreate, deliverDelete,
[c16e0a5]405 getInteractions,
[6bd25d1]406};
Note: See TracBrowser for help on using the repository browser.