]*>\s*@[^<]+<\/a>[ ]*)+/i, (m, p) => p || '');
s = s.replace(/^(\s*]*>)?\s*(?:@[\w.-]+(?:@[\w.-]+)?[ ]+)+/i, (m, p) => p || '');
return s;
}
// View-ready threaded view of a post's fediverse activity (inbound replies +
// our outbound replies, nested), plus like/boost counts.
export function getInteractions(postId, base, site) {
const s = iStmts();
// Privacy: a followers-only or direct (DM) reply is addressed to people, not to the
// public web, so it must NOT render in the public thread. It still reaches the owner
// via notifications (post context + reference included there). Legacy rows without a
// visibility value are treated as public. Likes/boosts stay counted (count-only).
const rows = s.list.all(postId).filter((r) =>
r.kind !== 'reply' || !(r.visibility === 'followers' || r.visibility === 'direct'));
const baseClean = (base || process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
const postNoteId = baseClean ? `${baseClean}/ap/notes/${postId}` : null;
// Our own (outbound) replies show the SITE identity for everyone (not "You").
let host = ''; try { host = new URL(baseClean).host; } catch { /* ignore */ }
const siteName = (site && (site.title || site.slug)) || '';
const siteHandle = (site && site.slug && host) ? `@${site.slug}@${host}` : '';
const siteUrl = baseClean ? `${baseClean}/` : '';
const siteIcon = (site && site.profile_photo) || null;
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_uri: r.actor_uri,
actor_name: r.actor_name, actor_handle: r.actor_handle, actor_url: r.actor_url,
actor_icon: r.actor_icon, content: stripLeadingMentions(r.content), created_at: r.published || r.created_at,
emoji_json: r.emoji_json, actor_emoji_json: r.actor_emoji_json, // FEP-9098 (thread render)
acted_boost: !!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,
media: (() => { try { return o.attachments ? JSON.parse(o.attachments) : []; } catch { return []; } })(),
actor_name: siteName, actor_handle: siteHandle, actor_url: siteUrl, actor_icon: siteIcon,
children: [],
});
}
const byId = new Map(nodes.map((n) => [n.noteId, n]));
// Conversation partners per node (u02, the reply editor's mentions bar): the
// node's author plus the ancestor authors up the chain. Our own nodes are
// skipped (we do not mention ourselves), deduped by actor, capped at 8.
for (const n of nodes) {
const seen = new Set();
const list = [];
let cur = n, guard = 0;
while (cur && guard++ < 12 && list.length < 8) {
if (!cur.mine && cur.actor_uri && !seen.has(cur.actor_uri)) {
seen.add(cur.actor_uri);
list.push({
uri: cur.actor_uri,
url: cur.actor_url || cur.actor_uri,
handle: cur.actor_handle || deriveHandle(cur.actor_uri),
});
}
cur = cur.parent ? byId.get(cur.parent) : null;
}
n.participants = list;
}
const isTop = (n) => !n.parent || n.parent === postNoteId || !byId.has(n.parent);
const tops = [];
for (const n of nodes) {
if (isTop(n)) { tops.push(n); continue; }
let anc = n, guard = 0;
while (!isTop(anc) && guard++ < 12) anc = byId.get(anc.parent);
anc.children.push(n);
}
const byTime = (a, b) => new Date(a.created_at) - new Date(b.created_at);
tops.sort(byTime).forEach((t) => t.children.sort(byTime));
return {
thread: tops,
likeCount: rows.filter((r) => r.kind === 'like').length,
announceCount: rows.filter((r) => r.kind === 'announce').length,
total: nodes.length,
};
}
// ── HTTP Signatures + delivery ────────────────────────────────────
const slugFromActorUrl = (url) => { const m = String(url || '').match(/\/ap\/users\/([^/?#]+)/); return m ? decodeURIComponent(m[1]) : null; };
// Which of OUR sites are named in a note's Mention tags? Only hrefs on our own base count
// (an /ap/users/ path on a remote host is someone else's actor), and the slug must be
// an existing site. Deduped.
export function localMentionSlugs(tags, base) {
if (!base) return [];
const out = [], seen = new Set();
for (const t of (Array.isArray(tags) ? tags : (tags ? [tags] : []))) {
if (!t || t.type !== 'Mention' || typeof t.href !== 'string') continue;
if (!t.href.startsWith(base + '/ap/users/')) continue;
const slug = slugFromActorUrl(t.href);
if (!slug || seen.has(slug)) continue; seen.add(slug);
try { if (db.prepare('SELECT 1 FROM sites WHERE slug = ?').get(slug)) out.push(slug); } catch { /* ignore */ }
}
return out;
}
// Sign + POST an activity to a remote inbox (draft-cavage HTTP Signatures, RSA-SHA256).
export async function deliver(inboxUrl, bodyObj, keyId, privatePem) {
const body = JSON.stringify(bodyObj);
const u = new URL(inboxUrl);
const date = new Date().toUTCString();
const digest = 'SHA-256=' + crypto.createHash('sha256').update(body).digest('base64');
const signingString = `(request-target): post ${u.pathname}\nhost: ${u.host}\ndate: ${date}\ndigest: ${digest}`;
const signature = crypto.sign('sha256', Buffer.from(signingString), privatePem).toString('base64');
const sig = `keyId="${keyId}",algorithm="rsa-sha256",headers="(request-target) host date digest",signature="${signature}"`;
const r = await safeFetch(inboxUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/activity+json', Accept: 'application/activity+json', Date: date, Digest: digest, Signature: sig },
body,
});
return r.status;
}
export async function fetchActor(url) {
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 */ }
}
// Record delivery health per follower so the followers list can flag dead accounts.
// Keyed by inbox: a shared-inbox POST reaches every follower behind it, so all of them
// are marked. A non-follower inbox (inline @mention) simply matches 0 rows.
let _fDelivOk, _fDelivErr;
function markFollowerDelivery(slug, inbox, ok) {
if (!slug || !inbox) return;
try {
if (!_fDelivOk) {
_fDelivOk = db.prepare('UPDATE ap_followers SET last_delivery_at = CURRENT_TIMESTAMP WHERE slug = ? AND (inbox = ? OR shared_inbox = ?)');
_fDelivErr = db.prepare('UPDATE ap_followers SET last_error_at = CURRENT_TIMESTAMP WHERE slug = ? AND (inbox = ? OR shared_inbox = ?)');
}
(ok ? _fDelivOk : _fDelivErr).run(slug, inbox, inbox);
} catch { /* health tracking is non-fatal */ }
}
// Deliver now; queue for retry if it fails.
export async function deliverWithRetry(slug, inbox, activity, keyId, privPem) {
if (!inbox) return;
try { const st = await deliver(inbox, activity, keyId, privPem); if (st >= 200 && st < 300) { markFollowerDelivery(slug, inbox, true); return; } } catch { /* queue below */ }
enqueueDelivery(slug, inbox, activity);
}
let _processingDeliv = false;
export async function processDeliveryQueue() {
if (_processingDeliv) return; // re-entrancy guard: 30 rows × 8s can exceed the 60s tick → no double-delivery
_processingDeliv = true;
try {
let rows;
try { rows = deliveryStmts().due.all(); } catch { return; }
if (!rows || !rows.length) return;
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
for (const row of rows) {
let ok = false;
try {
const keys = getOrCreateKeys(row.slug);
const st = await deliver(row.inbox, JSON.parse(row.body), `${actorId(base, row.slug)}#main-key`, keys.private_pem);
ok = st >= 200 && st < 300;
} catch { ok = false; }
if (ok) { markFollowerDelivery(row.slug, row.inbox, true); deliveryStmts().del.run(row.id); continue; }
const attempts = row.attempts + 1;
if (attempts >= DELIVERY_MAX_ATTEMPTS) { markFollowerDelivery(row.slug, row.inbox, false); deliveryStmts().del.run(row.id); console.warn('[AP] delivery gave up after', attempts, 'tries →', row.inbox); continue; }
// Index the backoff on the CURRENT attempt count (row.attempts) so the first
// retry uses the 1-min tier instead of skipping it.
const mins = DELIVERY_BACKOFF_MIN[Math.min(row.attempts, DELIVERY_BACKOFF_MIN.length - 1)];
deliveryStmts().bump.run(attempts, new Date(Date.now() + mins * 60000).toISOString(), row.id);
}
} finally { _processingDeliv = false; }
}
let _delivTimer = null;
export function startDeliveryWorker() {
if (_delivTimer) return;
_delivTimer = setInterval(() => { processDeliveryQueue().catch(() => {}); }, 60 * 1000);
if (_delivTimer.unref) _delivTimer.unref();
}
// Best-effort verification of an incoming signed request. Returns the sender's
// actor doc if the signature checks out, else null. (Not gating yet — MVP.)
// Max clock skew for the signed Date header (replay window). Generous default to tolerate
// federating servers with drifting clocks; an operator can widen it via env.
const SIG_MAX_SKEW_MS = (Number(process.env.AP_SIG_MAX_SKEW_MIN) || 60) * 60 * 1000;
export async function verifyRequest(req) {
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 */ }
}
// Replay defence: the Date header must be signed and recent. A captured signed request
// replayed later (or with a swapped body) is rejected.
if (ok) {
if (!hs.includes('date')) ok = false;
else {
const t = Date.parse(req.headers['date'] || '');
if (isNaN(t) || Math.abs(Date.now() - t) > SIG_MAX_SKEW_MS) ok = false;
}
}
// Digest is MANDATORY when the request carries a body: without a signed digest the body
// isn't covered by the signature and could be swapped on a replay.
if (ok && req.rawBody && req.rawBody.length) {
if (!hs.includes('digest')) ok = false;
else {
const exp = 'SHA-256=' + crypto.createHash('sha256').update(req.rawBody).digest('base64');
if (req.headers['digest'] !== exp) ok = false;
}
}
return ok ? actor : null;
}
// Parse a fediverse poll (an ActivityStreams `Question` — the Mastodon-standard poll form)
// into our compact shape. `oneOf` = single choice, `anyOf` = multiple; each option is a Note
// with a `name` and a `replies` collection whose `totalItems` is that option's vote count.
function parsePoll(o) {
if (!o || o.type !== 'Question') return null;
const raw = Array.isArray(o.oneOf) ? o.oneOf : (Array.isArray(o.anyOf) ? o.anyOf : null);
if (!raw || !raw.length) return null;
const options = raw.slice(0, 12).map((opt) => ({
name: String((opt && opt.name) || '').slice(0, 300),
count: Math.max(0, Number(opt && opt.replies && opt.replies.totalItems) || 0),
})).filter((x) => x.name);
if (!options.length) return null;
const endTime = o.endTime || (typeof o.closed === 'string' ? o.closed : null);
const closed = !!o.closed || (endTime ? Date.parse(endTime) <= Date.now() : false);
return { multiple: Array.isArray(o.anyOf), options, endTime, closed, voters: Number(o.votersCount) || null, voted: null };
}
// ── Polls WE host (a local post with a poll) ──────────────────────
// Parse the poll definition stored on our own post (posts.poll_json). Counts are
// NOT stored here — they're derived from the poll_votes ballots so a re-render always
// reflects the authoritative tally.
export function parseOwnPoll(pollJson) {
if (!pollJson) return null;
let d; try { d = typeof pollJson === 'string' ? JSON.parse(pollJson) : pollJson; } catch { return null; }
if (!d || !Array.isArray(d.options)) return null;
const options = d.options.map((o) => ({ name: String((o && o.name != null ? o.name : o) || '').slice(0, 300) })).filter((o) => o.name);
if (options.length < 2) return null;
const endTime = d.endTime || null;
const closed = !!d.closed || (endTime ? Date.parse(endTime) <= Date.now() : false);
return { multiple: !!d.multiple, options, endTime, closed };
}
// Live tally of a hosted poll from its ballots: per-option counts + unique voters.
export function pollTally(postId) {
const counts = {}; let voters = 0;
try {
for (const r of db.prepare('SELECT choice, COUNT(*) AS n FROM poll_votes WHERE post_id = ? GROUP BY choice').all(postId)) counts[r.choice] = r.n;
voters = db.prepare('SELECT COUNT(DISTINCT actor_uri) AS n FROM poll_votes WHERE post_id = ?').get(postId).n || 0;
} catch { /* table may not exist yet */ }
return { counts, voters };
}
// Render-ready view of a hosted poll (options with counts + percentages, totals, state).
// Voting is fediverse-only, so this is display-only on the site.
export function ownPollView(post) {
const poll = parseOwnPoll(post && post.poll_json);
if (!poll) return null;
const { counts, voters } = pollTally(post.id);
const total = Object.values(counts).reduce((a, b) => a + b, 0);
const denom = poll.multiple ? voters : total; // multiple-choice %: share of voters (can sum >100%)
const options = poll.options.map((o) => {
const count = counts[o.name] || 0;
return { name: o.name, count, pct: denom ? Math.round((count / denom) * 100) : 0 };
});
return { multiple: poll.multiple, options, total, voters, endTime: poll.endTime, closed: poll.closed };
}
// Attach the AS2 Question shape to a note built for a hosted poll. Mastodon renders a
// status with either media OR a poll (never both), so a poll federates as content +
// options with no media attachment. oneOf = single choice, anyOf = multiple.
function applyPollToNote(note, postId, poll) {
const { counts, voters } = pollTally(postId);
const opts = poll.options.map((o) => ({
type: 'Note',
name: o.name,
replies: { type: 'Collection', totalItems: counts[o.name] || 0 },
}));
note.type = 'Question';
note[poll.multiple ? 'anyOf' : 'oneOf'] = opts;
if (poll.endTime) note.endTime = new Date(poll.endTime).toISOString();
// Once closed, Mastodon expects a `closed` timestamp (the effective end).
if (poll.closed) note.closed = poll.endTime ? new Date(poll.endTime).toISOString() : new Date().toISOString();
note.votersCount = voters;
delete note.attachment; // media ATTACHMENTS + a poll are mutually exclusive on Mastodon
// Keep note.image: it's the cover, which Mastodon ignores on a Question anyway
// (same as on any Note) but Klonkt reads to show the cover in feeds/the Cirkel.
// Deleting it stripped the cover off every boosted poll.
return note;
}
// Record an inbound ballot on one of OUR polls. A vote arrives as a Create(Note) whose
// `name` is the chosen option and `inReplyTo` is our poll note — the Mastodon-standard
// vote form. Returns { handled } — handled=true means it was addressed to a poll (so the
// caller must NOT also store it as a reply), false means "not a poll, fall through".
function recordPollBallot(postId, actorUri, rawChoice) {
const choice = String(rawChoice == null ? '' : rawChoice).slice(0, 300);
if (!choice) return { handled: false };
let post; try { post = db.prepare('SELECT poll_json FROM posts WHERE id = ?').get(postId); } catch { return { handled: false }; }
const poll = post && parseOwnPoll(post.poll_json);
if (!poll) return { handled: false }; // not a poll → let the reply logic handle it
if (poll.closed) return { handled: true }; // voting closed → drop
if (!poll.options.some((o) => o.name === choice)) return { handled: true }; // unknown option → drop
try {
// Single choice = one ballot per actor: ignore a later/different vote. Multiple choice
// allows one ballot per distinct option (the UNIQUE(post,actor,choice) dedupes repeats).
if (!poll.multiple && db.prepare('SELECT 1 FROM poll_votes WHERE post_id = ? AND actor_uri = ? LIMIT 1').get(postId, actorUri)) return { handled: true };
db.prepare('INSERT OR IGNORE INTO poll_votes (post_id, actor_uri, choice) VALUES (?, ?, ?)').run(postId, actorUri, choice);
} catch { return { handled: true }; }
schedulePollUpdate(postId);
return { handled: true };
}
// Coalesce a burst of votes into ONE Update(Question) per poll: the first vote schedules a
// refresh ~15s out; further votes in that window ride the same pending update (which carries
// the accumulated tally). Non-follower voters re-fetch the Question (live tally) themselves.
const _pollUpdTimers = new Map();
function schedulePollUpdate(postId) {
if (_pollUpdTimers.has(postId)) return;
const t = setTimeout(() => { _pollUpdTimers.delete(postId); deliverPollUpdate(postId).catch(() => { /* best-effort */ }); }, 15000);
if (t.unref) t.unref();
_pollUpdTimers.set(postId, t);
}
// Push the fresh poll tally (or closed state) to followers as Update(Question).
export async function deliverPollUpdate(postId) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
if (!base || !postId) return;
let post, site;
try {
post = db.prepare('SELECT * FROM posts WHERE id = ?').get(postId);
if (!post || !post.poll_json) return;
site = db.prepare('SELECT * FROM sites WHERE id = ?').get(post.site_id);
} catch { return; }
if (site) await deliverUpdate(site, post);
}
// ── Web push to the owner (docs/webpush-design.md, slice 3) ─────────
// Fire-and-forget: a notification must never block or break inbox processing.
function pushEvent(slug, event) {
try { Push.notifySite(slug, event).catch(() => {}); } catch { /* never throw */ }
}
// Hub-aware path prefix for a site's pages ('' in solo).
function pushPrefix(slug) {
try { return getTenancy() === 'hub' ? `/user/${slug}` : ''; } catch { return ''; }
}
// Notification language: the site's content language (fallback: instance default).
function pushLang(slug) {
try { const r = db.prepare('SELECT language FROM sites WHERE slug = ?').get(slug); return (r && r.language) || process.env.KLONKT_DEFAULT_LANG || 'nl'; } catch { return 'nl'; }
}
// Site slug, target URL and title for a post-scoped notification.
function pushPostCtx(postId) {
try {
const r = db.prepare('SELECT p.slug AS post, p.title, s.slug AS site FROM posts p JOIN sites s ON s.id = p.site_id WHERE p.id = ?').get(postId);
if (!r) return null;
return { site: r.site, title: r.title || r.post, url: `${pushPrefix(r.site)}/${r.post}#fediverse` };
} catch { return null; }
}
// 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', 'Flag', 'Offer'];
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;
}
}
// FEP-633c §5.3 (modelled on the adoption offer): a gated follow forwarded to
// the guardians as an Offer(Follow), their Accept/Reject back to the ward.
if ((type === 'Offer' || type === 'Accept' || type === 'Reject') && act['shaer:followApproval'] === true) {
if (await handleFollowApprovalInbox(act, slugParam)) { console.log('[AP] follow-approval', type, 'from', claimedActor); return 202; }
}
// FEP-633c: the adoption handshake. An Offer lands at the local ward; an
// Accept/Reject answers an offer a local guardian sent. Anything the
// guardianship module does not recognize falls through to the old paths.
if (type === 'Offer' || type === 'Accept' || type === 'Reject') {
// Every LOCAL party this activity is addressed to gets its own copy of the
// handshake (a ward and a co-guardian may both live here). Gather candidate
// local slugs from the inbox owner, the `to` list, and the ward.
const cand = new Set();
if (slugParam) cand.add(slugParam);
for (const t of (Array.isArray(act.to) ? act.to : (act.to ? [act.to] : []))) {
if (typeof t === 'string') { const s = slugFromActorUrl(t); if (s) cand.add(s); }
}
if (type === 'Offer') {
const rel = Guardianship.parseRelationship(act.object);
if (rel) { const s = slugFromActorUrl(rel.ward); if (s) cand.add(s); }
}
let consumed = false;
for (const slug of cand) {
const gsite = db.prepare('SELECT * FROM sites WHERE slug = ?').get(slug);
if (gsite && await Guardianship.handleGuardianshipInbox(gsite, act).catch(() => false)) consumed = true;
}
if (consumed) { console.log('[AP] guardianship', type, 'from', claimedActor); return 202; }
}
// A moderation report (Flag) about our content — store it for the targeted site's owner
// (each Klonkt site is moderated by its own owner). Signature is enforced (GATED).
if (type === 'Flag') {
const objs = Array.isArray(act.object) ? act.object : (act.object ? [act.object] : []);
const objectUris = objs.map((o) => (typeof o === 'string' ? o : (o && o.id))).filter(Boolean);
let targetSlug = null;
const noteIds = [];
for (const u of objectUris) {
const s = slugFromActorUrl(u); // one of our actors?
if (s) { targetSlug = targetSlug || s; continue; }
const pid = postIdFromNoteUrl(u, base); // one of our notes?
if (pid) noteIds.push(pid);
}
if (!targetSlug && noteIds.length) {
try { const r = db.prepare('SELECT s.slug FROM posts p JOIN sites s ON s.id = p.site_id WHERE p.id = ? LIMIT 1').get(noteIds[0]); if (r) targetSlug = r.slug; } catch { /* ignore */ }
}
if (!targetSlug) return 202; // not about us / can't tell → drop
// Flag is GATED, so `verified` is the signer's (reporter's) actor doc already.
const ai = actorInfo(verified || null, claimedActor);
try {
db.prepare('INSERT INTO ap_reports (slug, actor_uri, actor_name, actor_handle, actor_icon, content, objects, created_at) VALUES (?,?,?,?,?,?,?,CURRENT_TIMESTAMP)')
.run(targetSlug, claimedActor || null, ai.name, ai.handle, ai.icon, HtmlSanitizerService.toPlainText(act.content || '').slice(0, 3000), JSON.stringify(objectUris.slice(0, 20)));
console.log('[AP] report received for', targetSlug, 'from', claimedActor);
} catch { /* ignore */ }
return 202;
}
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;
const fi = actorInfo(remote, who); // cache display for the friends list (shaer-aa3)
// FEP-633c §5.3: if the followed actor is a WARD (has guardians), the
// follow is gated. A committed guardian's own Follow is auto-accepted
// (it needs no gate); anyone else is held pending for guardian approval.
// Free actors / normal sites have no guardians → fall through, unchanged.
const wardGuardians = Guardianship.listGuardians(slug).map((g) => g.other_uri);
if (wardGuardians.length && !wardGuardians.includes(who)) {
const followId = (typeof act.id === 'string' && act.id) || `${who}#follow-${Date.now()}-${rid()}`;
Guardianship.follows.recordPending(slug, {
id: followId, follower: who, inbox: remote.inbox, sharedInbox,
name: fi.name, handle: fi.handle, icon: fi.icon, activity: act,
});
// FEP-633c §5.3, modelled on the guardian offer: the ward forwards the
// gated follow to its guardians for approval. A LOCAL guardian gets a
// push and reads /guardian directly; a REMOTE guardian gets an
// Offer(Follow) delivered so its instance stores a copy (same distributed
// pattern as the adoption offer). On quorum the ward returns Accept(Follow).
const wardActor = actorId(base, slug);
const wardKeys = getOrCreateKeys(slug);
const followObj = { id: followId, type: 'Follow', actor: who, object: wardActor };
for (const g of wardGuardians) {
// Local ONLY when the guardian lives on THIS instance: slugFromActorUrl
// ignores the host (an /ap/users/x path on a remote host is someone
// else's actor), so also require our base + an existing local site.
const gslug = g.startsWith(`${base}/`) ? slugFromActorUrl(g) : null;
const isLocal = gslug && db.prepare('SELECT 1 FROM sites WHERE slug = ?').get(gslug);
if (isLocal) {
const L = pushLang(gslug);
pushEvent(gslug, { type: 'guardian', title: i18nT(L, 'push.n_guard_cog_t'), body: i18nT(L, 'push.n_guard_cog_b', { who: fi.name || fi.handle || i18nT(L, 'notif.someone') }), url: `${pushPrefix(gslug)}/guardian` });
} else {
fetchActor(g).then((ga) => {
const inbox = ga && ((ga.endpoints && ga.endpoints.sharedInbox) || ga.inbox);
if (!inbox) return;
const offer = { '@context': AP_CONTEXT, id: `${wardActor}#followoffer-${Date.now()}-${rid()}`, type: 'Offer', actor: wardActor, to: [g], object: followObj, 'shaer:followApproval': true };
deliverWithRetry(slug, inbox, offer, `${wardActor}#main-key`, wardKeys.private_pem).catch(() => {});
}).catch(() => {});
}
}
console.log('[AP] Follow', who, '→ ward', slug, '(gated, awaiting guardians)');
return 202;
}
fStmts().ins.run(slug, who, remote.inbox, sharedInbox, fi.name, fi.handle, fi.icon);
try { _updFDisp.run(fi.name, fi.handle, fi.icon, slug, who); } catch { /* best effort */ }
{ const L = pushLang(slug); pushEvent(slug, { type: 'follow', title: i18nT(L, 'push.n_follow_t'), body: i18nT(L, 'push.n_follow_b', { who: fi.name || fi.handle || i18nT(L, 'notif.someone') }), url: `${pushPrefix(slug)}/connect` }); }
const me = actorId(base, slug);
const keys = getOrCreateKeys(slug);
const accept = { '@context': AP_CONTEXT, id: `${me}#accept-${Date.now()}-${rid()}`, type: 'Accept', actor: me, object: act };
deliver(remote.inbox, accept, `${me}#main-key`, keys.private_pem).catch((e) => console.warn('[AP] Accept delivery failed:', e.message));
// Auto-backfill: send our recent posts as Create so the instance has our history
// (Mastodon doesn't fetch history on follow). ONCE PER REMOTE INSTANCE only —
// Mastodon dedupes notes per-instance, so re-filling an instance that already has
// a follower of ours is wasted work (and won't re-populate the new follower's
// timeline anyway). Deliver to the shared inbox (instance-level) when present.
// Sync insert+check (no await between) → no interleave race with concurrent Follows.
const instanceFilled = sharedInbox &&
db.prepare('SELECT 1 FROM ap_followers WHERE slug = ? AND shared_inbox = ? AND actor_uri != ? LIMIT 1')
.get(slug, sharedInbox, who);
if (!instanceFilled) {
backfillNewFollower(base, slug, sharedInbox || remote.inbox).catch(() => { /* best-effort */ });
}
console.log('[AP] Follow', who, '→', slug, verified ? '(sig ok)' : '(sig unverified)');
return 202;
}
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' || act.object.type === 'Question')) {
const o = act.object;
// A poll ballot: a Note carrying a `name` (the chosen option) inReplyTo one of OUR poll
// posts. Record it (deduped per actor) BEFORE the reply logic so a vote is never stored
// as a comment. recordPollBallot returns handled=false only if the target isn't a poll.
if (o.name && o.inReplyTo && actorUri && !isLocalActor) {
const seg = postIdFromNoteUrl(o.inReplyTo, base);
if (seg && localPostExists(seg)) {
const rec = recordPollBallot(seg, actorUri, o.name);
if (rec.handled) { console.log('[AP] poll vote', actorUri, '→', seg); return 202; }
}
}
const tgt = findThreadTarget(o.inReplyTo, base);
if (tgt && actorUri && !isLocalActor) {
const ai = actorInfo(await resolveActor(actorUri), actorUri);
const html = HtmlSanitizerService.sanitize(o.content || '');
if (isRejectedObject(o.id)) { console.log('[AP] reply skipped (tombstoned)', o.id); return 202; }
iStmts().ins.run('reply', tgt.post_id, o.id || '', actorUri, ai.name, ai.handle, ai.url, ai.icon, html, o.published || null, tgt.parent_uri, noteVisibility(o), extractEmojiTags(o.tag), emojiJsonOf(ai.emojis));
console.log('[AP] reply', actorUri, '→', tgt.post_id);
// A reply is a post too: Berichten renders it the way de Krant renders a
// timeline row, so it needs the same media and the same quote/preview card.
{
const where = 'kind = ? AND post_id = ? AND actor_uri = ? AND object_uri = ?';
const key = ['reply', tgt.post_id, actorUri, o.id || ''];
const mj = mediaFromNote(o);
if (mj && mj !== '[]') { try { db.prepare(`UPDATE ap_interactions SET media_json = ? WHERE ${where}`).run(mj, ...key); } catch { /* ignore */ } }
resolveCard(o).then((c) => {
if (!c) return;
const col = c.column === 'quote_json' ? 'quote_json' : 'embed_json'; // never a value from the wire
try { db.prepare(`UPDATE ap_interactions SET ${col} = ? WHERE ${where}`).run(c.json, ...key); } catch { /* ignore */ }
}).catch(() => { /* best-effort */ });
}
{
// Private (followers/direct) replies push as a DM ping WITHOUT content
// (the push service should never carry private text, design decision);
// public replies carry a short snippet.
const ctx = pushPostCtx(tgt.post_id);
const vis = noteVisibility(o);
const priv = vis === 'direct' || vis === 'followers';
if (ctx) {
const L = pushLang(ctx.site);
const who = ai.name || ai.handle || i18nT(L, 'notif.someone');
if (priv) pushEvent(ctx.site, { type: 'dm', title: i18nT(L, 'push.n_dm_t'), body: i18nT(L, 'push.n_dm_b', { who }), url: `${pushPrefix(ctx.site)}/messages` });
else pushEvent(ctx.site, { type: 'reply', title: i18nT(L, 'push.n_reply_t', { title: ctx.title }), body: `${who}: ${HtmlSanitizerService.toPlainText(html).slice(0, 90)}`, url: ctx.url });
}
}
return 202;
}
// Home timeline (client): a top-level post from an account we follow.
if (actorUri && !isLocalActor && belongsInTimeline(o)) {
let subs = []; try { subs = db.prepare('SELECT slug, auto_boost FROM ap_following WHERE actor_uri = ?').all(actorUri); } catch { /* table may not exist yet */ }
if (subs.length) {
const ai = actorInfo(await resolveActor(actorUri), actorUri);
const html = 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);
const poll = parsePoll(o); // a Question (fediverse poll) → cache its options/counts
// "Feature" = show in the Cirkel (local only). We do NOT auto-Announce
// incoming posts to the fediverse — that flooded followers. Boosting to the
// fediverse is only ever a deliberate, manual per-post action (the 🔁 on
// the timeline).
for (const s of subs) {
tlStmts().ins.run(o.id, s.slug, actorUri, ai.name, ai.handle, ai.icon, ai.url, html, o.url || null, o.published || null, media, o.sensitive ? 1 : 0, o.summary || null);
// FEP-633c §2.2: register the ward hint on the stored object (no action yet).
if (Guardianship.objectHasGuardians(o)) { try { db.prepare('UPDATE ap_timeline SET has_guardians = 1 WHERE id = ? AND slug = ?').run(o.id, s.slug); } catch { /* ignore */ } }
// FEP-9098: keep the note's custom-emoji tags so the C2S inbox read can serve them.
{ const ej = extractEmojiTags(o.tag); if (ej) { try { db.prepare('UPDATE ap_timeline SET emoji_json = ? WHERE id = ? AND slug = ?').run(ej, o.id, s.slug); } catch { /* ignore */ } } }
storeAuthorEmoji(o.id, s.slug, ai); // custom-emoji display name for the byline
// FEP-e232 + FEP-044f: keep the note's object-link/quote tags for the same read.
{ const lj = extractLinkJson(o); if (lj) { try { db.prepare('UPDATE ap_timeline SET link_json = ? WHERE id = ? AND slug = ?').run(lj, o.id, s.slug); } catch { /* ignore */ } } }
if (poll) { try { db.prepare('UPDATE ap_timeline SET poll_json = ? WHERE id = ? AND slug = ?').run(JSON.stringify(poll), o.id, s.slug); } catch { /* ignore */ } }
}
// FEP-044f embedded quote card: resolve the quoted post out of band so
// the inbox response is not blocked on a remote fetch. Best-effort.
if (quoteHrefOf(o)) {
const slugs = subs.map((s) => s.slug);
resolveQuote(o).then((qj) => {
if (!qj) return;
for (const sl of slugs) { try { db.prepare('UPDATE ap_timeline SET quote_json = ? WHERE id = ? AND slug = ?').run(qj, o.id, sl); } catch { /* ignore */ } }
}).catch(() => { /* best-effort */ });
} else {
// No fediverse quote: try an EXTERNAL embed (oEmbed / known provider),
// thumbnail-only. Also out of band, and stored for everyone; the gate
// that decides who may SEE it is applied at serve time (§5.3-style
// gated feature, see the inbox read).
const slugs = subs.map((s) => s.slug);
resolveExternalEmbed(o.content).then((ej) => {
if (!ej) return;
for (const sl of slugs) { try { db.prepare('UPDATE ap_timeline SET embed_json = ? WHERE id = ? AND slug = ?').run(ej, o.id, sl); } catch { /* ignore */ } }
}).catch(() => { /* best-effort */ });
}
console.log('[AP] timeline +', actorUri, 'x' + subs.length);
}
}
// Mentioned in a post that is NOT a reply to our content (a reply to us already returned
// above): store a mention notification for each of our actors named in the Mention tags.
// Requires our own base prefix on the tag href — /ap/users/ on a REMOTE host is
// someone else's actor, not ours.
if (actorUri && !isLocalActor && o.id) {
const slugs = localMentionSlugs(o.tag, base);
if (slugs.length) {
const ai = actorInfo(await resolveActor(actorUri), actorUri);
const html = HtmlSanitizerService.sanitize(o.content || '');
// FEP-633c 5.2.1: a ward's call for help rides a direct mention; the
// flag is stored so the Guardian PWA's message centre can list it.
const help = Guardianship.isHelpRequest(o);
const wave = Guardianship.isWave(o);
const hasG = Guardianship.objectHasGuardians(o); // §2.2 hint, register-only
for (const slug of slugs) {
try {
const r = db.prepare(`INSERT OR IGNORE INTO ap_mentions (slug, object_uri, note_url, actor_uri, actor_name, actor_handle, actor_icon, actor_url, content, published, help_request, wave, has_guardians, emoji_json, actor_emoji_json, media_json, created_at)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)`)
.run(slug, o.id, safeUrl(o.url) || null, actorUri, ai.name, ai.handle, ai.icon, ai.url, html, o.published || null, help ? 1 : 0, wave ? 1 : 0, hasG ? 1 : 0,
extractEmojiTags(o.tag), emojiJsonOf(ai.emojis), mediaFromNote(o));
if (r.changes) {
// The quote / link-preview card resolves out of band (a remote
// fetch), exactly as it does for a timeline post, so the inbox
// answer is never blocked on it.
resolveCard(o).then((c) => {
if (!c) return;
const col = c.column === 'quote_json' ? 'quote_json' : 'embed_json'; // never a value from the wire
try { db.prepare(`UPDATE ap_mentions SET ${col} = ? WHERE slug = ? AND object_uri = ?`).run(c.json, slug, o.id); } catch { /* ignore */ }
}).catch(() => { /* best-effort */ });
console.log('[AP] mention', actorUri, '→', slug, help ? '(help request)' : '');
const vis = noteVisibility(o);
const priv = vis === 'direct' || vis === 'followers';
const L = pushLang(slug);
const who = ai.name || ai.handle || i18nT(L, 'notif.someone');
// Same privacy rule as replies: private mentions push without content.
// A help request pushes as its own alert type, aimed at the
// Guardian PWA's message centre.
if (help) pushEvent(slug, { type: 'help', title: i18nT(L, 'push.n_help_t'), body: i18nT(L, 'push.n_help_b', { who }), url: '/guardian' });
else if (priv) pushEvent(slug, { type: 'dm', title: i18nT(L, 'push.n_dm_t'), body: i18nT(L, 'push.n_dm_b', { who }), url: `${pushPrefix(slug)}/messages` });
else pushEvent(slug, { type: 'reply', title: i18nT(L, 'push.n_mention_t'), body: `${who}: ${HtmlSanitizerService.toPlainText(html).slice(0, 90)}`, url: `${pushPrefix(slug)}/messages` });
}
} catch { /* ignore */ }
}
}
}
return 202;
}
// A remote post we cached was edited upstream → refresh our cached copy. This is the
// push-based edit-sync that keeps the Cirkel/timeline fresh without polling (selfHeal
// does it on a version bump; this does it live). Scope to the SIGNING actor so B can't
// edit A's note (the signature gate guarantees claimedActor == the verified signer).
if (type === 'Update' && act.object && (act.object.type === 'Note' || act.object.type === 'Article' || act.object.type === 'Question')) {
const o = act.object;
if (o.id && claimedActor) {
const html = HtmlSanitizerService.sanitize(o.content || '');
const media = mediaFromNote(o);
try {
// Refresh url too (COALESCE keeps the old one if the Update omits it): a remote slug
// rename keeps the same AP id but changes the human url, so without this the cached
// post would keep linking to the old, now-dead URL.
const r = db.prepare('UPDATE ap_timeline SET content = ?, media_json = ?, nsfw = ?, cw = ?, url = COALESCE(?, url) WHERE id = ? AND author_uri = ?')
.run(html, media, o.sensitive ? 1 : 0, o.summary || null, o.url || null, o.id, claimedActor);
if (r.changes) console.log('[AP] timeline update', claimedActor, '→', o.id);
// A poll's Update carries the fresh vote counts / closed state. Refresh per-row so each
// site keeps its own `voted` state while the counts/closed update to the new totals.
const poll = parsePoll(o);
if (poll) {
const rows = db.prepare('SELECT rowid AS rid, poll_json FROM ap_timeline WHERE id = ? AND author_uri = ?').all(o.id, claimedActor);
const upd = db.prepare('UPDATE ap_timeline SET poll_json = ? WHERE rowid = ?');
for (const rw of rows) {
let voted = null; try { voted = rw.poll_json ? (JSON.parse(rw.poll_json).voted || null) : null; } catch { /* ignore */ }
upd.run(JSON.stringify({ ...poll, voted }), rw.rid);
}
}
} catch { /* ignore */ }
// If this note is a cached fediverse reply on one of our posts, refresh its text too.
try { db.prepare('UPDATE ap_interactions SET content = ? WHERE object_uri = ? AND actor_uri = ?').run(html, o.id, claimedActor); } catch { /* ignore */ }
}
return 202;
}
if (type === 'Like' || type === 'Announce') {
const tgt = act.object;
const objUrl = typeof tgt === 'string' ? tgt : (tgt && tgt.id);
const pid = postIdFromNoteUrl(objUrl, base);
if (pid && actorUri && !isLocalActor && localPostExists(pid)) {
// A boost/like of a non-public post is dropped, not stored: nobody
// outside the audience should even hold it (shaer-tqc hardening).
const vp = db.prepare('SELECT fan_only, ap_visibility FROM posts WHERE id = ?').get(pid);
if (vp && (vp.fan_only || vp.ap_visibility === 'direct' || vp.ap_visibility === 'friends')) {
console.log('[AP] dropped', type, 'on non-public post', pid);
return;
}
const ai = actorInfo(await resolveActor(actorUri), actorUri);
iStmts().ins.run(type.toLowerCase(), pid, '', actorUri, ai.name, ai.handle, ai.url, ai.icon, null, null, null, noteVisibility(act), null, emojiJsonOf(ai.emojis));
console.log('[AP]', type === 'Like' ? 'like' : 'boost', actorUri, '→', pid);
{
const ctx = pushPostCtx(pid);
if (ctx) {
const L = pushLang(ctx.site);
const who = ai.name || ai.handle || i18nT(L, 'notif.someone');
if (type === 'Like') pushEvent(ctx.site, { type: 'like', title: i18nT(L, 'push.n_like_t'), body: i18nT(L, 'push.n_like_b', { who, title: ctx.title }), url: ctx.url });
else pushEvent(ctx.site, { type: 'boost', title: i18nT(L, 'push.n_boost_t'), body: i18nT(L, 'push.n_boost_b', { who, title: ctx.title }), url: ctx.url });
}
}
} else if (type === 'Announce' && objUrl && actorUri && !isLocalActor) {
// A boost FROM an account we follow, of a REMOTE post → show it in the News feed.
// We only STORE it for display; we NEVER auto-Announce it onward (anti-feedback-loop:
// re-announcing an incoming Announce would cascade boosts across the network).
let subs = []; try { subs = db.prepare('SELECT slug FROM ap_following WHERE actor_uri = ?').all(actorUri); } catch { /* table may not exist */ }
if (subs.length) {
const bn = await fetchNoteAP(objUrl);
if (bn && bn !== 404 && (bn.type === 'Note' || bn.type === 'Article') && bn.id) {
const origUri = actorUriOf(bn.attributedTo);
// Block completeness: even if you follow the booster, drop a boost whose ORIGINAL
// author is blocked — otherwise a block is bypassed via someone else's boost.
if (origUri && isBlockedAny(origUri)) { console.log('[AP] timeline boost dropped (blocked origin)', origUri, 'via', actorUri); return 202; }
const oai = actorInfo(await resolveActor(origUri), origUri);
const html = HtmlSanitizerService.sanitize(bn.content || '');
const media = mediaFromNote(bn);
const booster = actorInfo(await resolveActor(actorUri), actorUri);
for (const s of subs) {
// published = now → the boost shows as fresh activity at the top (Mastodon shows
// reblogs at reblog-time, not the original's date). INSERT OR IGNORE: if we already
// have the note (e.g. we also follow the author), keep it and DON'T relabel it.
let inserted = false;
try { const r = tlStmts().ins.run(bn.id, s.slug, origUri || '', oai.name, oai.handle, oai.icon, oai.url, html, bn.url || null, new Date().toISOString(), media, bn.sensitive ? 1 : 0, bn.summary || null); inserted = r.changes > 0; } catch { /* ignore */ }
if (inserted) { try { db.prepare('UPDATE ap_timeline SET reblog_name = ?, reblog_handle = ?, reblog_icon = ?, reblog_emoji_json = ? WHERE slug = ? AND id = ?').run(booster.name, booster.handle, booster.icon, (booster.emojis && Object.keys(booster.emojis).length) ? JSON.stringify(booster.emojis) : null, s.slug, bn.id); } catch { /* ignore */ } }
storeAuthorEmoji(bn.id, s.slug, oai); // custom-emoji display name for the byline
// A boost carries the same renderable tags as a Create: capture the
// note's content emojis (FEP-9098) and object links / quote (FEP-e232/
// 044f) so boosted posts render like any other, not as raw shortcodes.
{ const ej = extractEmojiTags(bn.tag); if (ej) { try { db.prepare('UPDATE ap_timeline SET emoji_json = ? WHERE id = ? AND slug = ?').run(ej, bn.id, s.slug); } catch { /* ignore */ } } }
{ const lj = extractLinkJson(bn); if (lj) { try { db.prepare('UPDATE ap_timeline SET link_json = ? WHERE id = ? AND slug = ?').run(lj, bn.id, s.slug); } catch { /* ignore */ } } }
}
// FEP-044f: resolve the embedded quote card for a boosted post too
// (out of band, best-effort, so it does not block the inbox response).
if (quoteHrefOf(bn)) {
const slugs = subs.map((s) => s.slug);
resolveQuote(bn).then((qj) => {
if (!qj) return;
for (const sl of slugs) { try { db.prepare('UPDATE ap_timeline SET quote_json = ? WHERE id = ? AND slug = ?').run(qj, bn.id, sl); } catch { /* ignore */ } }
}).catch(() => { /* best-effort */ });
}
console.log('[AP] timeline boost +', actorUri, 'x' + subs.length);
}
}
}
return 202;
}
if (type === 'Delete') {
// A remote note was deleted upstream → drop it from replies AND the timeline.
// Scope to the SIGNING actor so actor B can't delete actor A's content (the
// signature gate guarantees claimedActor == the verified signer here).
const oid = typeof act.object === 'string' ? act.object : (act.object && act.object.id);
if (oid && claimedActor) {
try { db.prepare('DELETE FROM ap_interactions WHERE object_uri = ? AND actor_uri = ?').run(oid, claimedActor); } catch { /* ignore */ }
try { db.prepare('DELETE FROM ap_timeline WHERE id = ? AND author_uri = ?').run(oid, claimedActor); } catch { /* ignore */ }
// Also clear a boost/like YOU made of this now-deleted remote post (the interact-page
// ap_my_reactions state), so it can't stay stuck as "boosted" on a post that's gone.
// Guard: only when the deleter owns the note's domain (B mustn't clear your reactions
// to A's posts).
try {
let sameHost = false;
try { sameHost = new URL(oid).host === new URL(claimedActor).host; } catch { sameHost = false; }
if (sameHost) db.prepare('DELETE FROM ap_my_reactions WHERE target_uri = ?').run(oid);
} catch { /* ignore */ }
}
return 202;
}
// Accept/Reject of a Follow WE sent (client side).
if (type === 'Accept' && act.object) {
const fid = typeof act.object === 'string' ? act.object : (act.object && act.object.id);
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;
// Resolve inline @user@host mentions → link them in the note + collect their inboxes, so a
// mentioned person is notified even if they don't follow us (Mastodon-standard mention).
const mres = await resolveMentionsInText(base, post.content || '');
let post2 = mres.inboxes.length ? { ...post, content: mres.html } : post;
// FEP-044f: does this post quote a fediverse object? Resolve it once, here,
// and remember it on the post, so buildNote (sync, also used by the outbox)
// never has to fetch. The quoted author's inbox joins the delivery set: that
// IS the notification.
const quoteInboxes = [];
if (post2.quote_uri === undefined || post2.quote_uri === null) {
const q = await resolveOwnQuote(post2.content || '');
if (q) {
try { db.prepare('UPDATE posts SET quote_uri = ?, quote_actor = ? WHERE id = ?').run(q.uri, q.actor || null, post.id); } catch { /* ignore */ }
post2 = { ...post2, quote_uri: q.uri, quote_actor: q.actor || null };
}
}
if (post2.quote_actor) {
const a = await fetchActor(post2.quote_actor).catch(() => null);
const inbox = a && ((a.endpoints && a.endpoints.sharedInbox) || a.inbox);
if (inbox) quoteInboxes.push(inbox);
}
const followers = fStmts().list.all(site.slug);
const inboxes = [...new Set([...followers.map((f) => f.shared_inbox || f.inbox), ...mres.inboxes, ...quoteInboxes].filter(Boolean))];
if (!inboxes.length) return; // no followers, no one mentioned, no one quoted
const keys = getOrCreateKeys(site.slug);
const keyId = `${actorId(base, site.slug)}#main-key`;
const create = buildCreate(base, site, post2);
for (const inbox of inboxes) deliverWithRetry(site.slug, inbox, create, keyId, keys.private_pem);
}
// On a new Follow, send that follower our most recent posts as Create so their
// timeline shows our history (Mastodon does not backfill on follow). Oldest-first
// so they sort into the follower's timeline at their original dates.
async function backfillNewFollower(base, slug, inbox) {
if (!base || !slug || !inbox) return;
const site = db.prepare('SELECT * FROM sites WHERE slug = ?').get(slug);
if (!site) return;
const recent = db.prepare(
`SELECT id, slug, title, content, cover_image_url, cover_video_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': AP_CONTEXT,
id: `${nid}#delete-${Date.now()}-${rid()}`,
type: 'Delete',
actor: me,
to: [PUBLIC],
object: { id: nid, type: 'Tombstone' },
};
for (const inbox of inboxes) deliverWithRetry(site.slug, inbox, del, `${me}#main-key`, keys.private_pem);
}
// Tell followers an already-published post changed (Update + edited Note) so
// Mastodon refreshes the cached copy (e.g. after fixing content).
export async function deliverUpdate(site, post) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
if (!base || !site || !site.slug || !post || !post.id) return;
const mres = await resolveMentionsInText(base, post.content || ''); // link mentions + collect inboxes
const post2 = mres.inboxes.length ? { ...post, content: mres.html } : post;
const followers = fStmts().list.all(site.slug);
const inboxes = [...new Set([...followers.map((f) => f.shared_inbox || f.inbox), ...mres.inboxes].filter(Boolean))];
if (!inboxes.length) return;
const keys = getOrCreateKeys(site.slug);
const me = actorId(base, site.slug);
const note = buildNote(base, site, post2);
note.updated = new Date().toISOString();
const update = {
'@context': AP_CONTEXT,
id: `${noteId(base, post.id)}#update-${Date.now()}-${rid()}`,
type: 'Update', actor: me, to: [PUBLIC], cc: note.cc,
object: note,
};
for (const inbox of inboxes) deliverWithRetry(site.slug, inbox, update, `${me}#main-key`, keys.private_pem);
}
// Tell followers the ACTOR changed (Update + Person) so Mastodon re-processes the
// account AND re-fetches the featured (pinned) collection — there is no standard
// "featured changed" activity, so this is how a pin/unpin propagates promptly.
export async function deliverActorUpdate(site) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
if (!base || !site || !site.slug) return;
const followers = fStmts().list.all(site.slug);
if (!followers.length) return;
const inboxes = [...new Set(followers.map((f) => f.shared_inbox || f.inbox).filter(Boolean))];
const keys = getOrCreateKeys(site.slug);
const me = actorId(base, site.slug);
const update = {
'@context': AP_CONTEXT,
id: `${me}#update-${Date.now()}-${rid()}`,
type: 'Update', actor: me, to: [PUBLIC], cc: [`${me}/followers`],
object: buildActor(base, site),
};
for (const inbox of inboxes) deliverWithRetry(site.slug, inbox, update, `${me}#main-key`, keys.private_pem);
}
// Reliably set the pinned order on followers' instances via Add/Remove activities
// (how Mastodon itself federates pins) — pushed to the inbox + processed immediately,
// unlike the featured COLLECTION which Mastodon caches with sticky StatusPins.
// Mastodon's Add skips an already-pinned status, so we REMOVE every pin first, wait,
// then ADD in rank-DESCENDING order (rank 1 added LAST → newest StatusPin → shown first,
// because Mastodon displays pins newest-first). `alsoRemove` = ids to unpin too.
// Serialize pin-resyncs per site: two concurrent /save calls would otherwise interleave
// their Remove -> wait -> Add sequences and scramble the StatusPin order on Mastodon. A
// resync already in flight for a site coalesces later requests into ONE rerun after it
// finishes (accumulating their extra unpins), so rapid saves don't pile up N full resyncs.
const _pinResync = new Map(); // slug -> { promise, pending, pendingRemove:Set, site }
export function resyncFeaturedPins(site, alsoRemove = []) {
if (!site || !site.slug) return Promise.resolve();
const slug = site.slug;
const running = _pinResync.get(slug);
if (running) {
running.pending = true;
running.site = site; // use the latest site object on the rerun
for (const id of alsoRemove) running.pendingRemove.add(id);
return running.promise;
}
const state = { promise: null, pending: false, pendingRemove: new Set(), site };
state.promise = (async () => {
let extra = alsoRemove;
for (;;) {
try { await doResyncFeaturedPins(state.site, extra); }
catch (e) { console.warn('[AP] pin resync failed:', e.message); }
if (!state.pending) break;
state.pending = false;
extra = [...state.pendingRemove];
state.pendingRemove = new Set();
}
_pinResync.delete(slug);
})();
_pinResync.set(slug, state);
return state.promise;
}
// The actual resync work — do NOT call directly; go through resyncFeaturedPins() above so
// it stays serialized per site.
async function doResyncFeaturedPins(site, alsoRemove = []) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
if (!base || !site || !site.slug) return;
const followers = fStmts().list.all(site.slug);
if (!followers.length) return;
const inboxes = [...new Set(followers.map((f) => f.shared_inbox || f.inbox).filter(Boolean))];
const keys = getOrCreateKeys(site.slug);
const me = actorId(base, site.slug);
const keyId = `${me}#main-key`;
const featured = `${me}/featured`;
const note = (id) => noteId(base, id);
const pinned = db.prepare(
`SELECT id FROM posts WHERE site_id = ? AND status = 'published' AND (fan_only IS NULL OR fan_only = 0)
AND pinned IS NOT NULL AND pinned > 0
ORDER BY pinned DESC, COALESCE(published_at, created_at) ASC LIMIT 20`
).all(site.id);
const removeIds = [...new Set([...pinned.map((p) => p.id), ...alsoRemove])];
// 1. Remove every current pin so Mastodon can recreate them in order.
for (const id of removeIds) {
const rm = { '@context': AP_CONTEXT, id: `${me}#rm-${id}-${Date.now()}-${rid()}`, type: 'Remove', actor: me, object: note(id), target: featured, to: [PUBLIC] };
for (const inbox of inboxes) deliver(inbox, rm, keyId, keys.private_pem).catch(() => { /* best-effort */ });
}
if (!pinned.length) { console.log('[AP] unpinned all featured for', site.slug); return; }
await new Promise((r) => setTimeout(r, 5000)); // let the Removes land first
// 2. Add in rank-DESC order, gaps so each StatusPin gets an increasing created_at.
for (const p of pinned) {
const add = { '@context': AP_CONTEXT, id: `${me}#add-${p.id}-${Date.now()}-${rid()}`, type: 'Add', actor: me, object: note(p.id), target: featured, to: [PUBLIC], cc: [`${me}/followers`] };
for (const inbox of inboxes) deliver(inbox, add, keyId, keys.private_pem).catch(() => { /* best-effort */ });
await new Promise((r) => setTimeout(r, 2000));
}
console.log('[AP] resynced', pinned.length, 'featured pins for', site.slug);
}
// ── outbound replies (Klonkt → fediverse) ─────────────────────────
const escHtml = (s) => String(s || '').replace(/[<>&]/g, (c) => ({ '<': '<', '>': '>', '&': '&' }[c]));
const toISO = (v) => { if (!v) return new Date().toISOString(); const s = String(v); const d = new Date(/[TZ]/.test(s) ? s : s.replace(' ', 'T') + 'Z'); return isNaN(d) ? new Date().toISOString() : d.toISOString(); };
// Build one of OUR outbound reply Notes from an ap_outbox row.
// Turn #hashtags in reply text into Mastodon-style hashtag links (clickable + federated).
function linkHashtags(base, html) {
// Prefix: start / whitespace / '>' / opening bracket — "(#tag" is a tag too. NO quote
// chars in this class: a quote precedes attribute values (alt="#…"), which must not match.
return String(html || '').replace(/(^|[\s>([{])#([\p{L}\p{M}\p{N}_]+)/gu, (m, pre, tag) =>
`${pre}#${tag}`);
}
// Auto-link bare http(s) URLs in already-safe HTML (federated copies). Splits on existing
// … so a linked URL is never wrapped twice; requires start/whitespace/'>' before the
// URL so attribute values (src="https://…") never match. Trailing sentence punctuation stays
// outside the link (Mastodon-style).
function linkUrls(html) {
const parts = String(html || '').split(/(]*>[\s\S]*?<\/a>)/gi);
for (let i = 0; i < parts.length; i++) {
if (/^([{])(https?:\/\/[^\s<]+?)([.,;:!?)\]»]*)(?=$|[\s<])/g,
(m, pre, url, trail) => `${pre}${url}${trail}`);
}
return parts.join('');
}
// Linkify inline #hashtags and bare URLs in BODY html for on-site DISPLAY, using the
// EXACT same rules as the federated copy (linkHashtags/linkUrls), so the website and the
// Mastodon copy agree instead of the website showing raw text. Idempotent: existing
// … (editor links, embeds, shortcode buttons) are split out and left untouched, so
// nothing is double-wrapped. Pass base='' → root-relative /tag/ links.
export function linkifyBody(base, html) {
const withTags = String(html || '')
.split(/(]*>[\s\S]*?<\/a>)/gi)
.map((seg) => (/^]*>#([\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();
// Prefix also allows opening brackets — "(@user@host + me)" is a mention too (real-world
// miss: a bracketed mention federated as plain text and its target was never notified).
const re = /(^|[\s>([{])@([\p{L}\p{M}\p{N}_.-]+@[\p{L}\p{M}\p{N}.-]+)/gu;
let m;
while ((m = re.exec(html || ''))) handles.add(m[2]);
let out = String(html || '');
for (const h of handles) {
let actorUri = null;
try { actorUri = await webfingerResolve('@' + h); } catch { actorUri = null; }
if (!actorUri) continue;
const actor = await fetchActor(actorUri).catch(() => null);
const inbox = actor && ((actor.endpoints && actor.endpoints.sharedInbox) || actor.inbox);
if (inbox) inboxes.push(inbox);
const profileUrl = actorInfo(actor, actorUri).url || actorUri; // human profile page → the link href
const esc = h.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
out = out.replace(new RegExp('(^|[\\s>([{])@' + esc + '(?![\\p{L}\\p{M}\\p{N}_.-])', 'gu'),
(full, pre) => `${pre}@${h}`);
}
return { html: out, inboxes };
}
export function buildReplyNote(base, site, row) {
// Thin delegate: replies are built by buildNote (the single Note entry point) in reply mode.
return buildNote(base, site, row, { isReply: true });
}
// Resolve one of our outbound reply Notes by id (for /ap/notes/:id fallback).
export function getOutboxNote(base, id) {
const row = iStmts().getO.get(id);
if (!row) return null;
const site = db.prepare('SELECT * FROM sites WHERE slug = ?').get(row.site_slug);
if (!site) return null;
return buildReplyNote(base, site, row);
}
// ── ActivityPub Client-to-Server: ingest an activity POSTed to the outbox ──
// The C2S counterpart of handleInbox: a native/web client (Shaer) posts an
// activity here and we translate it onto the SAME delivery machinery the web UI
// uses (deliverReply / sendInteraction / followActor / deliverCreate). Returns
// { status, id?, url?, error? }. Auth + site-ownership are checked by the route.
const c2sIdOf = (x) => (typeof x === 'string' ? x : (x && (x.id || x.href))) || null;
export async function ingestOutboxActivity(site, user, activity) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
if (!base || !site || !activity || typeof activity !== 'object') return { status: 400, error: 'invalid_activity' };
// AP §6: a client MAY POST a bare object; the server wraps it in a Create.
let type = activity.type;
let object = activity.object;
if (type === 'Note' || type === 'Article') { object = activity; type = 'Create'; }
if (Array.isArray(type)) type = type.find((t) => typeof t === 'string');
// FEP-633c: the adoption handshake (Offer/Accept/Reject on a guardianship
// Relationship) belongs to the guardianship module; anything else falls
// through to the switch below.
if (type === 'Offer' || type === 'Accept' || type === 'Reject') {
const g = await Guardianship.handleGuardianshipOutbox(site, activity).catch(() => null);
if (g) return g;
}
try {
switch (type) {
case 'Create': {
if (!object || typeof object !== 'object') return { status: 400, error: 'missing_object' };
// Client sends `source` (plain/markdown) + `content` (HTML). deliverReply
// re-escapes, so it needs plain text; a top-level post keeps sanitized HTML.
const plain = (object.source && object.source.content) || HtmlSanitizerService.toPlainText(object.content || '');
if (!plain.trim() && !object.content) return { status: 400, error: 'empty_note' };
// Direct (private mention, shaer-tqc): NOT a post. Delivered over the
// outbox machinery to the addressed inboxes only; shows under Messages.
if (c2sVisibility(object) === 'direct') {
const arr = (v) => (Array.isArray(v) ? v : (v ? [v] : [])).filter((x) => typeof x === 'string');
const recipients = [...new Set([...arr(object.to), ...arr(object.cc)])]
.filter((u) => /^https?:\/\//i.test(u) && !/\/followers\/?$/.test(u) && u !== PUBLIC);
if (!recipients.length) return { status: 400, error: 'no_recipients' };
// AS2 attachments (e.g. the help-buoy capture, uploaded via
// uploadMedia): normalize our own absolute /media/ URLs to relative
// so the deliverReply-style validation applies unchanged.
const atts = (Array.isArray(object.attachment) ? object.attachment : [])
.map((a) => a && typeof a === 'object' ? {
url: String(a.url || '').replace(new RegExp('^' + base.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')), ''),
mediaType: String(a.mediaType || ''),
name: String(a.name || '').slice(0, 120),
} : null)
.filter(Boolean);
const help = object['shaer:helpRequest'] === true || object.helpRequest === true;
const r = await deliverDirectNote(site, { recipients, text: plain, language: object.language || null, inReplyTo: typeof object.inReplyTo === 'string' ? object.inReplyTo : null, attachments: atts, helpRequest: help });
if (!r || !r.id) return { status: 502, error: 'direct_failed' };
return { status: 201, id: r.id, url: `${base}/ap/notes/${r.id}` };
}
if (object.inReplyTo) {
const parent = await resolveRemoteNote(c2sIdOf(object.inReplyTo)).catch(() => null);
if (!parent) return { status: 502, error: 'cannot_resolve_inReplyTo' };
// Honour the client's visibility for the reply: 'friends' (followers-
// only, the Shaer detail-view Reply) drops Public; anything else stays
// quiet-public. 'direct' was already handled above.
const r = await deliverReply(site, { postId: parent.localPostId || '', postSlug: null, parent, text: plain, visibility: c2sVisibility(object) });
if (!r || !r.id) return { status: 502, error: 'reply_failed' };
return { status: 201, id: r.id, url: `${base}/ap/notes/${r.id}` };
}
return await c2sCreatePost(base, site, user, object);
}
case 'Like':
case 'Announce': {
const targetUri = c2sIdOf(object);
if (!targetUri) return { status: 400, error: 'missing_object' };
// A non-public local note cannot be boosted or liked into the open
// (shaer-tqc hardening; the Mastodon 422 equivalent).
const localPid = postIdFromNoteUrl(targetUri, base);
if (localPid) {
const p = db.prepare('SELECT fan_only, ap_visibility FROM posts WHERE id = ?').get(localPid);
if (p && (p.fan_only || p.ap_visibility === 'direct' || p.ap_visibility === 'friends')) {
return { status: 403, error: 'not_public' };
}
}
const note = await resolveRemoteNote(targetUri).catch(() => null);
const objUri = (note && note.object_uri) || targetUri;
const authorUri = note && note.actor_uri;
const kind = type === 'Announce' ? 'boost' : 'like';
await sendInteraction(site, kind, objUri, authorUri);
setMyReaction(site.slug, targetUri, kind, true);
if (type === 'Announce' && note) { try { upsertBoostedNote(site.slug, note); } catch { /* non-fatal */ } }
return { status: 202, url: objUri };
}
case 'Follow': {
const actorUri = c2sIdOf(object);
if (!actorUri) return { status: 400, error: 'missing_object' };
await followActor(site, actorUri);
return { status: 202, url: actorUri };
}
// Shaer "in Orbit" = a real Block (FEP-c648 client side): lands in
// ap_blocks, shows in the Block tab, and purges the actor's cached
// content. Client-side filtering becomes a cache of this state.
case 'Block': {
const targetUri = c2sIdOf(object);
if (!targetUri) return { status: 400, error: 'missing_object' };
const r = await blockTarget(site, targetUri);
if (r && r.error) return { status: 400, error: r.error };
return { status: 202, url: targetUri };
}
case 'Undo': {
const inner = object && typeof object === 'object' ? object : null;
let innerType = inner && inner.type;
if (Array.isArray(innerType)) innerType = innerType.find((t) => typeof t === 'string');
const innerTarget = c2sIdOf(inner && inner.object);
if (innerType === 'Follow') { await unfollowActor(site, innerTarget); return { status: 202, url: innerTarget }; }
if (innerType === 'Block') {
if (!innerTarget) return { status: 400, error: 'missing_object' };
unblock(site, innerTarget); // release from Orbit
return { status: 202, url: innerTarget };
}
if (innerType === 'Like' || innerType === 'Announce') {
const kind = innerType === 'Announce' ? 'unboost' : 'unlike';
const note = await resolveRemoteNote(innerTarget).catch(() => null);
const objUri = (note && note.object_uri) || innerTarget;
await sendInteraction(site, kind, objUri, note && note.actor_uri);
setMyReaction(site.slug, innerTarget, innerType === 'Announce' ? 'boost' : 'like', false);
if (innerType === 'Announce') { try { unmarkBoosted(site.slug, objUri); } catch { /* non-fatal */ } }
return { status: 202, url: objUri };
}
return { status: 400, error: 'unsupported_undo' };
}
// Delete/Update of arbitrary objects need the post-edit pipeline; tracked
// separately (klonkt-demo-c2s-del). Reject clearly rather than half-doing it.
default:
return { status: 400, error: 'unsupported_type', detail: String(type || 'none') };
}
} catch (e) {
console.warn('[AP] C2S ingest failed:', e && e.message);
return { status: 500, error: 'ingest_error' };
}
}
// Create a top-level microblog post from a C2S Note and federate it. Minimal
// sibling of the /posts/create route: sanitized HTML content, no title/cover.
async function c2sCreatePost(base, site, user, object) {
const html = HtmlSanitizerService.sanitize(object.content || (object.source && object.source.content) || '');
if (!html.trim()) return { status: 400, error: 'empty_note' };
const postId = crypto.randomUUID();
const slug = 'n-' + postId.slice(0, 8);
const now = new Date().toISOString();
// Visibility from the note's addressing (shaer-60b): Public in `to` = loud
// public, Public in `cc` = quiet public (unlisted), followers-only = friends
// (rides the existing fan_only pipeline: followers-only AP delivery + web
// gating), neither = participants-only (kept local until mention addressing
// lands; still followers-gated on the web).
const vis = c2sVisibility(object);
const fanOnly = (vis === 'friends' || vis === 'direct') ? 1 : 0;
db.prepare(`INSERT INTO posts (id, site_id, slug, author_id, title, content, excerpt, status, type, language, fan_only, ap_visibility, created_at, updated_at, published_at)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`)
.run(postId, site.id, slug, user.id, '', html, '', 'published', 'post', object.language || 'nl', fanOnly, vis, now, now, now);
try { db.prepare('UPDATE posts SET content_rendered = ? WHERE id = ?').run(bakePostContent(html), postId); } catch { /* render fallback covers it */ }
bakePostContentWithMentions(html).then((h) => { try { db.prepare('UPDATE posts SET content_rendered = ? WHERE id = ?').run(h, postId); } catch { /* keep sync bake */ } }).catch(() => {});
try { db.prepare('INSERT INTO posts_fts(content, title, author, post_id) VALUES (?,?,?,?)').run(HtmlSanitizerService.toPlainText(html), '', user.username || '', postId); } catch { /* FTS non-fatal */ }
if (vis !== 'direct') {
deliverCreate(site, { id: postId, slug, title: '', content: html, published_at: now, created_at: now, fan_only: fanOnly, ap_visibility: vis }).catch(() => { /* best-effort */ });
}
return { status: 201, id: postId, url: `${base}/ap/notes/${postId}` };
}
// The direct-note leg (ward call-for-help) lives in the guardianship module
// (src/services/guardianship/delivery.js); wired with our AP helpers at the
// bottom of this file. Re-exported so every existing caller keeps working.
export const c2sVisibility = Guardianship.c2sVisibility;
export const deliverDirectNote = Guardianship.deliverDirectNote;
// 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, html, language, attachments, mentions, visibility }) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
// Rich replies: `html` is the reply editor's HTML (sanitized here); `text` is
// the plain-text fallback (no-JS path, C2S `source`). Either may carry the reply.
const richClean = html ? HtmlSanitizerService.sanitize(String(html)) : '';
const rich = richClean && HtmlSanitizerService.toPlainText(richClean).trim() ? richClean : '';
// Attachments: only OUR OWN uploads (/media/... paths, no remote URLs — the
// upload route is the sole producer), image/audio/video only, max 4.
const media = (Array.isArray(attachments) ? attachments : [])
.filter((a) => a && typeof a.url === 'string' && /^\/media\/[\w./-]+$/.test(a.url)
&& /^(image|audio|video)\//.test(String(a.mediaType || '')))
.slice(0, 4)
.map((a) => ({ url: a.url, mediaType: String(a.mediaType), name: String(a.name || '').slice(0, 120) }));
// A media-only reply (no text) is a valid reply.
if (!base || !site || !site.slug || !parent || (!String(text || '').trim() && !rich && !media.length)) return null;
const me = actorId(base, site.slug);
// u02, the mentions bar: `mentions` undefined = legacy behavior (mention the
// parent author). An ARRAY (possibly empty) = the kept conversation partners
// exactly as the bar shows them; the mention prefix, the Mention tags (via
// mentionTags over the content) and the delivery targets all follow it.
const kept = Array.isArray(mentions)
? mentions
.filter((m) => m && typeof m.uri === 'string' && /^https?:\/\//i.test(m.uri))
.slice(0, 8)
.map((m) => ({
uri: m.uri,
url: (typeof m.url === 'string' && /^https?:\/\//i.test(m.url)) ? m.url : m.uri,
handle: String(m.handle || deriveHandle(m.uri)).slice(0, 120),
}))
: null;
const mentionAnchor = (uri, url, h) => {
const disp = h && h[0] === '@' ? h : '@' + (h || '');
return `${escHtml(disp)} `;
};
const handle = parent.actor_handle || deriveHandle(parent.actor_uri);
const mention = kept
? kept.map((k) => mentionAnchor(k.uri, k.url, k.handle)).join('')
: (parent.actor_uri ? mentionAnchor(parent.actor_uri, parent.actor_url, handle) : '');
// Who the stored reply is "to": the parent when kept, else the first kept chip.
const parentKept = !kept || kept.some((k) => k.uri === parent.actor_uri);
const toActorUri = parentKept ? (parent.actor_uri || null) : (kept[0] ? kept[0].uri : null);
const toHandle = parentKept ? handle : (kept[0] ? kept[0].handle : null);
let content;
let mres;
if (rich) {
// Same enrichment pipeline as the plain path (mentions/hashtags/URLs), on
// sanitized editor HTML. The parent mention goes inline into the first
// paragraph (Mastodon convention), or becomes its own leading one.
mres = await resolveMentionsInText(base, rich);
const processed = linkUrls(linkHashtags(base, mres.html));
if (processed.startsWith('')) {
content = processed.replace('
', `
${mention}`); // inline in the first paragraph
} else if (/^<(blockquote|ul|ol|pre|h[1-6]|div|hr)\b/i.test(processed)) {
content = `
${mention}
${processed}`; // block content: own leading paragraph
} else {
content = `${mention}${processed}
`; // bare inline text: one paragraph together
}
} else {
const body = escHtml(String(text).trim()).replace(/\r?\n/g, '
');
mres = await resolveMentionsInText(base, body); // link inline @mentions + collect their inboxes
content = `${mention}${linkUrls(linkHashtags(base, mres.html))}
`;
}
const replyLang = /^[a-z]{2,3}(-[A-Za-z0-9-]+)?$/.test(String(language || '')) ? language : null;
// Dedup: skip if the exact same reply was already sent (double-submit guard).
// Attachments count toward "the same": two media-only replies share content.
const mediaJson = media.length ? JSON.stringify(media) : null;
const dup = db.prepare('SELECT 1 FROM ap_outbox WHERE site_slug = ? AND IFNULL(in_reply_to, \'\') = ? AND content = ? AND IFNULL(attachments, \'\') = IFNULL(?, \'\') LIMIT 1')
.get(site.slug, parent.object_uri || '', content, mediaJson);
if (dup) { console.log('[AP] outreply skipped (duplicate)'); return { duplicate: true, delivered: 0 }; }
const id = crypto.randomUUID();
iStmts().insO.run(id, site.slug, postId, postSlug || null, parent.object_uri || null, toActorUri, toHandle, content, replyLang, mediaJson);
// Followers-only reply (shaer detail-view): mark the row so buildNote drops
// Public from cc. Default (undefined/'public'/'quiet') stays quiet-public.
if (visibility === 'friends') { try { db.prepare('UPDATE ap_outbox SET visibility = ? WHERE id = ?').run('friends', id); } catch { /* ignore */ } }
const row = iStmts().getO.get(id);
const note = buildReplyNote(base, site, row);
const create = {
'@context': AP_CONTEXT,
id: note.id + '#create', type: 'Create', actor: me,
published: note.published, to: note.to, cc: note.cc, object: note,
};
const keys = getOrCreateKeys(site.slug);
const keyId = `${me}#main-key`;
const inboxes = new Set();
// Everyone the mentions bar kept gets pinged; legacy path = the parent only.
const mentionTargets = kept ? kept.map((k) => k.uri) : (parent.actor_uri ? [parent.actor_uri] : []);
for (const uri of mentionTargets) {
const a = await fetchActor(uri).catch(() => null);
if (a) inboxes.add((a.endpoints && a.endpoints.sharedInbox) || a.inbox);
}
if (parent.threadInbox) inboxes.add(parent.threadInbox); // back-compat (single)
(parent.threadInboxes || []).forEach((i) => inboxes.add(i)); // whole ancestor chain
for (const f of fStmts().list.all(site.slug)) inboxes.add(f.shared_inbox || f.inbox);
mres.inboxes.forEach((i) => inboxes.add(i)); // people @mentioned inline in the reply
inboxes.delete(`${me}/inbox`); // never deliver to ourselves (already in ap_outbox)
inboxes.delete(`${base}/ap/inbox`); // (our own shared inbox) → avoids a self-duplicate
let delivered = 0;
for (const inbox of [...inboxes].filter(Boolean)) {
let ok = false;
try { const st = await deliver(inbox, create, keyId, keys.private_pem); ok = st >= 200 && st < 300; } catch { ok = false; }
if (ok) delivered++;
else enqueueDelivery(site.slug, inbox, create); // durable: retry a briefly-offline recipient (was silently dropped)
}
console.log('[AP] outreply', site.slug, '→', parent.actor_uri, 'delivered', delivered);
return { id, content, delivered };
}
// attributedTo may be a string, an object {id}, or an ARRAY — e.g. a PeerTube Video is
// attributed to [Person (account), Group (channel)]. Pick a usable actor URI (prefer Person).
function actorUriOf(att) {
if (!att) return null;
if (typeof att === 'string') return att;
if (Array.isArray(att)) {
const person = att.find((a) => a && typeof a === 'object' && a.type === 'Person' && a.id);
if (person) return person.id;
for (const a of att) { if (typeof a === 'string') return a; if (a && a.id) return a.id; }
return null;
}
return att.id || null;
}
// Resolve a remote post URL (any fediverse/Klonkt post) into a reply target.
// Returns a parent-shaped object usable by deliverReply(), or null.
export async function resolveRemoteNote(url) {
if (!/^https?:\/\//i.test(String(url || ''))) return null;
const note = await fetchActor(url).catch(() => null); // AP GET (content-negotiates)
if (!note || !note.id) return null;
const att = note.attributedTo;
const actorUri = actorUriOf(att);
if (!actorUri) return null;
const actor = await fetchActor(actorUri).catch(() => null);
const ai = actorInfo(actor, actorUri);
// Is what we're replying to a post (or a comment) on one of OUR posts? If so,
// link our reply to that local post so it shows nested in the post thread.
const localTgt = findThreadTarget(note.id, (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''));
// Walk the WHOLE reply chain upward (comment → parent comment → … → root post)
// and collect every ancestor author's inbox, so each participant's server —
// including the original post's author — receives + threads our reply.
const threadInboxes = [];
const seenInbox = new Set();
let cursor = note.inReplyTo, guard = 0;
while (cursor && guard++ < 6) {
const url = typeof cursor === 'string' ? cursor : (cursor && cursor.id);
if (!url) break;
const pn = await fetchActor(url).catch(() => null);
if (!pn) break;
const pa = actorUriOf(pn.attributedTo);
if (pa && pa !== actorUri) {
const paDoc = await fetchActor(pa).catch(() => null);
const inbox = paDoc && ((paDoc.endpoints && paDoc.endpoints.sharedInbox) || paDoc.inbox);
if (inbox && !seenInbox.has(inbox)) { seenInbox.add(inbox); threadInboxes.push(inbox); }
}
cursor = pn.inReplyTo; // climb to the next ancestor
}
// For non-Note objects (PeerTube Video, Article, …) the meaningful label is `name` (the
// title); prepend it so the reply page shows what you're replying to (sanitize cleans it).
let rawHtml = String(note.content || '').replace(/\[\[(track|album|playlist):[^\]]+\]\]/gi, '');
if (note.name && note.type && note.type !== 'Note') rawHtml = `${note.name}
` + rawHtml;
const images = (Array.isArray(note.attachment) ? note.attachment : [])
.filter((a) => a && a.url && (!a.mediaType || /^image\//i.test(a.mediaType)))
.map((a) => safeUrl(a.url)).filter(Boolean);
// A Klonkt hosted-audio post strips its cover from `attachment` (so Mastodon
// shows the player card, not a loose image) and puts it in `image` instead.
// Same fallback as mediaFromNote() so a boosted music post keeps its cover.
if (!images.length && note.image) {
const im = Array.isArray(note.image) ? note.image[0] : note.image;
const iu = safeUrl(typeof im === 'string' ? im : (im && im.url));
if (iu) images.push(iu);
}
return {
object_uri: safeUrl(note.id) || note.id,
actor_uri: actorUri,
actor_url: ai.url,
actor_handle: ai.handle,
actor_name: ai.name,
actor_icon: ai.icon,
url: note.url || url,
content: HtmlSanitizerService.sanitize(rawHtml), // full, sanitized
sensitive: !!note.sensitive, // remote CW → blur in the Cirkel
cw: note.summary || '',
images,
// Full typed media (incl. video/mp4) for the timeline cache. `images` above is
// image-only for the interact page preview; a boosted video-only post (Loops)
// lost its media entirely because upsertBoostedNote only saw `images`.
media: mediaFromNote(note),
threadInboxes, // every ancestor author's inbox
localPostId: localTgt ? localTgt.post_id : '', // our post this belongs to (if any)
poll: parsePoll(note), // a Question → its options/counts (else null)
preview: HtmlSanitizerService.toPlainText(note.content || '').slice(0, 240),
};
}
// List a site's own outbound fediverse replies (for the manage/delete view).
// The plain editable text of a stored reply (unwrap links → their text,
→ 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, language, 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': AP_CONTEXT, 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 { const st = await deliver(inbox, del, `${me}#main-key`, keys.private_pem); if (st >= 200 && st < 300) continue; } catch { /* queue below */ }
enqueueDelivery(site.slug, inbox, del); // durable: a failed comment-delete now retries (was silently dropped)
}
}
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, opts = {}) {
const row = iStmts().getO.get(outboxId);
if (!row || row.site_slug !== site.slug) return false;
const text = String(newText || '').trim();
// Rich edit: same sanitize + enrichment pipeline as deliverReply.
const richClean = opts.html ? HtmlSanitizerService.sanitize(String(opts.html)) : '';
const rich = richClean && HtmlSanitizerService.toPlainText(richClean).trim() ? richClean : '';
if (!text && !rich) 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 || '');
// An edit must not drop co-mentions (u02): reuse the OLD content's leading
// mention anchors (the bar's kept list at send time) when present; only fall
// back to rebuilding the single to_actor mention for legacy rows.
const oldPrefix = (String(row.content || '')
.match(/^\s*(?:]*>)?\s*((?:]*class="u-url mention"[^>]*>\s*@[^<]+<\/a>[\s ]*)+)/i) || [])[1] || '';
const mention = oldPrefix || (row.to_actor
? `${escHtml(toHandle)} ` : '');
let content;
let mres;
if (rich) {
mres = await resolveMentionsInText(base, rich);
const processed = linkUrls(linkHashtags(base, mres.html));
if (processed.startsWith('
')) content = processed.replace('
', `
${mention}`);
else if (/^<(blockquote|ul|ol|pre|h[1-6]|div|hr)\b/i.test(processed)) content = `
${mention}
${processed}`;
else content = `${mention}${processed}
`;
} else {
mres = await resolveMentionsInText(base, escHtml(text).replace(/\r?\n/g, '
'));
content = `${mention}${linkUrls(linkHashtags(base, mres.html))}
`;
}
// Language may be updated with the edit; attachments always survive untouched.
const newLang = /^[a-z]{2,3}(-[A-Za-z0-9-]+)?$/.test(String(opts.language || '')) ? opts.language : null;
db.prepare('UPDATE ap_outbox SET content = ?, language = COALESCE(?, language) WHERE id = ?').run(content, newLang, outboxId);
const note = buildReplyNote(base, site, iStmts().getO.get(outboxId));
note.updated = new Date().toISOString();
const update = {
'@context': AP_CONTEXT,
id: `${note.id}#update-${Date.now()}-${rid()}`, type: 'Update', actor: me,
published: note.published, updated: note.updated, to: note.to, cc: note.cc, object: note,
};
const keys = getOrCreateKeys(site.slug);
const inboxes = new Set();
if (toActor) inboxes.add((toActor.endpoints && toActor.endpoints.sharedInbox) || toActor.inbox);
for (const f of fStmts().list.all(site.slug)) inboxes.add(f.shared_inbox || f.inbox);
mres.inboxes.forEach((i) => inboxes.add(i)); // people @mentioned inline in the edit
inboxes.delete(`${me}/inbox`); inboxes.delete(`${base}/ap/inbox`);
let delivered = 0;
for (const inbox of [...inboxes].filter(Boolean)) {
let ok = false;
try { const st = await deliver(inbox, update, `${me}#main-key`, keys.private_pem); ok = st >= 200 && st < 300; } catch { ok = false; }
if (ok) delivered++;
else enqueueDelivery(site.slug, inbox, update); // durable: retry the edit later (was silently dropped)
}
console.log('[AP] outreply edit', site.slug, 'delivered', delivered);
return { ok: true, content, delivered };
}
// ── Fediverse CLIENT: follow accounts + home timeline ─────────────
// Resolve an @user@domain handle to its actor URL via WebFinger.
export async function webfingerResolve(handle) {
const h = String(handle || '').trim().replace(/^@/, '');
const parts = h.split('@');
if (parts.length !== 2 || !parts[0] || !parts[1]) return null;
const acct = `${parts[0]}@${parts[1]}`;
try {
const r = await safeFetch(`https://${parts[1]}/.well-known/webfinger?resource=acct:${encodeURIComponent(acct)}`,
{ headers: { Accept: 'application/jrd+json, application/json' } });
if (!r.ok) return null;
const jrd = await r.json();
const link = (jrd.links || []).find((l) => l.rel === 'self' && /activity\+json|ld\+json/.test(l.type || ''));
return safeUrl(link ? link.href : '') || null;
} catch { return null; }
}
let _insFw, _delFw, _listFw, _accFw, _oneFw, _setAB;
function fwStmts() {
if (!_insFw) {
_insFw = db.prepare('INSERT OR REPLACE INTO ap_following (slug, actor_uri, handle, name, icon, url, inbox, follow_id, status, auto_boost, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)');
_delFw = db.prepare('DELETE FROM ap_following WHERE slug = ? AND actor_uri = ?');
_listFw = db.prepare('SELECT * FROM ap_following WHERE slug = ? ORDER BY created_at DESC');
_accFw = db.prepare("UPDATE ap_following SET status = 'accepted' WHERE follow_id = ?");
_oneFw = db.prepare('SELECT * FROM ap_following WHERE slug = ? AND actor_uri = ?');
_setAB = db.prepare('UPDATE ap_following SET auto_boost = ? WHERE slug = ? AND actor_uri = ?');
}
return { ins: _insFw, del: _delFw, list: _listFw, acc: _accFw, one: _oneFw, setAB: _setAB };
}
export function listFollowing(slug) { return fwStmts().list.all(slug); }
// Toggle auto-boost ("feature") on an account we already follow.
export function setAutoBoost(slug, actorUri, on) {
try { fwStmts().setAB.run(on ? 1 : 0, slug, actorUri); } catch { /* ignore */ }
// Featuring an account → AP-native catch-up so the Cirkel isn't empty until they next
// post (push doesn't backfill history-before-follow). Fire-and-forget pull, sends nothing.
if (on) backfillFromOutbox(slug, actorUri).catch(() => {});
return { ok: true };
}
let _insTl, _listTl, _delTl;
function tlStmts() {
if (!_insTl) {
_insTl = db.prepare('INSERT OR IGNORE INTO ap_timeline (id, slug, author_uri, author_name, author_handle, author_icon, author_url, content, url, published, media_json, nsfw, cw, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)');
_listTl = db.prepare('SELECT * FROM ap_timeline WHERE slug = ? ORDER BY COALESCE(published, created_at) DESC LIMIT ? OFFSET ?');
_delTl = db.prepare('DELETE FROM ap_timeline WHERE id = ?');
}
return { ins: _insTl, list: _listTl, del: _delTl };
}
export function getTimeline(slug, limit, offset) { return tlStmts().list.all(slug, limit || 50, offset || 0); }
// Inbox C2S read: a timeline row's media_json ([{url, type}], written on the
// inbound Create) → AS2 `attachment` array, so a client (Shaer) can render a
// friend's images/audio/video natively, exactly like own outbox posts. The
// stored `type` is the mediaType and may be ''. Malformed JSON yields
// undefined and never blocks the item.
export function timelineAttachments(mediaJson) {
try {
const list = mediaJson ? JSON.parse(mediaJson) : [];
const rows = (Array.isArray(list) ? list : [])
.filter((m) => m && m.url)
.map((m) => ({ type: 'Document', mediaType: m.type || undefined, url: m.url }));
return rows.length ? rows : undefined;
} catch { return undefined; }
}
// FEP-9098 custom emojis. Inbound: keep the note's Emoji tags (as JSON) so we
// can serve them back. `extractEmojiTags` returns the JSON to store (or null);
// `timelineEmojis` turns the stored JSON back into an AS2 `tag` array for the
// C2S inbox read, so a client (Shaer) can render :shortcode: as an image.
export function extractEmojiTags(tag) {
const arr = Array.isArray(tag) ? tag : (tag ? [tag] : []);
const emojis = arr.filter((t) => t && (Array.isArray(t.type) ? t.type[0] : t.type) === 'Emoji'
&& typeof t.name === 'string' && t.icon);
return emojis.length ? JSON.stringify(emojis) : null;
}
export function timelineEmojis(emojiJson) {
try { const arr = emojiJson ? JSON.parse(emojiJson) : null; return (Array.isArray(arr) && arr.length) ? arr : undefined; }
catch { return undefined; }
}
// FEP-e232 object links (quotes / inline references). Inbound: keep the note's
// Link tags whose mediaType marks an AP object (the AS2-profiled ld+json, or
// activity+json as its equivalent) as JSON, so the C2S inbox read can serve
// them back and a client (Shaer) can render the quote/reference. Mirrors
// extractEmojiTags. Plain hyperlinks (text/html) and Mentions are dropped.
export function extractObjectLinkTags(tag) {
const arr = Array.isArray(tag) ? tag : (tag ? [tag] : []);
const links = arr.filter((t) => {
if (!t || (Array.isArray(t.type) ? t.type[0] : t.type) !== 'Link') return false;
if (typeof t.href !== 'string' || !t.href) return false;
const mt = String(t.mediaType || '').toLowerCase();
return (mt.startsWith('application/ld+json') && mt.includes('activitystreams'))
|| mt.startsWith('application/activity+json');
});
return links.length ? JSON.stringify(links) : null;
}
export function timelineObjectLinks(linkJson) {
try { const arr = linkJson ? JSON.parse(linkJson) : null; return (Array.isArray(arr) && arr.length) ? arr : undefined; }
catch { return undefined; }
}
// FEP-044f quote posts: a quote is usually NOT an FEP-e232 tag but an
// object-level property. FEP-044f §"how to recognise" lists them all:
// `quote` (the FEP property, a string or an embedded Link/object), and the
// de-facto `quoteUrl` (as:), `quoteUri` (fedibird), `_misskey_quote` (misskey).
// This returns the quoted object's URL from whichever is present.
export function extractQuoteUrl(note) {
if (!note || typeof note !== 'object') return null;
const q = note.quote ?? note.quoteUrl ?? note.quoteUri ?? note['_misskey_quote'];
if (!q) return null;
if (typeof q === 'string') return q || null;
if (typeof q === 'object') return (typeof q.id === 'string' && q.id) || (typeof q.href === 'string' && q.href) || null;
return null;
}
// The note's object-link tags for storage: real FEP-e232 Link tags PLUS any
// FEP-044f object-level quote, normalised to one FEP-e232-shaped Link (rel
// _misskey_quote) so the client's single object-link path renders them all.
// Deduped by href. Returns the JSON to store (or null if the note has neither).
export function extractLinkJson(note) {
const links = [];
const fromTag = extractObjectLinkTags(note && note.tag);
if (fromTag) { try { links.push(...JSON.parse(fromTag)); } catch { /* ignore */ } }
const qUrl = extractQuoteUrl(note);
if (qUrl && !links.some((l) => l && l.href === qUrl)) {
links.push({ type: 'Link', mediaType: 'application/activity+json', href: qUrl,
rel: ['https://misskey-hub.net/ns#_misskey_quote'], name: qUrl });
}
return links.length ? JSON.stringify(links) : null;
}
// The URL of the quoted post, from either an object-level quote (FEP-044f) or a
// quote-rel FEP-e232 Link tag. Used to resolve the embedded quote card.
export function quoteHrefOf(note) {
const direct = extractQuoteUrl(note);
if (direct) return direct;
const arr = Array.isArray(note && note.tag) ? note.tag : (note && note.tag ? [note.tag] : []);
for (const t of arr) {
if (!t || (Array.isArray(t.type) ? t.type[0] : t.type) !== 'Link' || typeof t.href !== 'string') continue;
const rel = Array.isArray(t.rel) ? t.rel : (t.rel ? [t.rel] : []);
if (rel.some((r) => /quote/i.test(String(r)))) return t.href;
}
return null;
}
// Turn the stored quote snapshot back into the object the C2S inbox read serves
// as `shaer:quote`, so the client can render the embedded quote card.
export function timelineQuote(quoteJson) {
try { const q = quoteJson ? JSON.parse(quoteJson) : null; return (q && typeof q === 'object') ? q : undefined; }
catch { return undefined; }
}
// Store the author's display-name emoji map (from actorInfo().emojis) on a
// timeline row, so the byline can render a ":shortcode:" name. No-op when the
// name has no custom emoji (the common case).
function storeAuthorEmoji(id, slug, ai) {
if (!ai || !ai.emojis || !Object.keys(ai.emojis).length) return;
try { db.prepare('UPDATE ap_timeline SET author_emoji_json = ? WHERE id = ? AND slug = ?').run(JSON.stringify(ai.emojis), id, slug); } catch { /* ignore */ }
}
// A display-name emoji map (actorInfo().emojis) → JSON to store, or null.
function emojiJsonOf(map) { return (map && Object.keys(map).length) ? JSON.stringify(map) : null; }
// ── Cirkel = posts from the accounts you auto-boost ("feature an artist") ──
let _abCount, _cirkelPosts, _cirkelMembers;
export function autoBoostCount(slug) {
try { if (!_abCount) _abCount = db.prepare('SELECT COUNT(*) AS n FROM ap_following WHERE slug = ? AND auto_boost = 1'); return _abCount.get(slug).n; } catch { return 0; }
}
export function getCirkelPosts(slug, limit, offset) {
try {
// Cirkel = posts from featured (auto_boost) accounts + posts you boosted
// (t.boosted), mixed by date. One row per note in ap_timeline → no duplicates.
if (!_cirkelPosts) _cirkelPosts = db.prepare(`
SELECT t.id, t.author_uri, t.author_name, t.author_handle, t.author_icon, t.author_url,
t.content, t.url, t.published, t.media_json, t.boosted, t.nsfw, t.cw
FROM ap_timeline t
LEFT JOIN ap_following f ON f.slug = t.slug AND f.actor_uri = t.author_uri
WHERE t.slug = ? AND (f.auto_boost = 1 OR t.boosted = 1)
ORDER BY COALESCE(t.published, t.created_at) DESC, t.rowid DESC
LIMIT ? OFFSET ?`);
return _cirkelPosts.all(slug, limit || 60, offset || 0);
} catch { return []; }
}
export function getCirkelMembers(slug) {
try { if (!_cirkelMembers) _cirkelMembers = db.prepare('SELECT name, url, icon FROM ap_following WHERE slug = ? AND auto_boost = 1 ORDER BY name'); return _cirkelMembers.all(slug); } catch { return []; }
}
// Mark a timeline post as boosted so it shows in the Cirkel (mixed by date).
let _markBoost, _unmarkBoost, _boostedCount;
export function markBoosted(slug, noteId) {
try { if (!_markBoost) _markBoost = db.prepare('UPDATE ap_timeline SET boosted = 1 WHERE slug = ? AND id = ?'); _markBoost.run(slug, noteId); } catch { /* ignore */ }
}
export function unmarkBoosted(slug, noteId) {
try { if (!_unmarkBoost) _unmarkBoost = db.prepare('UPDATE ap_timeline SET boosted = 0 WHERE slug = ? AND id = ?'); _unmarkBoost.run(slug, noteId); } catch { /* ignore */ }
}
let _markLike, _unmarkLike;
export function markLiked(slug, noteId) {
try { if (!_markLike) _markLike = db.prepare('UPDATE ap_timeline SET liked = 1 WHERE slug = ? AND id = ?'); _markLike.run(slug, noteId); } catch { /* ignore */ }
}
export function unmarkLiked(slug, noteId) {
try { if (!_unmarkLike) _unmarkLike = db.prepare('UPDATE ap_timeline SET liked = 0 WHERE slug = ? AND id = ?'); _unmarkLike.run(slug, noteId); } catch { /* ignore */ }
}
export function getTimelineReaction(slug, noteId) {
try { const r = db.prepare('SELECT liked, boosted FROM ap_timeline WHERE slug = ? AND id = ?').get(slug, noteId); return { liked: !!(r && r.liked), boosted: !!(r && r.boosted) }; } catch { return { liked: false, boosted: false }; }
}
// Boost a REMOTE post that may not be in your timeline (you don't follow the author):
// store it in ap_timeline (INSERT OR IGNORE → no dup for followed posts) so it shows in
// the Cirkel with a Boost badge, then flag it boosted.
export function upsertBoostedNote(slug, note) {
if (!slug || !note || !note.object_uri) return;
const id = note.object_uri;
// Prefer the full typed media (incl. video/mp4 — a Loops boost is video-only and
// rendered a bare text tile); fall back to the image-only list for older callers.
const media = (note.media && note.media !== '[]')
? note.media
: JSON.stringify((note.images || []).map((u) => ({ url: u, type: 'image/jpeg' })));
try {
const r = tlStmts().ins.run(id, slug, note.actor_uri || '', note.actor_name || '', note.actor_handle || '',
note.actor_icon || '', note.actor_url || '', note.content || '', note.url || null,
new Date().toISOString(), media, note.sensitive ? 1 : 0, note.cw || null);
if (!r.changes) {
// Row already cached (INSERT OR IGNORE) → refresh it with the freshly
// resolved note. Without this a row cached without its cover (or with
// stale content) stayed stale forever — even boosting again didn't heal it.
// Keep the CACHED media when the resolve yielded none: an empty re-resolve
// used to clobber a good media_json (the followed copy had the video, the
// boost wiped it to []).
db.prepare(`UPDATE ap_timeline SET content = ?, media_json = CASE WHEN ? = '[]' THEN media_json ELSE ? END,
nsfw = ?, cw = ?, url = COALESCE(?, url) WHERE slug = ? AND id = ?`)
.run(note.content || '', media, media, note.sensitive ? 1 : 0, note.cw || null, note.url || null, slug, id);
}
} catch { /* ignore */ }
markBoosted(slug, id);
}
export function boostedCount(slug) {
try { if (!_boostedCount) _boostedCount = db.prepare('SELECT COUNT(*) AS n FROM ap_timeline WHERE slug = ? AND boosted = 1'); return _boostedCount.get(slug).n; } catch { return 0; }
}
// Resolve a Klonkt/AP actor URL from a site root: a Klonkt site's root 302s to
// /ap/users/ (content negotiation; Location may be relative). Used by
// followActor for bare-domain follows.
// NB: the old auto-migration of legacy Cirkels (circle_links -> AP follows) was
// REMOVED on 2026-06-26 — it auto-sent Follows on boot, which violates "the code
// never throws anything into the fediverse automatically" (would surprise-Follow
// for some operators at scale). The dead circle_links table stays as harmless dead
// data; an operator restores an old cirkel by re-following in /following (their click).
async function resolveApActor(siteUrl) {
try {
const r = await fetch(siteUrl, { headers: { Accept: 'application/activity+json' }, redirect: 'manual' });
if (r.status >= 300 && r.status < 400) { const loc = r.headers.get('location'); if (loc) return new URL(loc, siteUrl).href; }
if (r.ok) return siteUrl;
} catch { /* unreachable */ }
return null;
}
// ── Self-heal: re-sync the fediverse cache (ap_timeline) after a DRASTIC update ──
// Runs ONCE per SELFHEAL_VERSION bump — NOT on every boot. Re-fetches each cached
// note and refreshes content + media (recovers covers/edits that were delivered
// during a flux window, e.g. a fleet-wide update), and drops notes that are gone
// (404/410). Bump SELFHEAL_VERSION only on a release that warrants a re-sync.
const SELFHEAL_VERSION = 21; // v21: drop direct notes (🛟 help requests, waves) that were cached as timeline posts
async function fetchNoteAP(url) {
try {
const r = await fetch(url, { headers: { Accept: 'application/activity+json' } });
if (r.status === 404 || r.status === 410) return 404;
if (r.ok) return await r.json();
} catch { /* unreachable */ }
return null;
}
function mediaFromNote(note) {
const atts = (Array.isArray(note.attachment) ? note.attachment : []).map((a) => ({ url: safeUrl(a && a.url), type: (a && a.mediaType) || '' })).filter((m) => m.url);
if (!atts.some((m) => !m.type || /image/i.test(m.type)) && note.image) {
const im = Array.isArray(note.image) ? note.image[0] : note.image;
const iu = safeUrl(typeof im === 'string' ? im : (im && im.url));
if (iu) atts.push({ url: iu, type: (im && im.mediaType) || 'image/jpeg' });
}
return JSON.stringify(atts);
}
// FEP-044f, emit side. The mirror of extractQuoteUrl (ingest): when one of our
// own posts quotes a fediverse object, say so in the shapes the network really
// reads. `quote` is the FEP property; quoteUrl / _misskey_quote are the de-facto
// ones Mastodon and Misskey look at, and the FEP-e232 `Link` in `tag` is the
// third form. All three point at the same object, which is what every reader
// expects. The quoted author goes in `cc`, because being quoted without being
// told is exactly the rudeness this FEP is trying to design away.
export function applyQuoteProps(note, quoteUri, quoteActor) {
if (!note || typeof quoteUri !== 'string' || !/^https?:\/\//i.test(quoteUri)) return note;
note.quote = quoteUri;
note.quoteUrl = quoteUri;
note['_misskey_quote'] = quoteUri;
note.tag = [...(note.tag || []), {
type: 'Link',
mediaType: 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
href: quoteUri,
rel: ['https://misskey-hub.net/ns#_misskey_quote'],
name: quoteUri,
}];
if (typeof quoteActor === 'string' && /^https?:\/\//i.test(quoteActor)) {
note.cc = [...new Set([...(note.cc || []), quoteActor])];
}
return note;
}
// The first external (non-fediverse) link in a note, resolved to the same card
// shape as a quote: THUMBNAIL ONLY, never the provider's iframe. An arbitrary
// third-party frame inside a kid-safe app is a hole you cannot close again, so
// the embed carries an image and a title and nothing executable.
// Returns the JSON to store, or null when there is nothing worth showing.
export async function resolveExternalEmbed(html) {
const first = firstExternalUrl(html);
if (!first) return null;
const io = EmbedResolver.liveIO({
safeFetch,
detectProvider: (u) => AudioEmbedService.detectProvider(u),
fetchActor,
actorInfo,
});
const card = await EmbedResolver.resolveEmbed(first, io).catch(() => null);
// 'ap' is handled by the quote path; a bare 'link' is not worth a card.
if (!card || card.kind === 'ap' || card.kind === 'link') return null;
const thumb = (card.media || []).find((m) => m && m.url);
if (!thumb && !card.title) return null;
// Title, provider and author name come from a third party. Store them as
// PLAIN TEXT (tags stripped, length-capped), so no renderer downstream has to
// be the one that remembers to escape. A card is a card, not an essay.
const plain = (v) => (v ? HtmlSanitizerService.toPlainText(String(v)).trim().slice(0, 200) : null);
return JSON.stringify({
url: card.url,
kind: card.kind, // 'provider' | 'oembed'
provider: plain(card.provider),
title: plain(card.title),
author: card.author ? { ...card.author, name: plain(card.author.name), handle: plain(card.author.handle) } : null,
media: thumb ? [thumb] : [], // thumbnail only, no html/iframe
});
}
/**
* Does our own post link to a fediverse object? Returns { uri, actor } when the
* first external link resolves to a quotable AP object, else null. Runs once at
* publish time; the answer is stored on the post.
*/
export async function resolveOwnQuote(html) {
const first = firstExternalUrl(html);
if (!first) return null;
const io = EmbedResolver.liveIO({ safeFetch, detectProvider: () => null, fetchActor, actorInfo });
const card = await EmbedResolver.resolveEmbed(first, io).catch(() => null);
if (!card || card.kind !== 'ap' || !card.id) return null;
return { uri: card.id, actor: card.attributedTo || null };
}
/** The first http(s) link in sanitized note HTML that is not a mention/hashtag. */
export function firstExternalUrl(html) {
if (!html || typeof html !== 'string') return null;
for (const m of html.matchAll(/]*href=["']([^"']+)["'][^>]*>/gi)) {
const tag = m[0];
if (/\b(mention|hashtag|u-url)\b/i.test(tag) && /mention|hashtag/i.test(tag)) continue;
const href = m[1];
if (/^https?:\/\//i.test(href)) return href;
}
return null;
}
/** The stored external-embed card, for the C2S read. */
export function timelineEmbed(embedJson) {
try { const e = embedJson ? JSON.parse(embedJson) : null; return (e && typeof e === 'object' && e.url) ? e : undefined; }
catch { return undefined; }
}
// FEP-044f embedded quote card: resolve the quoted post to a compact, sanitised
// snapshot { url, author{name,handle,icon}, content, published, media } so the
// client can render it as a nested card instead of a bare link. Best-effort and
// SSRF-safe (apGetJson): returns null on any failure, and the client falls back
// to the object-link chip. The content goes through the same sanitiser as every
// other note, so the kid-safe guarantees hold.
async function resolveQuote(note) {
const url = quoteHrefOf(note);
if (!url) return null;
const q = await apGetJson(url);
if (!q || typeof q !== 'object') return null;
const authorUri = typeof q.attributedTo === 'string' ? q.attributedTo
: (q.attributedTo && typeof q.attributedTo.id === 'string' ? q.attributedTo.id : null);
const ai = authorUri ? actorInfo(await fetchActor(authorUri), authorUri) : null;
// The quoted post's own FEP-9098 emojis, so :shortcode: renders in the card.
const emojis = {};
try {
for (const e of JSON.parse(extractEmojiTags(q.tag) || '[]')) {
const u = e.icon && (e.icon.url || (Array.isArray(e.icon) && e.icon[0] && e.icon[0].url));
if (typeof e.name === 'string' && u) emojis[e.name] = u;
}
} catch { /* ignore */ }
let media = []; try { media = JSON.parse(mediaFromNote(q)); } catch { /* ignore */ }
const snapshot = {
url: safeUrl(q.url || q.id || url) || url,
author: ai ? { name: ai.name, handle: ai.handle, icon: ai.icon } : null,
content: HtmlSanitizerService.sanitize(q.content || ''),
published: q.published || null,
media,
emojis: Object.keys(emojis).length ? emojis : undefined,
};
return JSON.stringify(snapshot);
}
/**
* The card under a post: a fediverse quote (FEP-044f) when the note has one,
* otherwise an external link preview. Both render as the SAME card, so only one
* of the two is ever stored. Returns {column, json} or null.
*
* Both halves reach out over the network, which is why every caller runs this
* out of band: an inbox answer must never wait on a third party.
*/
async function resolveCard(o) {
if (quoteHrefOf(o)) {
const qj = await resolveQuote(o);
return qj ? { column: 'quote_json', json: qj } : null;
}
const ej = await resolveExternalEmbed(o && o.content);
return ej ? { column: 'embed_json', json: ej } : null;
}
// A generic SSRF-safe AP GET (collections / pages).
async function apGetJson(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 > 3_000_000) return null;
return await r.json();
} catch { return null; }
}
// AP-native catch-up: pull an actor's standard `outbox` collection and merge their recent
// top-level posts into the timeline for `slug`. Push (Create delivery) cannot backfill
// history-from-before-you-followed or a delivery that was missed while you were down;
// reading the outbox is the spec-conform way to catch up. PULL ONLY — sends nothing.
export async function backfillFromOutbox(slug, actorUri, limit = 20) {
try {
if (!slug || !actorUri) return 0;
const actor = await fetchActor(actorUri);
if (!actor || !actor.outbox) return 0;
let page = await apGetJson(typeof actor.outbox === 'string' ? actor.outbox : actor.outbox.id);
let items = (page && (page.orderedItems || page.items)) || [];
if (!items.length && page && page.first) {
page = await apGetJson(typeof page.first === 'string' ? page.first : page.first.id);
items = (page && (page.orderedItems || page.items)) || [];
}
if (!Array.isArray(items) || !items.length) return 0;
const ai = actorInfo(actor, actorUri);
let added = 0;
for (const it of items.slice(0, limit)) {
// Each item is usually a Create wrapping a Note, or sometimes the Note itself.
const o = (it && typeof it.object === 'object' && it.object) ? it.object : it;
if (!o || !o.id) continue;
if (o.type && o.type !== 'Note' && o.type !== 'Article' && o.type !== 'Question') continue; // skip boosts/other
if (o.inReplyTo) continue; // top-level only
const auth = actorUriOf(o.attributedTo);
if (auth && auth !== actorUri) continue; // their OWN posts only
const html = HtmlSanitizerService.sanitize(o.content || '');
const poll = parsePoll(o); // a Question (poll) → carry its options/counts on backfill too
try {
const r = tlStmts().ins.run(o.id, slug, actorUri, ai.name, ai.handle, ai.icon, ai.url, html, o.url || null, o.published || null, mediaFromNote(o), o.sensitive ? 1 : 0, o.summary || null);
if (r && r.changes > 0) added++;
// FEP-9098: keep custom-emoji tags from backfilled posts too.
{ const ej = extractEmojiTags(o.tag); if (ej) { try { db.prepare('UPDATE ap_timeline SET emoji_json = ? WHERE id = ? AND slug = ?').run(ej, o.id, slug); } catch { /* ignore */ } } }
storeAuthorEmoji(o.id, slug, ai); // custom-emoji display name for the byline
// FEP-e232 + FEP-044f: keep object-link/quote tags from backfilled posts too.
{ const lj = extractLinkJson(o); if (lj) { try { db.prepare('UPDATE ap_timeline SET link_json = ? WHERE id = ? AND slug = ?').run(lj, o.id, slug); } catch { /* ignore */ } } }
// FEP-044f: resolve the embedded quote card for backfilled posts too.
if (quoteHrefOf(o)) { const qj = await resolveQuote(o); if (qj) { try { db.prepare('UPDATE ap_timeline SET quote_json = ? WHERE id = ? AND slug = ?').run(qj, o.id, slug); } catch { /* ignore */ } } }
// Set poll_json if this is a poll and we don't already have it (COALESCE preserves a vote).
if (poll) { try { db.prepare('UPDATE ap_timeline SET poll_json = COALESCE(poll_json, ?) WHERE id = ? AND slug = ?').run(JSON.stringify(poll), o.id, slug); } catch { /* ignore */ } }
} catch { /* ignore */ }
}
if (added) console.log('[AP] outbox backfill', actorUri, '→', slug, '+' + added);
return added;
} catch { return 0; }
}
// ── Remote thread crawl (fill the gaps in a local post's conversation) ────────────
// Most replies reach us by delivery, but replies-to-replies that live on other servers and
// aren't addressed to us are missed. This pulls the AS2 `replies` collections of the replies
// we DO have, caching any newly-found ones in ap_interactions.
//
// Matches Mastodon's behaviour: ONE level per crawl (like its FetchRepliesService), not a deep
// recursive walk. Deeper levels fill in incrementally across crawls — once a fetched reply is
// cached it becomes a seed itself, so its own replies are pulled on a later view (Mastodon's
// per-status cascade). Bounded + polite (serial), PULL only, and stale-while-revalidate: it
// never runs in a page request — the view renders from cache; a stale post kicks off a
// background refresh for the NEXT view.
const THREAD_TTL_MS = 15 * 60 * 1000; // don't re-crawl a post more than ~4×/hour
const THREAD_MAX_DEPTH = 1; // one hop per crawl (like Mastodon); deeper fills in over crawls
const THREAD_MAX_FETCHES = 30; // hard cap on remote GETs per crawl (be a good peer)
const _crawlingThreads = new Set(); // per-post in-flight lock (no stampede across views)
function threadCrawlTs(postId) {
try { const r = db.prepare('SELECT value FROM app_settings WHERE key = ?').get('thread_crawl:' + postId); return r ? (Number(r.value) || 0) : 0; }
catch { return 0; }
}
function setThreadCrawlTs(postId, ts) {
try { db.prepare('INSERT INTO app_settings (key, value) VALUES (?, ?) ON CONFLICT(key) DO UPDATE SET value = excluded.value').run('thread_crawl:' + postId, String(ts)); }
catch { /* ignore */ }
}
// Read a note's `replies` (string ref / Collection with `first` / paged CollectionPages) →
// child note URIs. Every remote GET goes through `budget` so the whole crawl stays capped.
async function collectReplyItems(repliesRef, maxPages, budget) {
const uris = [];
let node = typeof repliesRef === 'string' ? await budget.get(repliesRef) : repliesRef;
if (node && node.first) node = typeof node.first === 'string' ? await budget.get(node.first) : node.first;
let pages = 0;
while (node && pages++ < maxPages) {
for (const it of (node.items || node.orderedItems || [])) {
const u = typeof it === 'string' ? it : (it && it.id);
if (u && /^https?:\/\//i.test(u)) uris.push(u);
}
if (!node.next) break;
node = typeof node.next === 'string' ? await budget.get(node.next) : node.next;
}
return uris;
}
async function crawlThread(postId) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
if (!base) return;
// Seed frontier = the remote reply note URIs we already have; also the dedup set.
let known;
try { known = new Set(db.prepare("SELECT object_uri FROM ap_interactions WHERE post_id = ? AND kind = 'reply' AND object_uri != ''").all(postId).map((r) => r.object_uri)); }
catch { return; }
const seeds = [...known].filter((u) => /^https?:\/\//i.test(u));
if (!seeds.length) return; // nothing remote to expand
// Owner-removed replies (tombstones) join the dedup set AFTER seeding, so the
// crawler never re-adds them via thread-filling (they're gone from the seeds
// already because rejectInteraction deleted their ap_interactions row).
try { for (const r of db.prepare('SELECT object_uri FROM ap_rejected_objects WHERE post_id = ?').all(postId)) known.add(r.object_uri); }
catch { /* table always exists after boot migration */ }
let fetches = 0;
const budget = { get: async (u) => { if (fetches >= THREAD_MAX_FETCHES) return null; fetches++; return apGetJson(u); } };
const visited = new Set(); // notes whose replies collection we've already expanded
let frontier = seeds.slice();
let added = 0;
for (let depth = 0; depth < THREAD_MAX_DEPTH && frontier.length && fetches < THREAD_MAX_FETCHES; depth++) {
const nextFrontier = [];
for (const noteUri of frontier) {
if (visited.has(noteUri) || fetches >= THREAD_MAX_FETCHES) continue;
visited.add(noteUri);
const note = await budget.get(noteUri);
if (!note || !note.replies) continue;
const childUris = await collectReplyItems(note.replies, 2, budget);
for (const cu of childUris) {
if (known.has(cu) || fetches >= THREAD_MAX_FETCHES) continue;
known.add(cu);
const child = await budget.get(cu);
if (!child || !child.id || (child.type !== 'Note' && child.type !== 'Article')) continue;
if (isRejectedObject(child.id)) continue; // note id can differ from the collection URI (redirects)
const actorUri = actorUriOf(child.attributedTo);
if (!actorUri || isBlockedAny(actorUri)) continue; // skip blocked authors
const actor = await budget.get(actorUri); // may be null if budget spent → fallback handle
const ai = actorInfo(actor, actorUri);
const html = HtmlSanitizerService.sanitize(child.content || '');
// The child replies to `note` by construction (it's in note's replies collection).
try { iStmts().ins.run('reply', postId, child.id, actorUri, ai.name, ai.handle, ai.url, ai.icon, html, child.published || null, note.id || noteUri, noteVisibility(child), extractEmojiTags(child.tag), emojiJsonOf(ai.emojis)); added++; } catch { /* ignore */ }
nextFrontier.push(child.id); // expand this reply's own replies next depth
}
}
frontier = nextFrontier;
}
if (added) console.log('[AP] thread crawl', postId, '+' + added, 'remote replies (' + fetches + ' fetches)');
}
// Stale-while-revalidate entry point: call from the post view. Renders nothing, blocks nothing —
// fires a background crawl only if this post hasn't been crawled within the TTL.
export function maybeCrawlThread(postId) {
if (!postId || _crawlingThreads.has(postId)) return;
if (Date.now() - threadCrawlTs(postId) < THREAD_TTL_MS) return;
_crawlingThreads.add(postId);
setThreadCrawlTs(postId, Date.now()); // optimistic mark so concurrent/next views don't re-fire
crawlThread(postId).catch((e) => console.warn('[AP] thread crawl failed:', e && e.message)).finally(() => _crawlingThreads.delete(postId));
}
let _selfHealing = false;
export async function selfHealTimeline() {
if (_selfHealing) return; _selfHealing = true;
try {
let cur = 0;
try { const r = db.prepare('SELECT value FROM app_settings WHERE key = ?').get('selfheal_version'); cur = r ? (parseInt(r.value, 10) || 0) : 0; } catch { return; }
if (cur >= SELFHEAL_VERSION) return; // already healed for this version — skip on normal boots
// v21: direct notes used to land in the timeline as if they were posts, so a
// ward's 🛟 help request showed up in the guardian's Krant. The insert now
// refuses them; drop the ones already cached. Scoped to the two kinds we can
// still recognise afterwards (help request, wave) — a plain public mention
// from someone you follow IS a timeline post and must stay.
try {
const r = db.prepare(`DELETE FROM ap_timeline WHERE EXISTS (
SELECT 1 FROM ap_mentions m
WHERE m.object_uri = ap_timeline.id AND m.slug = ap_timeline.slug
AND (m.help_request = 1 OR m.wave = 1))`).run();
if (r.changes) console.log(`[AP] self-heal v21: ${r.changes} direct note(s) removed from the timeline`);
} catch { /* table may predate the columns */ }
let rows = [];
try { rows = db.prepare('SELECT id, slug, content, media_json, nsfw, cw, url, emoji_json, link_json, quote_json, author_uri, author_name, author_emoji_json, reblog_name, reblog_handle, reblog_emoji_json, embed_json FROM ap_timeline ORDER BY rowid DESC LIMIT 200').all(); } catch { /* no table */ }
let healed = 0, failed = 0;
for (const r of rows) {
// Link previews first, and deliberately BEFORE the note re-fetch. A
// preview is resolved from the content we already hold, so hanging it
// behind a remote fetch meant one unreachable origin skipped the whole
// row (`continue` below) and the card never appeared. It needs nothing
// from the origin, so it must not depend on it.
if (!r.quote_json && !r.embed_json) {
try {
const ej = await resolveExternalEmbed(r.content);
if (ej) db.prepare('UPDATE ap_timeline SET embed_json = ? WHERE id = ?').run(ej, r.id);
} catch { /* best-effort, never blocks the heal */ }
}
try {
const note = await fetchNoteAP(r.id);
if (note === 404) { db.prepare('DELETE FROM ap_timeline WHERE id = ?').run(r.id); healed++; continue; }
if (!note || typeof note !== 'object') { failed++; continue; } // origin unreachable right now
const html = HtmlSanitizerService.sanitize(note.content || '');
const media = mediaFromNote(note);
const nsfw = note.sensitive ? 1 : 0; // re-sync NSFW/sensitive + CW onto already-cached posts
const cw = note.summary || null;
const url = note.url || null; // re-sync the human url (catches a remote slug rename)
const emoji = extractEmojiTags(note.tag); // FEP-9098: re-capture custom-emoji tags (v8)
const link = extractLinkJson(note); // FEP-e232 + FEP-044f: re-capture object-link/quote tags (v9)
// FEP-044f: resolve the embedded quote card (v11). COALESCE-style: keep a
// cached snapshot if the quoted post is momentarily unreachable now.
const quote = quoteHrefOf(note) ? (await resolveQuote(note)) || r.quote_json || null : null;
if ((html && html !== r.content) || media !== (r.media_json || '[]') || nsfw !== (r.nsfw || 0) || (cw || '') !== (r.cw || '') || (url && url !== r.url) || (emoji || '') !== (r.emoji_json || '') || (link || '') !== (r.link_json || '') || (quote || '') !== (r.quote_json || '')) {
db.prepare('UPDATE ap_timeline SET content = ?, media_json = ?, nsfw = ?, cw = ?, url = COALESCE(?, url), emoji_json = ?, link_json = ?, quote_json = ? WHERE id = ?').run(html || r.content, media, nsfw, cw, url, emoji, link, quote, r.id);
healed++;
}
// v13: a custom-emoji display name needs the author's emoji map. Fetch
// the actor once, only for rows whose name has a shortcode and no map yet.
if (/:[A-Za-z0-9_+-]+:/.test(r.author_name || '') && !r.author_emoji_json && r.author_uri) {
const ai = actorInfo(await fetchActor(r.author_uri), r.author_uri);
if (ai.emojis) { try { db.prepare('UPDATE ap_timeline SET author_emoji_json = ? WHERE id = ?').run(JSON.stringify(ai.emojis), r.id); } catch { /* ignore */ } }
}
// v14: same for the booster's display name ("X boosted"). The row stores
// no booster URI, so resolve it from the handle via webfinger. Scoped to
// this exact row (slug) since a note can be boosted by different people.
if (/:[A-Za-z0-9_+-]+:/.test(r.reblog_name || '') && !r.reblog_emoji_json && r.reblog_handle) {
const bUri = await webfingerResolve(r.reblog_handle);
const em = bUri ? actorNameEmojis(await fetchActor(bUri)) : undefined;
if (em) { try { db.prepare('UPDATE ap_timeline SET reblog_emoji_json = ? WHERE id = ? AND slug = ?').run(JSON.stringify(em), r.id, r.slug); } catch { /* ignore */ } }
}
} catch { failed++; /* per-note best-effort */ }
}
// Only mark this version DONE after a clean pass. Some origins are briefly
// offline exactly when we heal (phone-hosted instances!): skipping them and
// consuming the version would leave those rows stale forever. Instead retry
// on the next boots, giving up after a few attempts (permanently-dead
// origins answer 404/410 and are deleted above, so they don't loop).
const setSetting = (k, v) => { try { db.prepare('INSERT OR REPLACE INTO app_settings (key, value) VALUES (?, ?)').run(k, String(v)); } catch { /* ignore */ } };
let attempts = 0;
try { const a = db.prepare('SELECT value FROM app_settings WHERE key = ?').get('selfheal_attempts'); attempts = a ? (parseInt(a.value, 10) || 0) : 0; } catch { /* ignore */ }
if (failed === 0 || attempts >= 4) {
setSetting('selfheal_version', SELFHEAL_VERSION);
setSetting('selfheal_attempts', 0);
} else {
setSetting('selfheal_attempts', attempts + 1);
}
if (rows.length) console.log(`[AP] self-heal v${SELFHEAL_VERSION}: ${healed}/${rows.length} timeline notes${failed ? ` (${failed} unreachable — will retry next boot)` : ''}`);
} catch { /* never block boot */ } finally { _selfHealing = false; }
}
// Follow a fediverse account by @handle (WebFinger → actor → signed Follow).
export async function followActor(site, handle, autoBoost = false) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
if (!base || !site || !site.slug) return { error: 'config' };
// Accept any of: a profile/actor URL, an @user@host handle (WebFinger), or a
// bare site domain (site.com) — for a single-actor site (Klonkt etc.) the root
// resolves to its AP actor, so you can follow a site by just its domain.
const s = String(handle || '').trim();
let actorUrl;
if (/^https?:\/\//i.test(s)) actorUrl = safeUrl(s) || null;
else if (s.includes('@')) actorUrl = await webfingerResolve(s);
else if (/^[a-z0-9.-]+\.[a-z]{2,}/i.test(s)) actorUrl = await resolveApActor('https://' + s.replace(/^\/+|\/+$/g, ''));
else actorUrl = null;
if (!actorUrl) return { error: 'not_found' };
const actor = await fetchActor(actorUrl).catch(() => null);
if (!actor || !actor.id || !actor.inbox) return { error: 'unreachable' };
const ai = actorInfo(actor, actor.id);
const me = actorId(base, site.slug);
const keys = getOrCreateKeys(site.slug);
const followId = `${me}#follow-${Date.now()}-${rid()}`;
fwStmts().ins.run(site.slug, actor.id, ai.handle, ai.name, ai.icon, ai.url, actor.inbox, followId, 'pending', autoBoost ? 1 : 0);
const follow = { '@context': AP_CONTEXT, id: followId, type: 'Follow', actor: me, object: actor.id };
// Deliver via the retry queue: a Follow that fails the first attempt (peer down,
// timeout, transient 5xx) is retried with backoff instead of staying stuck on
// 'pending' forever — the Accept can only come back once the Follow lands.
await deliverWithRetry(site.slug, actor.inbox, follow, `${me}#main-key`, keys.private_pem);
console.log('[AP] follow', site.slug, '→', actor.id);
// Follow + feature in one step → backfill their recent posts into the Cirkel right away.
if (autoBoost) backfillFromOutbox(site.slug, actor.id).catch(() => {});
return { ok: true, name: ai.name, handle: ai.handle, actor: actor.id };
}
// Resolve a profile URL or @handle to a followable remote actor (for the
// authorize_interaction "Follow" flow). Returns display fields + inbox, or null
// when it isn't a reachable actor (e.g. the input was a post, not a profile).
export async function resolveRemoteActor(input) {
const s = String(input || '').trim();
const actorUrl = /^https?:\/\//i.test(s) ? (safeUrl(s) || null) : await webfingerResolve(s);
if (!actorUrl) return null;
const actor = await fetchActor(actorUrl).catch(() => null);
if (!actor || !actor.id || !actor.inbox) return null;
const ai = actorInfo(actor, actor.id);
return { actor_uri: actor.id, actor_name: ai.name, actor_handle: ai.handle, actor_url: ai.url, actor_icon: ai.icon, inbox: actor.inbox };
}
export async function unfollowActor(site, actorUri) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
const me = actorId(base, site.slug);
const keys = getOrCreateKeys(site.slug);
const row = fwStmts().one.get(site.slug, actorUri);
// Undo(Follow) MUST reference the original Follow's real id so the remote can correlate it
// and drop the follow. The old `${me}#follow` fallback never matched anything → the unfollow
// silently failed on the remote. With no stored follow id (legacy row), skip the network Undo
// rather than send an unmatchable one. Deliver durably via the retry queue.
if (row && row.inbox && row.follow_id) {
const undo = { '@context': AP_CONTEXT, id: `${me}/undo/${Date.now()}-${rid()}`, type: 'Undo', actor: me, object: { id: row.follow_id, type: 'Follow', actor: me, object: actorUri } };
deliverWithRetry(site.slug, row.inbox, undo, `${me}#main-key`, keys.private_pem);
} else if (row && row.inbox) {
console.warn('[AP] unfollow', site.slug, '→', actorUri, '— no stored follow id; removed locally only (legacy follow, remote may keep it)');
}
fwStmts().del.run(site.slug, actorUri);
return { ok: true };
}
// FEP-633c §5.3 note (authorized fetch): true when `actorUri` is a committed
// guardian of the local ward `wardSlug` — so a signed GET from it may read the
// ward's non-public history without the guardian appearing as a follower.
export function isWardGuardian(wardSlug, actorUri) {
try { return !!Guardianship.getRelation(wardSlug, 'ward', actorUri); } catch { return false; }
}
// FEP-633c §5.3: the guardians approved a gated follow of their ward. Send the
// Accept to the follower and record them, so delivery (incl. followers-only)
// begins. `pending` is a row from ap_pending_follows.
export async function acceptGatedFollow(pending) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
const slug = pending.ward_slug;
const me = actorId(base, slug);
const keys = getOrCreateKeys(slug);
fStmts().ins.run(slug, pending.follower_uri, pending.follower_inbox, pending.follower_shared_inbox, pending.follower_name, pending.follower_handle, pending.follower_icon);
const original = pending.activity_json ? JSON.parse(pending.activity_json) : { type: 'Follow', actor: pending.follower_uri, object: me };
const accept = { '@context': AP_CONTEXT, id: `${me}#accept-${Date.now()}-${rid()}`, type: 'Accept', actor: me, object: original };
await deliverWithRetry(slug, pending.follower_inbox, accept, `${me}#main-key`, keys.private_pem);
const filled = pending.follower_shared_inbox &&
db.prepare('SELECT 1 FROM ap_followers WHERE slug = ? AND shared_inbox = ? AND actor_uri != ? LIMIT 1').get(slug, pending.follower_shared_inbox, pending.follower_uri);
if (!filled) backfillNewFollower(base, slug, pending.follower_shared_inbox || pending.follower_inbox).catch(() => {});
console.log('[AP] gated Follow accepted', pending.follower_uri, '→ ward', slug);
return { ok: true };
}
// The guardians denied the follow: send a Reject so the follower's server clears
// its pending state, then the caller drops the record.
export async function rejectGatedFollow(pending) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
const slug = pending.ward_slug;
const me = actorId(base, slug);
const keys = getOrCreateKeys(slug);
const original = pending.activity_json ? JSON.parse(pending.activity_json) : { type: 'Follow', actor: pending.follower_uri, object: me };
const reject = { '@context': AP_CONTEXT, id: `${me}#reject-${Date.now()}-${rid()}`, type: 'Reject', actor: me, object: original };
if (pending.follower_inbox) await deliverWithRetry(slug, pending.follower_inbox, reject, `${me}#main-key`, keys.private_pem).catch(() => {});
console.log('[AP] gated Follow rejected', pending.follower_uri, '→ ward', slug);
return { ok: true };
}
// ── Cross-instance follow-approval (FEP-633c §5.3, modelled on the guardian
// offer). Inbound: an Offer(Follow) forwarded by a ward to a guardian (leg
// 2), or a guardian's Accept/Reject coming back to the ward (leg 4). ──────
async function handleFollowApprovalInbox(act, slugParam) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
const type = Array.isArray(act.type) ? act.type[0] : act.type;
const actorUri = typeof act.actor === 'string' ? act.actor : (act.actor && act.actor.id);
// Leg 2: I am a guardian; the object is the Follow to approve. The Offer is
// signed by the ward, so act.actor is the ward.
if (type === 'Offer') {
const fo = (act.object && typeof act.object === 'object') ? act.object : null;
const foType = fo && (Array.isArray(fo.type) ? fo.type[0] : fo.type);
if (!fo || foType !== 'Follow') return false;
const followId = fo.id;
const follower = typeof fo.actor === 'string' ? fo.actor : (fo.actor && fo.actor.id);
const wardUri = actorUri;
if (!followId || !follower || !wardUri) return false;
const recips = (Array.isArray(act.to) ? act.to : (act.to ? [act.to] : [])).filter((x) => typeof x === 'string');
if (slugParam) recips.push(actorId(base, slugParam));
let stored = false;
for (const r of new Set(recips)) {
const gslug = slugFromActorUrl(r);
if (!gslug) continue;
if (!Guardianship.getRelation(gslug, 'guardian', wardUri)) continue; // must actually guard this ward
const wardDoc = await fetchActor(wardUri).catch(() => null);
const fai = actorInfo(await fetchActor(follower).catch(() => null), follower);
Guardianship.follows.recordReview(gslug, { id: followId, wardUri, wardInbox: wardDoc && wardDoc.inbox, follower, followerHandle: fai.handle, followerIcon: fai.icon, followJson: JSON.stringify(fo) });
const L = pushLang(gslug);
pushEvent(gslug, { type: 'guardian', title: i18nT(L, 'push.n_guard_cog_t'), body: i18nT(L, 'push.n_guard_cog_b', { who: fai.name || fai.handle || i18nT(L, 'notif.someone') }), url: `${pushPrefix(gslug)}/guardian` });
stored = true;
}
return stored;
}
// Leg 4: I am the ward; a guardian decided. object is the Follow (id).
const fo = act.object;
const followId = typeof fo === 'string' ? fo : (fo && fo.id);
if (!followId) return false;
const pending = Guardianship.follows.getPending(followId);
if (!pending) return false;
const guardians = Guardianship.listGuardians(pending.ward_slug).map((g) => g.other_uri);
if (!guardians.includes(actorUri)) return false; // only a real guardian of this ward decides
const decision = type === 'Reject' ? 'reject' : 'approve';
const r = Guardianship.follows.decide(followId, actorUri, decision, guardians);
try {
if (r.outcome === 'approved') { await acceptGatedFollow(r.follow); Guardianship.follows.remove(followId); }
else if (r.outcome === 'rejected') { await rejectGatedFollow(r.follow); Guardianship.follows.remove(followId); }
} catch { /* delivery is retried */ }
return true;
}
// Leg 3: a guardian in /guardian decides on a forwarded follow; send the
// Accept/Reject back to the ward's inbox (signed by the guardian).
export async function sendFollowDecision(guardianSite, review, decision) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
const me = actorId(base, guardianSite.slug);
const keys = getOrCreateKeys(guardianSite.slug);
const fo = review.follow_json ? JSON.parse(review.follow_json) : { id: review.id, type: 'Follow', actor: review.follower_uri, object: review.ward_uri };
const activity = { '@context': AP_CONTEXT, id: `${me}#followdec-${Date.now()}-${rid()}`, type: decision === 'reject' ? 'Reject' : 'Accept', actor: me, to: [review.ward_uri], object: fo, 'shaer:followApproval': true };
if (review.ward_inbox) await deliverWithRetry(guardianSite.slug, review.ward_inbox, activity, `${me}#main-key`, keys.private_pem);
return { ok: true };
}
// Send a Like or Announce (boost) on a remote note FROM this site.
export async function sendInteraction(site, kind, targetNoteId, authorUri) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
if (!base || !site || !site.slug || !targetNoteId) return { error: 'config' };
const me = actorId(base, site.slug);
const keys = getOrCreateKeys(site.slug);
// 'unboost' = Undo(Announce): retracts a boost so followers' servers remove the
// reblog (matched on actor+object — no record of the original Announce needed).
const fanout = (kind === 'boost' || kind === 'unboost'); // also goes to our followers
const followersCol = `${me}/followers`;
// Address the original author in cc so their server (Mastodon, WordPress/ActivityPub, …)
// attributes the boost to their post and notifies them — without this, a shared-inbox
// receiver has nothing to route the Announce to. Non-fragment activity ids + a `published`
// stamp keep us aligned with what Mastodon emits.
const audience = authorUri ? [followersCol, authorUri] : [followersCol];
let act;
if (kind === 'unboost' || kind === 'unlike') {
// Undo(Announce) retracts a boost; Undo(Like) un-favourites (matched on actor+object,
// no record of the original activity needed — Mastodon honours both).
const inner = kind === 'unboost' ? 'Announce' : 'Like';
act = {
'@context': AP_CONTEXT,
id: `${me}/undo/${Date.now()}-${rid()}`, type: 'Undo', actor: me,
object: { id: `${me}/${inner.toLowerCase()}/${Date.now()}-${rid()}`, type: inner, actor: me, object: targetNoteId },
};
if (kind === 'unboost') { act.to = [PUBLIC]; act.cc = audience; }
} else {
const type = kind === 'boost' ? 'Announce' : 'Like';
act = {
'@context': AP_CONTEXT,
id: `${me}/${type.toLowerCase()}/${Date.now()}-${rid()}`,
type, actor: me, object: targetNoteId,
};
if (type === 'Announce') { act.published = new Date().toISOString(); act.to = [PUBLIC]; act.cc = audience; }
}
const inboxes = new Set();
// Author first, via their PERSONAL inbox (not the shared one) so a multi-user receiver
// routes the Announce/Like to the right post unambiguously.
if (authorUri) { const a = await fetchActor(authorUri).catch(() => null); if (a) inboxes.add(a.inbox || (a.endpoints && a.endpoints.sharedInbox)); }
if (fanout) { for (const f of fStmts().list.all(site.slug)) inboxes.add(f.shared_inbox || f.inbox); }
// Queue each delivery (immediate attempt + backoff retries on failure via ap_delivery)
// instead of a single fire-and-forget POST, so a transient hiccup at the receiver doesn't
// silently lose the boost — same durability a new post (deliverCreate) already gets.
let queued = 0;
for (const inbox of [...inboxes].filter(Boolean)) { deliverWithRetry(site.slug, inbox, act, `${me}#main-key`, keys.private_pem); queued++; }
console.log('[AP]', kind, site.slug, '→', targetNoteId, 'queued', queued, 'inbox(es)');
return { ok: true, delivered: queued };
}
// Notifications inbox: new followers + replies/likes/boosts on this site's posts.
export function getNotifications(slug, limit) {
// Per-source cap scales with the requested limit so Messages can page deep
// (Load more). Bounded so a huge offset can't ask for unbounded rows.
const L = Math.min(1000, Math.max(80, limit || 60));
const out = [];
try {
for (const f of db.prepare('SELECT actor_uri, created_at FROM ap_followers WHERE slug = ? ORDER BY created_at DESC LIMIT ?').all(slug, L)) {
out.push({ type: 'follow', handle: deriveHandle(f.actor_uri), url: f.actor_uri, created_at: f.created_at });
}
} catch { /* ignore */ }
try {
const rows = db.prepare(`
SELECT i.kind, i.actor_name, i.actor_handle, i.actor_url, i.actor_icon, i.content, i.created_at, i.published, i.visibility,
i.emoji_json, i.actor_emoji_json, i.media_json, i.quote_json, i.embed_json,
p.slug AS post_slug, p.title AS post_title
FROM ap_interactions i LEFT JOIN posts p ON p.id = i.post_id
WHERE p.site_id = (SELECT id FROM sites WHERE slug = ?)
ORDER BY i.created_at DESC LIMIT ?
`).all(slug, L);
for (const r of rows) out.push({
type: r.kind, name: r.actor_name, handle: r.actor_handle, url: r.actor_url, icon: r.actor_icon,
content: stripLeadingMentions(r.content), post_slug: r.post_slug, post_title: r.post_title, created_at: r.created_at,
// When the post was written, for display. created_at (when it reached us)
// stays the sort key and the unread watermark: a note that federated late
// is still new to you.
published: r.published,
emoji_json: r.emoji_json, actor_emoji_json: r.actor_emoji_json, // FEP-9098 (messages render)
media_json: r.media_json, quote_json: r.quote_json, embed_json: r.embed_json, // rendered like a Krant post
// followers/direct = a private message to the owner (not on the public thread) → 🔒 in Messages
visibility: r.visibility || 'public',
});
} catch { /* ignore */ }
try {
for (const r of db.prepare('SELECT actor_uri, actor_name, actor_handle, actor_icon, content, objects, created_at FROM ap_reports WHERE slug = ? ORDER BY created_at DESC LIMIT ?').all(slug, L)) {
// The reported objects: our own notes resolve to post links so the owner
// sees WHICH post the report is about; other URIs (e.g. the actor itself)
// are skipped — the report row already names the account.
const about = [];
try {
for (const u of JSON.parse(r.objects || '[]')) {
const m = String(u).match(/\/ap\/notes\/([^/?#]+)/);
if (!m) continue;
const p = db.prepare('SELECT slug, title FROM posts WHERE id = ?').get(decodeURIComponent(m[1]));
if (p) about.push({ slug: p.slug, title: p.title || p.slug });
}
} catch { /* malformed objects json → no links */ }
out.push({ type: 'report', name: r.actor_name, handle: r.actor_handle, url: r.actor_uri, icon: r.actor_icon, content: r.content, objects: about, created_at: r.created_at });
}
} catch { /* ignore */ }
try {
for (const r of db.prepare(`SELECT object_uri, note_url, actor_uri, actor_name, actor_handle, actor_icon, actor_url, content, wave, help_request, created_at, published,
emoji_json, actor_emoji_json, media_json, quote_json, embed_json
FROM ap_mentions WHERE slug = ? ORDER BY created_at DESC LIMIT ?`).all(slug, L)) {
out.push({ type: 'mention', name: r.actor_name, handle: r.actor_handle, url: r.actor_url || r.actor_uri, icon: r.actor_icon, content: stripLeadingMentions(r.content), note_url: r.note_url || r.object_uri, wave: r.wave ? 1 : 0, help_request: r.help_request ? 1 : 0, actorUri: r.actor_uri, created_at: r.created_at, published: r.published,
// Same trimmings a Krant row has, so Berichten renders the post identically.
emoji_json: r.emoji_json, actor_emoji_json: r.actor_emoji_json, media_json: r.media_json, quote_json: r.quote_json, embed_json: r.embed_json });
}
} catch { /* ignore */ }
// Your own polls that have closed → a "results are in" item, derived read-time
// from poll_json (Scheduler marks closed=1) with the tally via ownPollView.
try {
const site = db.prepare('SELECT id FROM sites WHERE slug = ?').get(slug);
if (site) {
const polls = db.prepare(`
SELECT id, slug, title, poll_json FROM posts
WHERE site_id = ? AND poll_json IS NOT NULL
AND json_extract(poll_json, '$.closed') = 1
AND json_extract(poll_json, '$.endTime') IS NOT NULL
ORDER BY json_extract(poll_json, '$.endTime') DESC LIMIT 20`).all(site.id);
for (const p of polls) {
const view = ownPollView(p);
if (!view) continue;
let endTime = null; try { endTime = JSON.parse(p.poll_json).endTime; } catch { /* keep null */ }
out.push({ type: 'poll_done', post_slug: p.slug, post_title: p.title, poll: view, created_at: endTime || null });
}
}
} catch { /* ignore */ }
// NaN-safe sort: one row with a missing/garbled created_at would otherwise make the
// comparator return NaN and scramble the WHOLE ordering (seen live: follow rows landing
// between likes, which also broke Messages' like-grouping).
out.sort((a, b) => _msgTs(b) - _msgTs(a));
return out.slice(0, limit || 60);
}
function _msgTs(x) { const t = Date.parse((x && x.created_at) || ''); return Number.isFinite(t) ? t : 0; }
// ── Blocking / defederation ───────────────────────────────────────
// Extracted to BlocklistService (shared: Klonkt's Block tab + Shaer's "in
// Orbit"). Thin delegations keep every existing caller working.
export function listBlocks(slug) { return Blocklist.listBlocks(slug); }
// True if an actor (or its whole domain) is blocked anywhere on this instance.
// Vote on a remote fediverse poll (a cached Question). A ballot = a Create(Note) carrying only a
// `name` (the chosen option) + inReplyTo the Question, addressed to the poll's author — the
// Mastodon-standard vote. Records our choice locally + optimistically bumps the counts; the
// author's Update(Question) refreshes the authoritative totals when it arrives.
export async function voteOnPoll(site, questionId, choices) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
if (!base || !site || !site.slug || !questionId) return { error: 'config' };
let row; try { row = db.prepare('SELECT author_uri, poll_json FROM ap_timeline WHERE id = ? AND slug = ? LIMIT 1').get(questionId, site.slug); } catch { /* ignore */ }
if (!row || !row.poll_json) return { error: 'not_found' };
let poll; try { poll = JSON.parse(row.poll_json); } catch { return { error: 'not_found' }; }
if (poll.closed) return { error: 'closed' };
if (poll.voted) return { error: 'already' };
const valid = new Set(poll.options.map((o) => o.name));
const picks = (Array.isArray(choices) ? choices : [choices]).map(String).filter((c) => valid.has(c));
if (!picks.length) return { error: 'invalid' };
const chosen = poll.multiple ? [...new Set(picks)] : [picks[0]];
const me = actorId(base, site.slug);
const keys = getOrCreateKeys(site.slug);
const authorUri = row.author_uri || null;
const author = authorUri ? await fetchActor(authorUri).catch(() => null) : null;
const inbox = author && (author.inbox || (author.endpoints && author.endpoints.sharedInbox));
if (!inbox) return { error: 'unreachable' };
for (const name of chosen) {
const nid = `${me}/votes/${Date.now()}-${rid()}`;
const note = { id: nid, type: 'Note', attributedTo: me, to: authorUri ? [authorUri] : [], name, inReplyTo: questionId, published: new Date().toISOString() };
const create = { '@context': AP_CONTEXT, id: `${nid}/activity`, type: 'Create', actor: me, to: note.to, object: note };
deliverWithRetry(site.slug, inbox, create, `${me}#main-key`, keys.private_pem);
}
// Local optimistic update (authoritative counts arrive via the author's Update(Question)).
poll.voted = poll.multiple ? chosen : chosen[0];
for (const o of poll.options) if (chosen.includes(o.name)) o.count = (o.count || 0) + 1;
if (poll.voters != null) poll.voters += 1;
try { db.prepare('UPDATE ap_timeline SET poll_json = ? WHERE id = ? AND slug = ?').run(JSON.stringify(poll), questionId, site.slug); } catch { /* ignore */ }
return { ok: true };
}
// Vote on ANY fediverse poll by URL (the interact page) — no timeline cache needed. Fetches
// the Question fresh, validates the choice(s), and casts the Mastodon-standard ballot (a
// Create(Note) with `name` + inReplyTo) straight to the poll's author. Used for polls you find
// by URL, not just ones from accounts you follow (which go through voteOnPoll via /news).
export async function voteOnRemotePoll(site, questionUrl, choices) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
if (!base || !site || !site.slug || !/^https?:\/\//i.test(String(questionUrl || ''))) return { error: 'config' };
const q = await fetchActor(questionUrl).catch(() => null); // AP GET (SSRF-guarded)
if (!q || q.type !== 'Question' || !q.id) return { error: 'not_found' };
const poll = parsePoll(q);
if (!poll) return { error: 'not_found' };
if (poll.closed) return { error: 'closed' };
const valid = new Set(poll.options.map((o) => o.name));
const picks = (Array.isArray(choices) ? choices : [choices]).map(String).filter((c) => valid.has(c));
if (!picks.length) return { error: 'invalid' };
const chosen = poll.multiple ? [...new Set(picks)] : [picks[0]];
const authorUri = actorUriOf(q.attributedTo);
const author = authorUri ? await fetchActor(authorUri).catch(() => null) : null;
const inbox = author && (author.inbox || (author.endpoints && author.endpoints.sharedInbox));
if (!inbox) return { error: 'unreachable' };
const me = actorId(base, site.slug);
const keys = getOrCreateKeys(site.slug);
for (const name of chosen) {
const nid = `${me}/votes/${Date.now()}-${rid()}`;
const note = { id: nid, type: 'Note', attributedTo: me, to: [authorUri], name, inReplyTo: q.id, published: new Date().toISOString() };
const create = { '@context': AP_CONTEXT, id: `${nid}/activity`, type: 'Create', actor: me, to: note.to, object: note };
deliverWithRetry(site.slug, inbox, create, `${me}#main-key`, keys.private_pem);
}
return { ok: true };
}
// Report a remote post or account to its home instance (moderation). Sends the Mastodon-standard
// AS2 `Flag`: object = [reported account, reported status?], content = the reason, delivered to the
// reported account's inbox so their instance's moderators receive it. objectUri = a post URL (its
// author is resolved + included) OR pass actorUri to report an account directly.
export async function sendReport(site, { objectUri, actorUri, reason }) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
if (!base || !site || !site.slug) return { error: 'config' };
let targetActor = actorUri || null;
let noteUri = null;
if (objectUri && /^https?:\/\//i.test(objectUri)) {
const note = await apGetJson(objectUri).catch(() => null);
if (note && note.id) { noteUri = note.id; if (!targetActor) targetActor = actorUriOf(note.attributedTo); }
else if (!targetActor) return { error: 'not_found' };
}
if (!targetActor || !/^https?:\/\//i.test(targetActor)) return { error: 'not_found' };
const actor = await fetchActor(targetActor).catch(() => null);
const inbox = actor && (actor.inbox || (actor.endpoints && actor.endpoints.sharedInbox)); // personal inbox → their moderators
if (!inbox) return { error: 'unreachable' };
const me = actorId(base, site.slug);
const keys = getOrCreateKeys(site.slug);
const object = [targetActor];
if (noteUri && noteUri !== targetActor) object.push(noteUri);
const flag = {
'@context': AP_CONTEXT,
id: `${me}#report-${Date.now()}-${rid()}`,
type: 'Flag',
actor: me,
content: String(reason == null ? '' : reason).slice(0, 3000),
object, // [account, status?] — Mastodon's Flag shape
to: [targetActor],
};
deliverWithRetry(site.slug, inbox, flag, `${me}#main-key`, keys.private_pem);
return { ok: true };
}
export function isBlockedAny(actorUri) { return Blocklist.isBlockedAny(actorUri); }
// Block an actor (@handle or actor URL) or a whole domain; purges their content.
// The handle resolver is ours; the storage/purge lives in BlocklistService.
export async function blockTarget(site, input) { return Blocklist.blockTarget(site, input, webfingerResolve); }
export function unblock(site, target) { return Blocklist.unblock(site, target); }
// ── Guardianship module wiring (src/services/guardianship/) ────────
// The module owns FEP-633c (context, relations, handshake, queues, the
// direct-note leg); we hand it our AP helpers ONCE and delegate. It never
// imports us back.
function selfActorId(slug) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
return actorId(base, slug);
}
// Deliver one activity to one actor's inbox, signed; queued + retried on any
// hiccup so a slow or briefly-down ward server never loses the offer. Returns
// { delivered, inbox }: delivered=false means the account could not be
// resolved at all (a bad handle) — the offer stays recorded regardless.
export async function deliverToActor(site, actorUri, activity) {
const me = selfActorId(site.slug);
const keys = getOrCreateKeys(site.slug);
const payload = { '@context': AP_CONTEXT, ...activity };
const a = await fetchActor(actorUri).catch(() => null);
const inbox = a && (a.inbox || (a.endpoints && a.endpoints.sharedInbox));
if (!inbox) {
console.warn('[AP] guardianship: could not resolve an inbox for', actorUri, '(offer recorded, not sent)');
return { delivered: false, inbox: null };
}
try {
const st = await deliver(inbox, payload, `${me}#main-key`, keys.private_pem);
if (st >= 200 && st < 300) { console.log('[AP] guardianship', activity.type, 'delivered →', inbox, st); return { delivered: true, inbox }; }
console.warn('[AP] guardianship', activity.type, 'got', st, 'from', inbox, '→ queued for retry');
} catch (e) { console.warn('[AP] guardianship', activity.type, 'to', inbox, 'failed:', e.message, '→ queued for retry'); }
enqueueDelivery(site.slug, inbox, payload);
return { delivered: true, inbox }; // queued: the retry worker gets it there
}
Guardianship.wireDelivery({
actorId, fetchActor, deriveHandle, escHtml, linkUrls, linkHashtags,
getOutboxRow: (id) => iStmts().getO.get(id),
buildReplyNote, AP_CONTEXT, getOrCreateKeys, deliver, enqueueDelivery,
});
// Which local site (if any) hosts this actor URI — used by the handshake to
// apply the local side of a commit and to derive a ward's existing guardians.
function localSlugOf(actorUri) {
const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, '');
if (!actorUri || !actorUri.startsWith(`${base}/ap/users/`)) return null;
const slug = slugFromActorUrl(actorUri);
if (!slug) return null;
try { return db.prepare('SELECT slug FROM sites WHERE slug = ?').get(slug) ? slug : null; }
catch { return null; }
}
Guardianship.wireHandshake({
selfId: selfActorId,
localSlug: localSlugOf,
deliverTo: deliverToActor,
deriveHandle,
fetchActor,
// Guardian PWA / Berichten push. The kid answers an incoming offer in its
// own Berichten; an existing guardian and a commit land in the PWA.
onEvent: (slug, ev) => {
const L = pushLang(slug);
const texts = {
offer_received: ['push.n_guard_offer_t', 'push.n_guard_offer_b'], // I am the ward
offer_for_ward: ['push.n_guard_cog_t', 'push.n_guard_cog_b'], // I co-guard this ward
committed: ['push.n_guard_ward_t', 'push.n_guard_ward_b'],
}[ev.kind];
if (!texts) return;
const who = deriveHandle(ev.candidate || ev.ward || ev.guardian || '') || '?';
const url = ev.kind === 'offer_received' ? `${pushPrefix(slug)}/messages` : '/guardian';
pushEvent(slug, { type: 'guardian', title: i18nT(L, texts[0]), body: i18nT(L, texts[1], { who }), url });
},
});
export default {
AP_CONTEXT, getOrCreateKeys, apWants, sendAP, actorId, noteId,
buildActor, buildNote, buildCreate, buildOutbox, buildFollowers, buildFollowing, buildFeatured,
followerCount, deliver, fetchActor, verifyRequest, handleInbox, deliverCreate, deliverDelete, deliverUpdate, deliverActorUpdate, resyncFeaturedPins,
getInteractions, getInteractionById, setInteractionBoosted, setInteractionLiked, setMyReaction, getMyReactions, buildReplyNote, getOutboxNote, deliverReply, resolveRemoteNote,
listOutbox, deliverOutboxDelete, deliverOutboxUpdate, deliverDirectNote,
webfingerResolve, followActor, resolveRemoteActor, unfollowActor, listFollowing, setAutoBoost, backfillFromOutbox, getTimeline, timelineAttachments, timelineEmojis, timelineObjectLinks, timelineQuote, timelineEmbed, applyQuoteProps, deliverToActor, sendInteraction, voteOnPoll, voteOnRemotePoll,
acceptGatedFollow, rejectGatedFollow, isWardGuardian, sendFollowDecision,
parseOwnPoll, pollTally, ownPollView, deliverPollUpdate, maybeCrawlThread, sendReport, localMentionSlugs,
autoBoostCount, boostedCount, markBoosted, unmarkBoosted, markLiked, unmarkLiked, getTimelineReaction, upsertBoostedNote, getCirkelPosts, getCirkelMembers, selfHealTimeline,
getNotifications, listBlocks, isBlockedAny, blockTarget, unblock,
deliverWithRetry, enqueueDelivery, processDeliveryQueue, startDeliveryWorker,
getReplyUris, markNotificationsSeen, countUnseenNotifications, hasPlayableAudio,
linkifyBody, bakePostContent, bakePostContentWithMentions, listFollowers, removeFollower, listConnections,
noteVisibility, belongsInTimeline, isRejectedObject, rejectInteraction, interactionReportTarget,
getMessages, notificationsSeenAt, ingestOutboxActivity, c2sVisibility, actorDisplay, buildActorRef, prefersEnriched,
};