Skip to content

Node.js based forum software built for the modern web

fix: #10067, count posts instead of incr/decr

Barış Soner Uşaklı830166d12023
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));