From 7df5cabb7685ea23c136d8b8e333b12fa3b40107 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Fri, 22 Mar 2024 14:39:18 -0400 Subject: [PATCH] feat: have category actor send Announce(Note) activity on posts from that cid re: #12434 --- src/activitypub/notes.js | 8 ++++++++ src/api/activitypub.js | 25 ++++++++++++++++++------- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/src/activitypub/notes.js b/src/activitypub/notes.js index 51ad12f45c17..ef53e813d3cc 100644 --- a/src/activitypub/notes.js +++ b/src/activitypub/notes.js @@ -254,3 +254,11 @@ Notes.syncUserInboxes = async function (tid) { winston.verbose(`[activitypub/syncUserInboxes] Syncing tid ${tid} with ${uids.size} inboxes`); await db.sortedSetsAdd(keys, keys.map(() => score || Date.now()), tid); }; + +Notes.getCategoryFollowers = async (cid) => { + // Retrieves remote users who have followed a category; used to build recipient list + let uids = await db.getSortedSetRangeByScore(`cid:${cid}:uid:watch:state`, 0, -1, categories.watchStates.tracking, categories.watchStates.tracking); + uids = uids.filter(uid => !utils.isNumber(uid)); + + return uids; +}; diff --git a/src/api/activitypub.js b/src/api/activitypub.js index 8edbe3eb24aa..bd8d700908fd 100644 --- a/src/api/activitypub.js +++ b/src/api/activitypub.js @@ -104,15 +104,26 @@ activitypubApi.create.post = enabledCheck(async (caller, { pid }) => { const object = await activitypub.mocks.note(post); const { targets } = await buildRecipients(object, post.user.uid); - - const payload = { - type: 'Create', - to: object.to, - cc: object.cc, - object, + const { cid } = post.category; + const followers = await activitypub.notes.getCategoryFollowers(cid); + + const payloads = { + create: { + type: 'Create', + to: object.to, + cc: object.cc, + object, + }, + announce: { + type: 'Announce', + to: [`${nconf.get('url')}/category/${cid}/followers`], + cc: [activitypub._constants.publicAddress], + object, + }, }; - await activitypub.send('uid', caller.uid, Array.from(targets), payload); + await activitypub.send('uid', caller.uid, Array.from(targets), payloads.create); + await activitypub.send('cid', cid, followers, payloads.announce); }); activitypubApi.update = {};