fix: topic postercount field if owner is changed

also fix when posts are purged
isekai-main
Barış Soner Uşaklı 2 years ago
parent 86dd04d5fd
commit 00be053e94

@ -116,7 +116,9 @@ module.exports = function (Posts) {
const topicPostCountTasks = []; const topicPostCountTasks = [];
const topicTasks = []; const topicTasks = [];
const zsetIncrBulk = []; const zsetIncrBulk = [];
const tids = [];
for (const [tid, posts] of Object.entries(postsByTopic)) { for (const [tid, posts] of Object.entries(postsByTopic)) {
tids.push(tid);
incrObjectBulk.push([`topic:${tid}`, { postcount: -posts.length }]); incrObjectBulk.push([`topic:${tid}`, { postcount: -posts.length }]);
if (posts.length && posts[0]) { if (posts.length && posts[0]) {
const topicData = posts[0].topic; const topicData = posts[0].topic;
@ -142,6 +144,14 @@ module.exports = function (Posts) {
user.updatePostCount(_.uniq(postData.map(p => p.uid))), user.updatePostCount(_.uniq(postData.map(p => p.uid))),
notifications.rescind(...postData.map(p => `new_post:tid:${p.tid}:pid:${p.pid}:uid:${p.uid}`)), notifications.rescind(...postData.map(p => `new_post:tid:${p.tid}:pid:${p.pid}:uid:${p.uid}`)),
]); ]);
const tidPosterZsets = tids.map(tid => `tid:${tid}:posters`);
await db.sortedSetsRemoveRangeByScore(tidPosterZsets, '-inf', 0);
const posterCounts = await db.sortedSetsCard(tidPosterZsets);
await db.setObjectBulk(
tids.map((tid, idx) => (
[`topic:${tid}`, { postercount: posterCounts[idx] || 0 }]
))
);
} }
async function deleteFromCategoryRecentPosts(postData) { async function deleteFromCategoryRecentPosts(postData) {

@ -206,6 +206,9 @@ module.exports = function (Posts) {
await async.eachOf(postsByUser, async (posts, uid) => { await async.eachOf(postsByUser, async (posts, uid) => {
await db.sortedSetIncrBy(`tid:${tid}:posters`, -posts.length, uid); await db.sortedSetIncrBy(`tid:${tid}:posters`, -posts.length, uid);
}); });
await db.sortedSetsRemoveRangeByScore([`tid:${tid}:posters`], '-inf', 0);
const posterCount = await db.sortedSetCard(`tid:${tid}:posters`);
await topics.setTopicField(tid, 'postercount', posterCount);
}); });
} }

@ -114,7 +114,7 @@ describe('Post\'s', () => {
await posts.changeOwner([pid1, pid2], newUid); await posts.changeOwner([pid1, pid2], newUid);
assert.deepStrictEqual(await db.sortedSetScores(`tid:${postResult.topicData.tid}:posters`, [oldUid, newUid]), [0, 2]); assert.deepStrictEqual(await db.sortedSetScores(`tid:${postResult.topicData.tid}:posters`, [oldUid, newUid]), [null, 2]);
assert.deepStrictEqual(await posts.isOwner([pid1, pid2], oldUid), [false, false]); assert.deepStrictEqual(await posts.isOwner([pid1, pid2], oldUid), [false, false]);
assert.deepStrictEqual(await posts.isOwner([pid1, pid2], newUid), [true, true]); assert.deepStrictEqual(await posts.isOwner([pid1, pid2], newUid), [true, true]);
@ -130,6 +130,8 @@ describe('Post\'s', () => {
assert.strictEqual(await topics.isOwner(postResult.topicData.tid, oldUid), false); assert.strictEqual(await topics.isOwner(postResult.topicData.tid, oldUid), false);
assert.strictEqual(await topics.isOwner(postResult.topicData.tid, newUid), true); assert.strictEqual(await topics.isOwner(postResult.topicData.tid, newUid), true);
assert.strictEqual(await topics.getTopicField(postResult.topicData.tid, 'postercount'), 1);
}); });
it('should fail to change owner if new owner does not exist', async () => { it('should fail to change owner if new owner does not exist', async () => {

Loading…
Cancel
Save