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

isekai-main
Barış Soner Uşaklı 3 years ago
parent f4aa249d8c
commit 830166d120

@ -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}`),
];

@ -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),
]);
});

@ -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) {

@ -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));

Loading…
Cancel
Save