From 830166d12023b14bb8a024a6d1c4edacd120be3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Tue, 7 Dec 2021 18:40:23 -0500 Subject: [PATCH] fix: #10067, count posts instead of incr/decr --- src/posts/delete.js | 2 +- src/posts/user.js | 4 ++-- src/user/posts.js | 21 +++++++++++++++++---- test/user.js | 4 ++-- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/posts/delete.js b/src/posts/delete.js index 5359e54033..9cb26b2860 100644 --- a/src/posts/delete.js +++ b/src/posts/delete.js @@ -84,7 +84,7 @@ module.exports = function (Posts) { topics.updateTeaser(postData.tid), topics.updateLastPostTimeFromLastPid(postData.tid), db.sortedSetIncrBy(`tid:${postData.tid}:posters`, -1, postData.uid), - user.incrementUserPostCountBy(postData.uid, -1), + user.updatePostCount(postData.uid), notifications.rescind(`new_post:tid:${postData.tid}:pid:${postData.pid}:uid:${postData.uid}`), ]; diff --git a/src/posts/user.js b/src/posts/user.js index 9ff193c04b..b797fed89b 100644 --- a/src/posts/user.js +++ b/src/posts/user.js @@ -169,7 +169,7 @@ module.exports = function (Posts) { db.setObjectField(pids.map(pid => `post:${pid}`), 'uid', toUid), db.sortedSetRemoveBulk(bulkRemove), db.sortedSetAddBulk(bulkAdd), - user.incrementUserPostCountBy(toUid, pids.length), + user.updatePostCount(toUid), user.incrementUserReputationBy(toUid, repChange), handleMainPidOwnerChange(postData, toUid), reduceCounters(postsByUser), @@ -187,7 +187,7 @@ module.exports = function (Posts) { await async.eachOfSeries(postsByUser, async (posts, uid) => { const repChange = posts.reduce((acc, val) => acc + val.votes, 0); await Promise.all([ - user.incrementUserPostCountBy(uid, -posts.length), + user.updatePostCount(uid), user.incrementUserReputationBy(uid, -repChange), ]); }); diff --git a/src/user/posts.js b/src/user/posts.js index b106897c05..5885885b06 100644 --- a/src/user/posts.js +++ b/src/user/posts.js @@ -56,10 +56,11 @@ module.exports = function (User) { // For scheduled posts, use "action" time. It'll be updated in related cron job when post is published const lastposttime = postData.timestamp > Date.now() ? Date.now() : postData.timestamp; - await User.addPostIdToUser(postData); - await User.incrementUserPostCountBy(postData.uid, 1); - await User.setUserField(postData.uid, 'lastposttime', lastposttime); - await User.updateLastOnlineTime(postData.uid); + await Promise.all([ + User.addPostIdToUser(postData), + User.setUserField(postData.uid, 'lastposttime', lastposttime), + User.updateLastOnlineTime(postData.uid), + ]); }; User.addPostIdToUser = async function (postData) { @@ -67,6 +68,18 @@ module.exports = function (User) { `uid:${postData.uid}:posts`, `cid:${postData.cid}:uid:${postData.uid}:pids`, ], postData.timestamp, postData.pid); + await User.updatePostCount(postData.uid); + }; + + User.updatePostCount = async (uid) => { + const exists = await User.exists(uid); + if (exists) { + const count = await db.sortedSetCard(`uid:${uid}:posts`); + await Promise.all([ + User.setUserField(uid, 'postcount', count), + db.sortedSetAdd('users:postcount', count, uid), + ]); + } }; User.incrementUserPostCountBy = async function (uid, value) { diff --git a/test/user.js b/test/user.js index fb588e3fbd..f07e27ae8e 100644 --- a/test/user.js +++ b/test/user.js @@ -514,7 +514,7 @@ describe('User', () => { }); }); - it('should not re-add user to users:postcount if post is deleted after user deletion', async () => { + it('should not re-add user to users:postcount if post is purged after user account deletion', async () => { const uid = await User.create({ username: 'olduserwithposts' }); assert(await db.isSortedSetMember('users:postcount', uid)); @@ -531,7 +531,7 @@ describe('User', () => { assert(!await db.isSortedSetMember('users:postcount', uid)); }); - it('should not re-add user to users:reputation if post is upvoted after user deletion', async () => { + it('should not re-add user to users:reputation if post is upvoted after user account deletion', async () => { const uid = await User.create({ username: 'olduserwithpostsupvote' }); assert(await db.isSortedSetMember('users:reputation', uid));