From da085b0ece4bede414c73ebc93491093074b4fe9 Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Mon, 12 Feb 2024 14:32:55 -0500 Subject: [PATCH] feat: save actor follower URL backreference and sorted set backreference --- src/activitypub/actors.js | 25 ++++++++++++++++++------- src/activitypub/inbox.js | 1 + 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/src/activitypub/actors.js b/src/activitypub/actors.js index 5a67012c22..61c3c546ad 100644 --- a/src/activitypub/actors.js +++ b/src/activitypub/actors.js @@ -28,6 +28,7 @@ Actors.assert = async (ids, options = {}) => { return true; } + const followersUrlMap = new Map(); const actors = await Promise.all(ids.map(async (id) => { try { const actor = (typeof id === 'object' && id.hasOwnProperty('id')) ? id : await activitypub.get('uid', 0, id); @@ -49,6 +50,11 @@ Actors.assert = async (ids, options = {}) => { const outbox = actor.outbox ? await activitypub.get('uid', 0, actor.outbox) : { totalItems: 0 }; actor.postcount = outbox.totalItems; + // Followers url for backreference + if (actor.hasOwnProperty('followers') && activitypub.helpers.isUri(actor.followers)) { + followersUrlMap.set(actor.followers, actor.id); + } + return actor; } catch (e) { return null; @@ -59,14 +65,19 @@ Actors.assert = async (ids, options = {}) => { const profiles = await activitypub.mocks.profile(actors); const now = Date.now(); + const bulkSet = profiles.map((profile) => { + if (!profile) { + return null; + } + const key = `userRemote:${profile.uid}`; + return [key, profile]; + }).filter(Boolean); + if (followersUrlMap.size) { + bulkSet.push(['followersUrl:uid', Object.fromEntries(followersUrlMap)]); + } + await Promise.all([ - db.setObjectBulk(profiles.map((profile, idx) => { - if (!profile) { - return null; - } - const key = `userRemote:${ids[idx]}`; - return [key, profile]; - }).filter(Boolean)), + db.setObjectBulk(bulkSet), db.sortedSetAdd('usersRemote:lastCrawled', ids.map((id, idx) => (profiles[idx] ? now : null)).filter(Boolean), ids.filter((id, idx) => profiles[idx])), ]); diff --git a/src/activitypub/inbox.js b/src/activitypub/inbox.js index 0ea48737f1..8aaa1ce279 100644 --- a/src/activitypub/inbox.js +++ b/src/activitypub/inbox.js @@ -204,6 +204,7 @@ inbox.accept = async (req) => { if (type === 'Follow') { const now = Date.now(); await db.sortedSetAdd(`followingRemote:${uid}`, now, actor); + await db.sortedSetAdd(`followersRemote:${actor}`, now, uid); // for followers backreference const followingRemoteCount = await db.sortedSetCard(`followingRemote:${uid}`); await user.setUserField(uid, 'followingRemoteCount', followingRemoteCount); }