fix: update logic so that purging a post does not remove toPid fields from children, updated addParentPosts so that post existence is checked

This commit is contained in:
Julian Lam
2025-10-29 14:49:53 -04:00
parent 728184dab6
commit f6219d0026
2 changed files with 8 additions and 10 deletions

View File

@@ -197,20 +197,15 @@ module.exports = function (Posts) {
}
async function deleteFromReplies(postData) {
const arrayOfReplyPids = await db.getSortedSetsMembers(postData.map(p => `pid:${p.pid}:replies`));
const allReplyPids = _.flatten(arrayOfReplyPids);
const promises = [
db.deleteObjectFields(
allReplyPids.map(pid => `post:${pid}`), ['toPid']
),
db.deleteAll(postData.map(p => `pid:${p.pid}:replies`)),
];
// Any replies to deleted posts will retain toPid reference (gh#13527)
await db.deleteAll(postData.map(p => `pid:${p.pid}:replies`));
// Remove post(s) from parents' replies zsets
const postsWithParents = postData.filter(p => parseInt(p.toPid, 10));
const bulkRemove = postsWithParents.map(p => [`pid:${p.toPid}:replies`, p.pid]);
promises.push(db.sortedSetRemoveBulk(bulkRemove));
await Promise.all(promises);
await db.sortedSetRemoveBulk(bulkRemove);
// Recalculate reply count
const parentPids = _.uniq(postsWithParents.map(p => p.toPid));
const counts = await db.sortedSetsCard(parentPids.map(pid => `pid:${pid}:replies`));
await db.setObjectBulk(parentPids.map((pid, index) => [`post:${pid}`, { replies: counts[index] }]));