${escTitle}
` : ''; // Images travel as AP `attachment` (Mastodon strips🎵 ${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${tagLinks.join(' ')}
`; } const seen = new Set(); const attachment = urls.filter(Boolean) .filter((u) => { if (seen.has(u)) return false; seen.add(u); return true; }) .map((u) => ({ type: 'Document', mediaType: mediaType(u), url: u })); 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 ? [`${aId}/followers`] : [PUBLIC], cc: post.fan_only ? [] : [`${aId}/followers`], tag: buildHashtagList(base, post.tags, body), 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, }; if (post.nsfw) note.summary = post.content_warning || 'Gevoelige inhoud'; if (attachment.length) note.attachment = attachment; // Playable-audio posts suppress the cover attachment (player card). Still expose // the cover via AS2 `image` so card/grid consumers (the Klonkt Cirkel) can show // it — Mastodon ignores a Note's `image`, so the player card is unaffected. if (post.cover_image_url && playable) { const cov = abs(post.cover_image_url); if (cov) note.image = { type: 'Image', mediaType: mediaType(cov), url: cov }; } // 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)}`; 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; } } export function buildCreate(base, site, post) { const note = buildNote(base, site, post); return { '@context': 'https://www.w3.org/ns/activitystreams', id: note.id + '#create', type: 'Create', actor: actorId(base, site.slug), published: note.published, to: note.to, cc: note.cc, object: note, }; } export function buildOutbox(base, site, posts) { const id = `${actorId(base, site.slug)}/outbox`; const items = (posts || []).slice(0, MAX_OUTBOX).map((p) => buildCreate(base, site, p)); return { '@context': 'https://www.w3.org/ns/activitystreams', id, type: 'OrderedCollection', totalItems: items.length, orderedItems: items, }; } export function buildFollowers(base, site, count) { const id = `${actorId(base, site.slug)}/followers`; return { '@context': 'https://www.w3.org/ns/activitystreams', id, type: 'OrderedCollection', totalItems: count || 0, orderedItems: [], // hidden for privacy; count only }; } // 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) { const id = `${actorId(base, site.slug)}/following`; return { '@context': 'https://www.w3.org/ns/activitystreams', id, type: 'OrderedCollection', totalItems: count || 0, orderedItems: [], // count only }; } // 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) { const id = `${actorId(base, site.slug)}/featured`; const items = (posts || []).map((p) => buildNote(base, site, p)); return { '@context': 'https://www.w3.org/ns/activitystreams', id, type: 'OrderedCollection', totalItems: items.length, orderedItems: items, }; } // ── followers store (lazy stmts) ────────────────────────────────── let _insF, _delF, _listF, _cntF; function fStmts() { if (!_insF) { _insF = db.prepare('INSERT OR IGNORE INTO ap_followers (slug, actor_uri, inbox, shared_inbox, created_at) VALUES (?,?,?,?,CURRENT_TIMESTAMP)'); _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; } // ── inbound interactions store (replies / likes / boosts) + our outbound replies ── let _insI, _delLA, _delReply, _listI, _getI, _insO, _listO, _getO; 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, 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 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, 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). function postIdFromNoteUrl(url, base) { const s = String(url || ''); if (base && !s.startsWith(base)) return null; const m = s.match(/\/ap\/notes\/([^/?#]+)/); return m ? decodeURIComponent(m[1]) : null; } 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; return { name: (doc && (doc.name || doc.preferredUsername)) || handle, handle, url: safeUrl((doc && (doc.url || doc.id)) || actorUri) || null, icon: safeUrl(icon) || null, }; } // 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/ 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();
const rows = s.list.all(postId);
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;
const nodes = [];
for (const r of rows) {
if (r.kind !== 'reply') continue;
nodes.push({
noteId: r.object_uri, parent: r.parent_uri || null, mine: false, id: r.id,
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,
acted_boost: !!r.acted_boost, acted_like: !!r.acted_like,
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,
actor_name: siteName, actor_handle: siteHandle, actor_url: siteUrl, actor_icon: siteIcon,
children: [],
});
}
const byId = new Map(nodes.map((n) => [n.noteId, n]));
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; };
// 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) {
try {
const r = await safeFetch(url, { headers: { Accept: 'application/activity+json' } });
if (!r.ok) return null;
const len = Number(r.headers.get('content-length') || 0);
if (len > 2_000_000) return null; // refuse oversized actor docs
return await r.json();
} catch { return null; }
}
// ── 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 */ }
}
// 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) 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) { deliveryStmts().del.run(row.id); continue; }
const attempts = row.attempts + 1;
if (attempts >= DELIVERY_MAX_ATTEMPTS) { 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();
}
// 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.)
export async function verifyRequest(req) {
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;
const actor = await fetchActor(p.keyId.split('#')[0]);
const pem = actor && actor.publicKey && actor.publicKey.publicKeyPem;
if (!pem) return null;
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 */ }
}
if (ok && hs.includes('digest') && req.rawBody) {
const exp = 'SHA-256=' + crypto.createHash('sha256').update(req.rawBody).digest('base64');
if (req.headers['digest'] !== exp) ok = false;
}
return ok ? actor : null;
}
// Handle an incoming inbox POST. slugParam = null for the shared /ap/inbox.
export async function handleInbox(req, slugParam) {
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(/\/+$/, '');
const verified = await verifyRequest(req).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'];
if (GATED.includes(type)) {
if (!verified || !claimedActor || verified.id !== claimedActor) {
console.warn('[AP] inbox REJECTED (signature)', type, claimedActor || '?', 'from', ip, verified ? '(signer mismatch)' : '(unsigned/invalid)');
return 401;
}
}
if (type === 'Follow') {
const who = typeof act.actor === 'string' ? act.actor : (act.actor && act.actor.id);
const slug = slugParam || slugFromActorUrl(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;
fStmts().ins.run(slug, who, remote.inbox, sharedInbox);
const me = actorId(base, slug);
const keys = getOrCreateKeys(slug);
const accept = { '@context': 'https://www.w3.org/ns/activitystreams', 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;
}
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));
// Activities from our OWN actors are already stored via ap_outbox — don't re-store.
const isLocalActor = !!(base && actorUri && actorUri.startsWith(`${base}/ap/users/`));
// Inbound reply: a Create whose object replies to one of our notes (post OR comment).
if (type === 'Create' && act.object && (act.object.type === 'Note' || act.object.type === 'Article')) {
const o = act.object;
const tgt = findThreadTarget(o.inReplyTo, base);
if (tgt && actorUri && !isLocalActor) {
const ai = actorInfo(await resolveActor(actorUri), actorUri);
const html = HtmlSanitizerService.sanitize(o.content || '');
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);
console.log('[AP] reply', actorUri, '→', tgt.post_id);
return 202;
}
// Home timeline (client): a top-level post from an account we follow.
if (actorUri && !isLocalActor && !o.inReplyTo && o.id) {
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 = HtmlSanitizerService.sanitize(o.content || '');
const _atts = (Array.isArray(o.attachment) ? o.attachment : []).map((a) => ({ url: safeUrl(a && a.url), type: (a && a.mediaType) || '' })).filter((m) => m.url);
// Fallback cover: a Note's `image` (set when the attachment was suppressed
// for a player-card post, e.g. hosted-audio posts).
if (!_atts.some((m) => !m.type || /image/i.test(m.type)) && o.image) {
const _im = Array.isArray(o.image) ? o.image[0] : o.image;
const _iu = safeUrl(typeof _im === 'string' ? _im : (_im && _im.url));
if (_iu) _atts.push({ url: _iu, type: (_im && _im.mediaType) || 'image/jpeg' });
}
const media = JSON.stringify(_atts);
// "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, o.url || null, o.published || null, media, o.sensitive ? 1 : 0, o.summary || null);
}
console.log('[AP] timeline +', actorUri, 'x' + subs.length);
}
}
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)) {
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);
console.log('[AP]', type === 'Like' ? 'like' : 'boost', actorUri, '→', pid);
} 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);
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, bn.summary || null); inserted = r.changes > 0; } catch { /* ignore */ }
if (inserted) { try { db.prepare('UPDATE ap_timeline SET reblog_name = ?, reblog_handle = ?, reblog_icon = ? WHERE slug = ? AND id = ?').run(booster.name, booster.handle, booster.icon, s.slug, bn.id); } catch { /* ignore */ } }
}
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 */ }
}
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);
if (fid) { try { fwStmts().acc.run(fid); } catch { /* ignore */ } }
console.log('[AP] follow accepted', actorUri);
return 202;
}
if (type === 'Reject' && act.object) {
const who = actorUri;
if (who && slugParam) { try { fwStmts().del.run(slugParam, who); } catch { /* ignore */ } }
return 202;
}
console.log('[AP] inbox', type || 'unknown', '→', slugParam || 'shared', 'from', ip, '(ignored)');
return 202;
}
// 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) {
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 keyId = `${actorId(base, site.slug)}#main-key`;
const create = buildCreate(base, site, post);
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;
const recent = db.prepare(
`SELECT id, slug, title, content, cover_image_url, nsfw, content_warning, published_at, created_at
FROM posts WHERE site_id = ? AND status = 'published' AND (fan_only IS NULL OR fan_only = 0)
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': 'https://www.w3.org/ns/activitystreams',
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 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 note = buildNote(base, site, post);
note.updated = new Date().toISOString();
const update = {
'@context': 'https://www.w3.org/ns/activitystreams',
id: `${noteId(base, post.id)}#update-${Date.now()}-${rid()}`,
type: 'Update', actor: me, to: [PUBLIC], cc: [`${me}/followers`],
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': ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'],
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.
export async function resyncFeaturedPins(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 AS = 'https://www.w3.org/ns/activitystreams';
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': AS, 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': AS, 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) {
return String(html || '').replace(/(^|[\s>])#([\p{L}\p{M}\p{N}_]+)/gu, (m, pre, tag) =>
`${pre}#${tag}`);
}
// Extract the AP Hashtag tag objects from already-linked reply content.
function hashtagTags(base, content) {
const tags = [], seen = new Set();
const re = /class="[^"]*\bhashtag\b[^"]*"[^>]*>#([\p{L}\p{M}\p{N}_]+) x.trim()).filter(Boolean);
}
return [];
}
// A tag → { label, slug }. Multi-word tags become CamelCase (#LiveMusic) for the display
// name (Mastodon hashtags can't contain spaces; CamelCase is the accessibility norm); the
// slug/href stays lowercase ("livemusic").
function tagParts(raw) {
const words = String(raw || '').trim().split(/[\s_]+/).map((w) => w.replace(/[^\p{L}\p{M}\p{N}]/gu, '')).filter(Boolean);
if (!words.length) return null;
const slug = words.join('').toLowerCase();
if (!slug) return null;
const label = words.length > 1 ? words.map((w) => w[0].toUpperCase() + w.slice(1)).join('') : words[0];
return { label, slug };
}
// Merge a post's tags field + the #hashtags linked inline in its body into one deduped
// Hashtag tag list (with hrefs to our /tag page).
function buildHashtagList(base, tagsField, content) {
const out = [], seen = new Set();
for (const t of normalizeTags(tagsField)) {
const p = tagParts(t); if (!p || seen.has(p.slug)) continue; seen.add(p.slug);
out.push({ type: 'Hashtag', href: `${base}/tag/${encodeURIComponent(p.slug)}`, name: '#' + p.label });
}
for (const h of hashtagTags(base, content)) {
const k = h.name.slice(1).toLowerCase(); if (seen.has(k)) continue; seen.add(k);
out.push(h);
}
return out;
}
// Extract Mention tag objects from already-linked content (class="u-url mention").
function mentionTags(content) {
const tags = [], seen = new Set();
// The link href is the human profile URL; the actor URI (for the Mention tag) is in data-actor.
const re = /@([^<]+)<\/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();
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) {
const me = actorId(base, site.slug);
return {
id: noteId(base, row.id),
type: 'Note',
attributedTo: me,
inReplyTo: row.in_reply_to || undefined,
content: row.content,
url: row.post_slug ? `${base}/${encodeURIComponent(row.post_slug)}` : undefined,
published: toISO(row.created_at),
to: row.to_actor ? [row.to_actor] : [PUBLIC],
cc: [PUBLIC, `${me}/followers`],
tag: [
...mentionTags(row.content),
...hashtagTags(base, row.content),
],
};
}
// 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);
}
// Send a reply FROM this site to a remote actor (in reply to their inbound reply).
// `parent` = an ap_interactions row (actor_uri, actor_url, actor_handle, object_uri).
export async function deliverReply(site, { postId, postSlug, parent, text }) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
if (!base || !site || !site.slug || !parent || !String(text || '').trim()) return null;
const me = actorId(base, site.slug);
const handle = parent.actor_handle || deriveHandle(parent.actor_uri);
const dispHandle = handle && handle[0] === '@' ? handle : '@' + (handle || '');
const body = escHtml(String(text).trim()).replace(/\r?\n/g, ' ${mention}${linkHashtags(base, mres.html)} ${note.name} ${mention}${linkHashtags(base, mres.html)}
');
const mres = await resolveMentionsInText(base, body); // link inline @mentions + collect their inboxes
const mention = parent.actor_uri
? `${escHtml(dispHandle)} ` : '';
const content = `
→ newline)
// so the manage view can prefill an edit box; the mention is re-added on save.
function outboxEditableText(content) {
return String(content || '')
.replace(/
/gi, '\n')
.replace(/]*>([\s\S]*?)<\/a>/gi, '$1')
.replace(/<[^>]+>/g, '')
.replace(/</g, '<').replace(/>/g, '>').replace(/&/g, '&')
.trim();
}
export function listOutbox(siteSlug) {
return db.prepare('SELECT id, content, to_handle, in_reply_to, created_at FROM ap_outbox WHERE site_slug = ? ORDER BY created_at DESC')
.all(siteSlug).map((r) => { const c = stripLeadingMentions(r.content); return { ...r, content: c, editable: outboxEditableText(c) }; });
}
// Delete one of our outbound replies: send Delete(Tombstone) to recipients + remove it.
export async function deliverOutboxDelete(site, outboxId) {
const row = iStmts().getO.get(outboxId);
if (!row || row.site_slug !== site.slug) return false;
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
if (base) {
const me = actorId(base, site.slug);
const nid = noteId(base, row.id);
const del = { '@context': 'https://www.w3.org/ns/activitystreams', id: `${nid}#delete-${Date.now()}-${rid()}`, type: 'Delete', actor: me, to: [PUBLIC], object: { id: nid, type: 'Tombstone' } };
const keys = getOrCreateKeys(site.slug);
const inboxes = new Set();
if (row.to_actor) { const a = await fetchActor(row.to_actor).catch(() => null); if (a) inboxes.add((a.endpoints && a.endpoints.sharedInbox) || a.inbox); }
for (const f of fStmts().list.all(site.slug)) inboxes.add(f.shared_inbox || f.inbox);
for (const inbox of [...inboxes].filter(Boolean)) { try { await deliver(inbox, del, `${me}#main-key`, keys.private_pem); } catch { /* best-effort */ } }
}
db.prepare('DELETE FROM ap_outbox WHERE id = ?').run(outboxId);
return true;
}
// Edit one of our outbound replies: rewrite the stored content (mention re-added + #tags
// re-linked) and send an Update(Note) so recipients refresh their cached copy.
export async function deliverOutboxUpdate(site, outboxId, newText) {
const row = iStmts().getO.get(outboxId);
if (!row || row.site_slug !== site.slug) return false;
const text = String(newText || '').trim();
if (!text) return false;
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
if (!base) return false;
const me = actorId(base, site.slug);
const toActor = row.to_actor ? await fetchActor(row.to_actor).catch(() => null) : null;
const toProfile = row.to_actor ? (actorInfo(toActor, row.to_actor).url || row.to_actor) : '';
const _h = row.to_handle || deriveHandle(row.to_actor);
const toHandle = _h && _h[0] === '@' ? _h : '@' + (_h || '');
const mention = row.to_actor
? `${escHtml(toHandle)} ` : '';
const mres = await resolveMentionsInText(base, escHtml(text).replace(/\r?\n/g, '
'));
const content = `