Changeset 914eb9f in Klonkt for src/services/ActivityPubService.js
- Timestamp:
- 06/24/2026 03:19:11 PM (3 months ago)
- Branches:
- main
- Children:
- d988fa0
- Parents:
- 0dba092
- File:
-
- 1 edited
-
src/services/ActivityPubService.js (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/services/ActivityPubService.js
r0dba092 r914eb9f 406 406 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); 407 407 console.log('[AP] reply', actorUri, '→', tgt.post_id); 408 return 202; 409 } 410 // Home timeline (client): a top-level post from an account we follow. 411 if (actorUri && !isLocalActor && !o.inReplyTo && o.id) { 412 let subs = []; try { subs = db.prepare('SELECT slug FROM ap_following WHERE actor_uri = ?').all(actorUri); } catch { /* table may not exist yet */ } 413 if (subs.length) { 414 const ai = actorInfo(await resolveActor(actorUri), actorUri); 415 const html = HtmlSanitizerService.sanitize(o.content || ''); 416 const media = JSON.stringify((Array.isArray(o.attachment) ? o.attachment : []).filter((a) => a && a.url).map((a) => ({ url: a.url, type: a.mediaType || '' }))); 417 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); 418 console.log('[AP] timeline +', actorUri, 'x' + subs.length); 419 } 408 420 } 409 421 return 202; … … 420 432 } 421 433 if (type === 'Delete') { 422 // A remote reply was deleted upstream → drop it if we stored it.434 // A remote note was deleted upstream → drop it from replies AND the timeline. 423 435 const oid = typeof act.object === 'string' ? act.object : (act.object && act.object.id); 424 if (oid) iStmts().delReply.run(oid); 436 if (oid) { iStmts().delReply.run(oid); try { tlStmts().del.run(oid); } catch { /* ignore */ } } 437 return 202; 438 } 439 // Accept/Reject of a Follow WE sent (client side). 440 if (type === 'Accept' && act.object) { 441 const fid = typeof act.object === 'string' ? act.object : (act.object && act.object.id); 442 if (fid) { try { fwStmts().acc.run(fid); } catch { /* ignore */ } } 443 console.log('[AP] follow accepted', actorUri); 444 return 202; 445 } 446 if (type === 'Reject' && act.object) { 447 const who = actorUri; 448 if (who && slugParam) { try { fwStmts().del.run(slugParam, who); } catch { /* ignore */ } } 425 449 return 202; 426 450 } … … 616 640 } 617 641 642 // ── Fediverse CLIENT: follow accounts + home timeline ───────────── 643 // Resolve an @user@domain handle to its actor URL via WebFinger. 644 export async function webfingerResolve(handle) { 645 const h = String(handle || '').trim().replace(/^@/, ''); 646 const parts = h.split('@'); 647 if (parts.length !== 2 || !parts[0] || !parts[1]) return null; 648 const acct = `${parts[0]}@${parts[1]}`; 649 try { 650 const r = await fetch(`https://${parts[1]}/.well-known/webfinger?resource=acct:${encodeURIComponent(acct)}`, 651 { headers: { Accept: 'application/jrd+json, application/json' }, redirect: 'follow', signal: AbortSignal.timeout(8000) }); 652 if (!r.ok) return null; 653 const jrd = await r.json(); 654 const link = (jrd.links || []).find((l) => l.rel === 'self' && /activity\+json|ld\+json/.test(l.type || '')); 655 return link ? link.href : null; 656 } catch { return null; } 657 } 658 659 let _insFw, _delFw, _listFw, _accFw, _oneFw; 660 function fwStmts() { 661 if (!_insFw) { 662 _insFw = db.prepare('INSERT OR REPLACE INTO ap_following (slug, actor_uri, handle, name, icon, url, inbox, follow_id, status, created_at) VALUES (?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)'); 663 _delFw = db.prepare('DELETE FROM ap_following WHERE slug = ? AND actor_uri = ?'); 664 _listFw = db.prepare('SELECT * FROM ap_following WHERE slug = ? ORDER BY created_at DESC'); 665 _accFw = db.prepare("UPDATE ap_following SET status = 'accepted' WHERE follow_id = ?"); 666 _oneFw = db.prepare('SELECT * FROM ap_following WHERE slug = ? AND actor_uri = ?'); 667 } 668 return { ins: _insFw, del: _delFw, list: _listFw, acc: _accFw, one: _oneFw }; 669 } 670 export function listFollowing(slug) { return fwStmts().list.all(slug); } 671 672 let _insTl, _listTl, _delTl; 673 function tlStmts() { 674 if (!_insTl) { 675 _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, created_at) VALUES (?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP)'); 676 _listTl = db.prepare('SELECT * FROM ap_timeline WHERE slug = ? ORDER BY COALESCE(published, created_at) DESC LIMIT ?'); 677 _delTl = db.prepare('DELETE FROM ap_timeline WHERE id = ?'); 678 } 679 return { ins: _insTl, list: _listTl, del: _delTl }; 680 } 681 export function getTimeline(slug, limit) { return tlStmts().list.all(slug, limit || 50); } 682 683 // Follow a fediverse account by @handle (WebFinger → actor → signed Follow). 684 export async function followActor(site, handle) { 685 const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); 686 if (!base || !site || !site.slug) return { error: 'config' }; 687 const actorUrl = await webfingerResolve(handle); 688 if (!actorUrl) return { error: 'not_found' }; 689 const actor = await fetchActor(actorUrl).catch(() => null); 690 if (!actor || !actor.id || !actor.inbox) return { error: 'unreachable' }; 691 const ai = actorInfo(actor, actor.id); 692 const me = actorId(base, site.slug); 693 const keys = getOrCreateKeys(site.slug); 694 const followId = `${me}#follow-${Date.now()}`; 695 fwStmts().ins.run(site.slug, actor.id, ai.handle, ai.name, ai.icon, ai.url, actor.inbox, followId, 'pending'); 696 const follow = { '@context': 'https://www.w3.org/ns/activitystreams', id: followId, type: 'Follow', actor: me, object: actor.id }; 697 try { await deliver(actor.inbox, follow, `${me}#main-key`, keys.private_pem); } 698 catch (e) { console.warn('[AP] follow deliver failed:', e.message); } 699 console.log('[AP] follow', site.slug, '→', actor.id); 700 return { ok: true, name: ai.name, handle: ai.handle }; 701 } 702 703 export async function unfollowActor(site, actorUri) { 704 const base = (process.env.PUBLIC_BASE_URL || '').replace(/\/+$/, ''); 705 const me = actorId(base, site.slug); 706 const keys = getOrCreateKeys(site.slug); 707 const row = fwStmts().one.get(site.slug, actorUri); 708 if (row && row.inbox) { 709 const undo = { '@context': 'https://www.w3.org/ns/activitystreams', id: `${me}#unfollow-${Date.now()}`, type: 'Undo', actor: me, object: { id: row.follow_id || `${me}#follow`, type: 'Follow', actor: me, object: actorUri } }; 710 try { await deliver(row.inbox, undo, `${me}#main-key`, keys.private_pem); } catch { /* best-effort */ } 711 } 712 fwStmts().del.run(site.slug, actorUri); 713 return { ok: true }; 714 } 715 618 716 export default { 619 717 getOrCreateKeys, apWants, sendAP, actorId, noteId, … … 622 720 getInteractions, getInteractionById, buildReplyNote, getOutboxNote, deliverReply, resolveRemoteNote, 623 721 listOutbox, deliverOutboxDelete, 722 webfingerResolve, followActor, unfollowActor, listFollowing, getTimeline, 624 723 };
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)