From e794f1d2ceb9af4cb1ea8a6059688287ff00182c Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Thu, 7 Dec 2023 13:23:06 -0500 Subject: [PATCH] fix: store remote followed users count separately from local --- src/controllers/activitypub/index.js | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/controllers/activitypub/index.js b/src/controllers/activitypub/index.js index 1ecb86a697..d23081425c 100644 --- a/src/controllers/activitypub/index.js +++ b/src/controllers/activitypub/index.js @@ -132,8 +132,10 @@ Controller.follow = async (req, res) => { }); const now = Date.now(); - await db.sortedSetAdd(`followingRemote:${req.uid}`, now, objectId); - await recountFollowing(req.uid); + await Promise.all([ + db.sortedSetAdd(`followingRemote:${req.uid}`, now, objectId), + db.incrObjectField(`user:${req.uid}`, 'followingRemoteCount'), + ]); helpers.formatApiResponse(200, res); } catch (e) { @@ -152,19 +154,13 @@ Controller.unfollow = async (req, res) => { }, }); - await db.sortedSetRemove(`followingRemote:${req.uid}`, objectId); - await recountFollowing(req.uid); + await Promise.all([ + db.sortedSetRemove(`followingRemote:${req.uid}`, objectId), + db.decrObjectField(`user:${req.uid}`, 'followingRemoteCount'), + ]); helpers.formatApiResponse(200, res); } catch (e) { helpers.formatApiResponse(400, res, e); } }; - -async function recountFollowing(uid) { - const [followingCount, followingRemoteCount] = await Promise.all([ - db.sortedSetCard(`following:${uid}`), - db.sortedSetCard(`followingRemote:${uid}`), - ]); - await user.setUserField(uid, 'followingCount', followingCount + followingRemoteCount); -}