fix: store remote followed users count separately from local

This commit is contained in:
Julian Lam
2023-12-07 13:23:06 -05:00
parent 4324f09c85
commit e794f1d2ce

View File

@@ -132,8 +132,10 @@ Controller.follow = async (req, res) => {
}); });
const now = Date.now(); const now = Date.now();
await db.sortedSetAdd(`followingRemote:${req.uid}`, now, objectId); await Promise.all([
await recountFollowing(req.uid); db.sortedSetAdd(`followingRemote:${req.uid}`, now, objectId),
db.incrObjectField(`user:${req.uid}`, 'followingRemoteCount'),
]);
helpers.formatApiResponse(200, res); helpers.formatApiResponse(200, res);
} catch (e) { } catch (e) {
@@ -152,19 +154,13 @@ Controller.unfollow = async (req, res) => {
}, },
}); });
await db.sortedSetRemove(`followingRemote:${req.uid}`, objectId); await Promise.all([
await recountFollowing(req.uid); db.sortedSetRemove(`followingRemote:${req.uid}`, objectId),
db.decrObjectField(`user:${req.uid}`, 'followingRemoteCount'),
]);
helpers.formatApiResponse(200, res); helpers.formatApiResponse(200, res);
} catch (e) { } catch (e) {
helpers.formatApiResponse(400, res, 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);
}