From 30b6bcfca117e667c262c0462fc5f0100e6a436c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Wed, 26 Apr 2023 10:13:27 -0400 Subject: [PATCH] fix: #11519, clear parent cache all the way to root --- src/categories/create.js | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/categories/create.js b/src/categories/create.js index ce3f01f22e..c2f7fb7968 100644 --- a/src/categories/create.js +++ b/src/categories/create.js @@ -95,11 +95,9 @@ module.exports = function (Categories) { await privileges.categories.give(result.modPrivileges, category.cid, ['administrators', 'Global Moderators']); await privileges.categories.give(result.guestPrivileges, category.cid, ['guests', 'spiders']); - cache.del([ - 'categories:cid', - `cid:${parentCid}:children`, - `cid:${parentCid}:children:all`, - ]); + cache.del('categories:cid'); + await clearParentCategoryCache(parentCid); + if (data.cloneFromCid && parseInt(data.cloneFromCid, 10)) { category = await Categories.copySettingsFrom(data.cloneFromCid, category.cid, !data.parentCid); } @@ -112,6 +110,22 @@ module.exports = function (Categories) { return category; }; + async function clearParentCategoryCache(parentCid) { + while (parseInt(parentCid, 10) >= 0) { + cache.del([ + `cid:${parentCid}:children`, + `cid:${parentCid}:children:all`, + ]); + + if (parseInt(parentCid, 10) === 0) { + return; + } + // clear all the way to root + // eslint-disable-next-line no-await-in-loop + parentCid = await Categories.getCategoryField(parentCid, 'parentCid'); + } + } + async function duplicateCategoriesChildren(parentCid, cid, uid) { let children = await Categories.getChildren([cid], uid); if (!children.length) {