Changeset 7d932ce in Klonkt for src/services
- Timestamp:
- 06/24/2026 01:24:23 PM (3 months ago)
- Branches:
- main
- Children:
- a885afc
- Parents:
- cc24fa1
- Location:
- src/services
- Files:
-
- 2 edited
-
ActivityPubService.js (modified) (8 diffs)
-
i18n.js (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/services/ActivityPubService.js
rcc24fa1 r7d932ce 198 198 function iStmts() { 199 199 if (!_insI) { 200 _insI = db.prepare('INSERT OR IGNORE INTO ap_interactions (kind, post_id, object_uri, actor_uri, actor_name, actor_handle, actor_url, actor_icon, content, published, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)');200 _insI = db.prepare('INSERT OR IGNORE INTO ap_interactions (kind, post_id, object_uri, actor_uri, actor_name, actor_handle, actor_url, actor_icon, content, published, parent_uri, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)'); 201 201 _delLA = db.prepare('DELETE FROM ap_interactions WHERE kind = ? AND post_id = ? AND actor_uri = ?'); 202 202 _delReply = db.prepare("DELETE FROM ap_interactions WHERE kind = 'reply' AND object_uri = ?"); 203 _listI = db.prepare('SELECT id, kind, object_uri, actor_uri, actor_name, actor_handle, actor_url, actor_icon, content, published, created_at FROM ap_interactions WHERE post_id = ? ORDER BY created_at ASC');203 _listI = db.prepare('SELECT id, kind, object_uri, parent_uri, actor_uri, actor_name, actor_handle, actor_url, actor_icon, content, published, created_at FROM ap_interactions WHERE post_id = ? ORDER BY created_at ASC'); 204 204 _getI = db.prepare('SELECT * FROM ap_interactions WHERE id = ?'); 205 205 _insO = db.prepare('INSERT INTO ap_outbox (id, site_slug, post_id, post_slug, in_reply_to, to_actor, to_handle, content, created_at) VALUES (?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)'); … … 235 235 } 236 236 237 // Stored, view-ready summary of a post's inbound fediverse activity + our replies. 238 export function getInteractions(postId) { 237 // Given an inReplyTo note URL, find which local post the thread belongs to + the 238 // note being replied to (parent), so a reply-to-a-comment can be nested. 239 function findThreadTarget(inReplyTo, base) { 240 if (!inReplyTo) return null; 241 const seg = postIdFromNoteUrl(inReplyTo, base); // our /ap/notes/<id> segment (if ours) 242 if (seg && localPostExists(seg)) return { post_id: seg, parent_uri: inReplyTo }; 243 if (seg) { 244 try { const o = db.prepare('SELECT post_id FROM ap_outbox WHERE id = ?').get(seg); if (o && o.post_id) return { post_id: o.post_id, parent_uri: inReplyTo }; } catch { /* ignore */ } 245 } 246 try { const row = db.prepare("SELECT post_id FROM ap_interactions WHERE object_uri = ? AND kind = 'reply' LIMIT 1").get(inReplyTo); if (row && row.post_id) return { post_id: row.post_id, parent_uri: inReplyTo }; } catch { /* ignore */ } 247 return null; 248 } 249 250 // View-ready threaded view of a post's fediverse activity (inbound replies + 251 // our outbound replies, nested), plus like/boost counts. 252 export function getInteractions(postId, base) { 239 253 const s = iStmts(); 240 254 const rows = s.list.all(postId); 241 const outReplies = s.listO.all(postId).map((o) => ({ 242 id: o.id, content: o.content, in_reply_to: o.in_reply_to, to_handle: o.to_handle, 243 created_at: o.created_at, mine: true, 244 })); 255 const baseClean = (base || process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); 256 const postNoteId = baseClean ? `${baseClean}/ap/notes/${postId}` : null; 257 258 const nodes = []; 259 for (const r of rows) { 260 if (r.kind !== 'reply') continue; 261 nodes.push({ 262 noteId: r.object_uri, parent: r.parent_uri || null, mine: false, 263 actor_name: r.actor_name, actor_handle: r.actor_handle, actor_url: r.actor_url, 264 actor_icon: r.actor_icon, content: r.content, created_at: r.published || r.created_at, 265 children: [], 266 }); 267 } 268 for (const o of s.listO.all(postId)) { 269 nodes.push({ 270 noteId: baseClean ? `${baseClean}/ap/notes/${o.id}` : o.id, parent: o.in_reply_to || null, 271 mine: true, outboxId: o.id, content: o.content, created_at: o.created_at, children: [], 272 }); 273 } 274 275 const byId = new Map(nodes.map((n) => [n.noteId, n])); 276 const isTop = (n) => !n.parent || n.parent === postNoteId || !byId.has(n.parent); 277 const tops = []; 278 for (const n of nodes) { 279 if (isTop(n)) { tops.push(n); continue; } 280 let anc = n, guard = 0; 281 while (!isTop(anc) && guard++ < 12) anc = byId.get(anc.parent); 282 anc.children.push(n); 283 } 284 const byTime = (a, b) => new Date(a.created_at) - new Date(b.created_at); 285 tops.sort(byTime).forEach((t) => t.children.sort(byTime)); 286 245 287 return { 246 replies: rows.filter((r) => r.kind === 'reply'), 247 outReplies, 288 thread: tops, 248 289 likeCount: rows.filter((r) => r.kind === 'like').length, 249 290 announceCount: rows.filter((r) => r.kind === 'announce').length, 250 total: rows.length + outReplies.length,291 total: nodes.length, 251 292 }; 252 293 } … … 346 387 const resolveActor = async (uri) => ((verified && verified.id === uri) ? verified : await fetchActor(uri).catch(() => null)); 347 388 348 // Inbound reply: a Create whose object replies to one of our notes .389 // Inbound reply: a Create whose object replies to one of our notes (post OR comment). 349 390 if (type === 'Create' && act.object && (act.object.type === 'Note' || act.object.type === 'Article')) { 350 391 const o = act.object; 351 const pid = postIdFromNoteUrl(o.inReplyTo, base);352 if ( pid && actorUri && localPostExists(pid)) {392 const tgt = findThreadTarget(o.inReplyTo, base); 393 if (tgt && actorUri) { 353 394 const ai = actorInfo(await resolveActor(actorUri), actorUri); 354 395 const html = HtmlSanitizerService.sanitize(o.content || ''); 355 iStmts().ins.run('reply', pid, o.id || '', actorUri, ai.name, ai.handle, ai.url, ai.icon, html, o.published || null);356 console.log('[AP] reply', actorUri, '→', pid);396 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); 397 console.log('[AP] reply', actorUri, '→', tgt.post_id); 357 398 } 358 399 return 202; … … 363 404 if (pid && actorUri && localPostExists(pid)) { 364 405 const ai = actorInfo(await resolveActor(actorUri), actorUri); 365 iStmts().ins.run(type.toLowerCase(), pid, '', actorUri, ai.name, ai.handle, ai.url, ai.icon, null, null );406 iStmts().ins.run(type.toLowerCase(), pid, '', actorUri, ai.name, ai.handle, ai.url, ai.icon, null, null, null); 366 407 console.log('[AP]', type === 'Like' ? 'like' : 'boost', actorUri, '→', pid); 367 408 } … … 475 516 if (a) inboxes.add((a.endpoints && a.endpoints.sharedInbox) || a.inbox); 476 517 } 518 if (parent.threadInbox) inboxes.add(parent.threadInbox); // post author's server (nesting) 477 519 for (const f of fStmts().list.all(site.slug)) inboxes.add(f.shared_inbox || f.inbox); 478 520 let delivered = 0; … … 495 537 const actor = await fetchActor(actorUri).catch(() => null); 496 538 const ai = actorInfo(actor, actorUri); 539 // If this note is itself a reply (a comment), also reach the original post's 540 // author so THEIR server threads our reply under the comment. 541 let threadInbox = null; 542 if (note.inReplyTo) { 543 const parentUrl = typeof note.inReplyTo === 'string' ? note.inReplyTo : (note.inReplyTo && note.inReplyTo.id); 544 const parentNote = parentUrl ? await fetchActor(parentUrl).catch(() => null) : null; 545 const pAtt = parentNote && (typeof parentNote.attributedTo === 'string' ? parentNote.attributedTo : (parentNote.attributedTo && parentNote.attributedTo.id)); 546 if (pAtt && pAtt !== actorUri) { 547 const pa = await fetchActor(pAtt).catch(() => null); 548 threadInbox = pa && ((pa.endpoints && pa.endpoints.sharedInbox) || pa.inbox); 549 } 550 } 497 551 const rawHtml = String(note.content || '').replace(/\[\[(track|album|playlist):[^\]]+\]\]/gi, ''); 498 552 const images = (Array.isArray(note.attachment) ? note.attachment : []) … … 509 563 content: HtmlSanitizerService.sanitize(rawHtml), // full, sanitized 510 564 images, 565 threadInbox, // post author's inbox (if a comment) 511 566 preview: HtmlSanitizerService.toPlainText(note.content || '').slice(0, 240), 512 567 }; 568 } 569 570 // List a site's own outbound fediverse replies (for the manage/delete view). 571 export function listOutbox(siteSlug) { 572 return db.prepare('SELECT id, content, to_handle, in_reply_to, created_at FROM ap_outbox WHERE site_slug = ? ORDER BY created_at DESC').all(siteSlug); 573 } 574 575 // Delete one of our outbound replies: send Delete(Tombstone) to recipients + remove it. 576 export async function deliverOutboxDelete(site, outboxId) { 577 const row = iStmts().getO.get(outboxId); 578 if (!row || row.site_slug !== site.slug) return false; 579 const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); 580 if (base) { 581 const me = actorId(base, site.slug); 582 const nid = noteId(base, row.id); 583 const del = { '@context': 'https://www.w3.org/ns/activitystreams', id: `${nid}#delete-${Date.now()}`, type: 'Delete', actor: me, to: [PUBLIC], object: { id: nid, type: 'Tombstone' } }; 584 const keys = getOrCreateKeys(site.slug); 585 const inboxes = new Set(); 586 if (row.to_actor) { const a = await fetchActor(row.to_actor).catch(() => null); if (a) inboxes.add((a.endpoints && a.endpoints.sharedInbox) || a.inbox); } 587 for (const f of fStmts().list.all(site.slug)) inboxes.add(f.shared_inbox || f.inbox); 588 for (const inbox of [...inboxes].filter(Boolean)) { try { await deliver(inbox, del, `${me}#main-key`, keys.private_pem); } catch { /* best-effort */ } } 589 } 590 db.prepare('DELETE FROM ap_outbox WHERE id = ?').run(outboxId); 591 return true; 513 592 } 514 593 … … 518 597 followerCount, deliver, fetchActor, verifyRequest, handleInbox, deliverCreate, deliverDelete, 519 598 getInteractions, getInteractionById, buildReplyNote, getOutboxNote, deliverReply, resolveRemoteNote, 599 listOutbox, deliverOutboxDelete, 520 600 }; -
src/services/i18n.js
rcc24fa1 r7d932ce 109 109 'fedi.heading': 'Vanuit de fediverse', 'fedi.likes': 'sterren', 'fedi.boosts': 'boosts', 'fedi.replies': 'Reacties uit de fediverse', 110 110 'fedi.reply': 'Reageer', 'fedi.reply_ph': 'Je antwoord aan de fediverse…', 'fedi.send': 'Versturen', 'fedi.you': 'Jij', 111 'fedi.remote_title': 'Reageer via de fediverse', 'fedi.remote_reply': 'Reageer via de fediverse', 'fedi.remote_prompt': 'Je fediverse-adres (bv. @jij@mastodon.social):', 'fedi.remote_notfound': 'Kon die post niet ophalen. Plak de volledige post-URL:', 'fedi.remote_load': 'Ophalen', 'fedi.remote_replying_to': 'Je reageert op', 'fedi.remote_as': 'Wordt verzonden als {site}.', 'fedi.remote_view_original': 'Bekijk de hele post + reacties op de bron →', 'fedi.remote_reply_short': 'via de fediverse', 'fedi.remote_sent_title': 'Verzonden ✅', 'fedi.remote_sent': 'Je reactie is onderweg naar de fediverse en verschijnt zo bij de ontvanger.', 'fedi.remote_back': '← Terug naar je site', 111 'fedi.remote_title': 'Reageer via de fediverse', 'fedi.remote_reply': 'Reageer via de fediverse', 'fedi.remote_prompt': 'Je fediverse-adres (bv. @jij@mastodon.social):', 'fedi.remote_notfound': 'Kon die post niet ophalen. Plak de volledige post-URL:', 'fedi.remote_load': 'Ophalen', 'fedi.remote_replying_to': 'Je reageert op', 'fedi.remote_as': 'Wordt verzonden als {site}.', 'fedi.remote_view_original': 'Bekijk de hele post + reacties op de bron →', 'fedi.remote_reply_short': 'via de fediverse', 'fedi.remote_sent_title': 'Verzonden ✅', 'fedi.remote_sent': 'Je reactie is onderweg naar de fediverse en verschijnt zo bij de ontvanger.', 'fedi.remote_back': '← Terug naar je site', 'fedi.delete_confirm': 'Deze reactie verwijderen?', 'fedi.manage_title': 'Mijn fediverse-reacties', 'fedi.manage_empty': 'Je hebt nog geen reacties verstuurd.', 112 112 'comments.to_start': 'om de conversatie te starten.', 113 113 'comments.reply': 'Reageer', 'comments.delete': 'Verwijder', 'comments.cancel': 'Annuleren', … … 1104 1104 'fedi.heading': 'From the fediverse', 'fedi.likes': 'favourites', 'fedi.boosts': 'boosts', 'fedi.replies': 'Replies from the fediverse', 1105 1105 'fedi.reply': 'Reply', 'fedi.reply_ph': 'Your reply to the fediverse…', 'fedi.send': 'Send', 'fedi.you': 'You', 1106 'fedi.remote_title': 'Reply via the fediverse', 'fedi.remote_reply': 'Reply via the fediverse', 'fedi.remote_prompt': 'Your fediverse address (e.g. @you@mastodon.social):', 'fedi.remote_notfound': 'Could not fetch that post. Paste the full post URL:', 'fedi.remote_load': 'Fetch', 'fedi.remote_replying_to': 'Replying to', 'fedi.remote_as': 'Sent as {site}.', 'fedi.remote_view_original': 'View the full post + comments on the source →', 'fedi.remote_reply_short': 'via the fediverse', 'fedi.remote_sent_title': 'Sent ✅', 'fedi.remote_sent': 'Your reply is on its way to the fediverse and will appear for the recipient shortly.', 'fedi.remote_back': '← Back to your site', 1106 'fedi.remote_title': 'Reply via the fediverse', 'fedi.remote_reply': 'Reply via the fediverse', 'fedi.remote_prompt': 'Your fediverse address (e.g. @you@mastodon.social):', 'fedi.remote_notfound': 'Could not fetch that post. Paste the full post URL:', 'fedi.remote_load': 'Fetch', 'fedi.remote_replying_to': 'Replying to', 'fedi.remote_as': 'Sent as {site}.', 'fedi.remote_view_original': 'View the full post + comments on the source →', 'fedi.remote_reply_short': 'via the fediverse', 'fedi.remote_sent_title': 'Sent ✅', 'fedi.remote_sent': 'Your reply is on its way to the fediverse and will appear for the recipient shortly.', 'fedi.remote_back': '← Back to your site', 'fedi.delete_confirm': 'Delete this reply?', 'fedi.manage_title': 'My fediverse replies', 'fedi.manage_empty': 'You have not sent any replies yet.', 1107 1107 'comments.to_start': 'to start the conversation.', 1108 1108 'comments.reply': 'Reply', 'comments.delete': 'Delete', 'comments.cancel': 'Cancel', … … 2097 2097 'fedi.heading': 'Aus dem Fediverse', 'fedi.likes': 'Favoriten', 'fedi.boosts': 'Boosts', 'fedi.replies': 'Antworten aus dem Fediverse', 2098 2098 'fedi.reply': 'Antworten', 'fedi.reply_ph': 'Deine Antwort an das Fediverse…', 'fedi.send': 'Senden', 'fedi.you': 'Du', 2099 'fedi.remote_title': 'Über das Fediverse antworten', 'fedi.remote_reply': 'Über das Fediverse antworten', 'fedi.remote_prompt': 'Deine Fediverse-Adresse (z.B. @du@mastodon.social):', 'fedi.remote_notfound': 'Beitrag konnte nicht geladen werden. Füge die vollständige Beitrags-URL ein:', 'fedi.remote_load': 'Laden', 'fedi.remote_replying_to': 'Antwort an', 'fedi.remote_as': 'Wird als {site} gesendet.', 'fedi.remote_view_original': 'Ganzen Beitrag + Kommentare an der Quelle ansehen →', 'fedi.remote_reply_short': 'übers Fediverse', 'fedi.remote_sent_title': 'Gesendet ✅', 'fedi.remote_sent': 'Deine Antwort ist auf dem Weg ins Fediverse und erscheint gleich beim Empfänger.', 'fedi.remote_back': '← Zurück zu deiner Seite', 2099 'fedi.remote_title': 'Über das Fediverse antworten', 'fedi.remote_reply': 'Über das Fediverse antworten', 'fedi.remote_prompt': 'Deine Fediverse-Adresse (z.B. @du@mastodon.social):', 'fedi.remote_notfound': 'Beitrag konnte nicht geladen werden. Füge die vollständige Beitrags-URL ein:', 'fedi.remote_load': 'Laden', 'fedi.remote_replying_to': 'Antwort an', 'fedi.remote_as': 'Wird als {site} gesendet.', 'fedi.remote_view_original': 'Ganzen Beitrag + Kommentare an der Quelle ansehen →', 'fedi.remote_reply_short': 'übers Fediverse', 'fedi.remote_sent_title': 'Gesendet ✅', 'fedi.remote_sent': 'Deine Antwort ist auf dem Weg ins Fediverse und erscheint gleich beim Empfänger.', 'fedi.remote_back': '← Zurück zu deiner Seite', 'fedi.delete_confirm': 'Diese Antwort löschen?', 'fedi.manage_title': 'Meine Fediverse-Antworten', 'fedi.manage_empty': 'Du hast noch keine Antworten gesendet.', 2100 2100 'comments.to_start': 'um das Gespräch zu starten.', 2101 2101 'comments.reply': 'Antworten', 'comments.delete': 'Löschen', 'comments.cancel': 'Abbrechen',
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)