Node.js based forum software built for the modern web
feat(activitypub): add staged delivery for topic replies
Julian Lamc30039336cdd
diff --git a/src/activitypub/index.js b/src/activitypub/index.jsindex 1fea8a133b..a4e199cc18 100644--- a/src/activitypub/index.js+++ b/src/activitypub/index.js@@ -387,7 +387,7 @@ ActivityPub.get = async (type, id, uri, options) => {}};-ActivityPub.send = async (type, id, targets, payload) => {+ActivityPub.send = async (type, id, targets, payload, { delayMs = 0 } = {}) => {if (!meta.config.activitypubEnabled) {return ActivityPub.helpers.log('[activitypub/send] Federation not enabled; not sending.');}@@ -420,10 +420,10 @@ ActivityPub.send = async (type, id, targets, payload) => {setImmediate(() => {const retryQueueAdd = [];const retryQueuedSet = [];+ const nextTryOn = Date.now() + (delayMs || 0);inboxes.forEach((uri) => {const queueId = createHash('sha256').update(`${type}:${id}:${uri}`).digest('hex');- const nextTryOn = Date.now();retryQueueAdd.push(['ap:retry:queue', nextTryOn, queueId]);retryQueuedSet.push([`ap:retry:queue:${queueId}`, {queueId,@@ -440,11 +440,11 @@ ActivityPub.send = async (type, id, targets, payload) => {if (retryQueueAdd.length) {db.sortedSetAddBulk(retryQueueAdd);db.setObjectBulk(retryQueuedSet);- }- // Start the drain loop if not already draining- if (SendPool.pool > 0) {- SendPool.drainLoop();+ // Start the drain loop if not already draining+ if (SendPool.pool > 0) {+ SendPool.drainLoop();+ }}});};diff --git a/src/activitypub/out.js b/src/activitypub/out.jsindex ccf10840ec..cfc0f2ecc9 100644--- a/src/activitypub/out.js+++ b/src/activitypub/out.js@@ -91,12 +91,34 @@ Out.create.note = enabledCheck(async (uid, post) => {}const { activity, targets } = await activitypub.mocks.activities.create(pid, uid, post);-- await Promise.all([- activitypub.send('uid', uid, Array.from(targets), activity),+ const targetsArr = Array.from(targets);++ // Separate topic participants (immediate) from the rest (deferred)+ const { tid } = await topics.getTopicFields(pid, ['tid']);+ const participantUids = await db.getSortedSetMembers(`tid:${tid}:posters`);+ const participantRemoteUris = participantUids.filter(uid => !utils.isNumber(uid));+ const immediateTargetsSet = new Set(+ participantRemoteUris.length+ ? targetsArr.filter(uri => participantRemoteUris.includes(uri))+ : [],+ );+ const deferredTargets = targetsArr.filter(uri => !immediateTargetsSet.has(uri));++ const sendPromises = [activitypub.feps.announce(pid, activity),- // utils.isNumber(post.cid) ? activitypubApi.add(caller, { pid }) : undefined,- ]);+ ];+ if (immediateTargetsSet.size) {+ sendPromises.push(activitypub.send('uid', uid, Array.from(immediateTargetsSet), activity, {+ delayMs: 0,+ }));+ }+ // Always send to deferred targets (even if empty) to match original behavior+ // where send() was always called with all targets.+ sendPromises.push(activitypub.send('uid', uid, deferredTargets, activity, {+ delayMs: 10000,+ }));++ await Promise.all(sendPromises);});Out.create.privateNote = enabledCheck(async (messageObj) => {