2014-06-10 17:48:48 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2021-02-04 00:06:15 -07:00
|
|
|
const async = require('async');
|
|
|
|
|
const db = require('../database');
|
|
|
|
|
const batch = require('../batch');
|
|
|
|
|
const plugins = require('../plugins');
|
|
|
|
|
const topics = require('../topics');
|
|
|
|
|
const groups = require('../groups');
|
|
|
|
|
const privileges = require('../privileges');
|
|
|
|
|
const cache = require('../cache');
|
2014-06-10 17:48:48 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Categories) {
|
2019-07-16 00:41:42 -04:00
|
|
|
Categories.purge = async function (cid, uid) {
|
2021-02-04 00:01:39 -07:00
|
|
|
await batch.processSortedSet(`cid:${cid}:tids`, async (tids) => {
|
|
|
|
|
await async.eachLimit(tids, 10, async (tid) => {
|
2019-07-16 00:41:42 -04:00
|
|
|
await topics.purgePostsAndTopic(tid, uid);
|
|
|
|
|
});
|
|
|
|
|
}, { alwaysStartAt: 0 });
|
2014-06-10 17:48:48 -04:00
|
|
|
|
2021-02-03 23:59:08 -07:00
|
|
|
const pinnedTids = await db.getSortedSetRevRange(`cid:${cid}:tids:pinned`, 0, -1);
|
2021-02-04 00:01:39 -07:00
|
|
|
await async.eachLimit(pinnedTids, 10, async (tid) => {
|
2019-07-16 00:41:42 -04:00
|
|
|
await topics.purgePostsAndTopic(tid, uid);
|
2016-11-25 17:46:29 +03:00
|
|
|
});
|
2021-01-28 10:18:09 -05:00
|
|
|
const categoryData = await Categories.getCategoryData(cid);
|
|
|
|
|
await purgeCategory(categoryData);
|
|
|
|
|
plugins.hooks.fire('action:category.delete', { cid: cid, uid: uid, category: categoryData });
|
2019-07-16 00:41:42 -04:00
|
|
|
};
|
|
|
|
|
|
2021-01-28 10:18:09 -05:00
|
|
|
async function purgeCategory(categoryData) {
|
2021-02-06 14:10:15 -07:00
|
|
|
const { cid } = categoryData;
|
2021-01-28 10:18:09 -05:00
|
|
|
await db.sortedSetRemoveBulk([
|
|
|
|
|
['categories:cid', cid],
|
2021-02-03 23:59:08 -07:00
|
|
|
['categories:name', `${categoryData.name.substr(0, 200).toLowerCase()}:${cid}`],
|
2021-01-28 10:18:09 -05:00
|
|
|
]);
|
2019-07-16 00:41:42 -04:00
|
|
|
await removeFromParent(cid);
|
2020-12-06 12:36:40 -05:00
|
|
|
await deleteTags(cid);
|
2019-07-16 00:41:42 -04:00
|
|
|
await db.deleteAll([
|
2021-02-03 23:59:08 -07:00
|
|
|
`cid:${cid}:tids`,
|
|
|
|
|
`cid:${cid}:tids:pinned`,
|
|
|
|
|
`cid:${cid}:tids:posts`,
|
|
|
|
|
`cid:${cid}:tids:votes`,
|
|
|
|
|
`cid:${cid}:tids:lastposttime`,
|
|
|
|
|
`cid:${cid}:recent_tids`,
|
|
|
|
|
`cid:${cid}:pids`,
|
|
|
|
|
`cid:${cid}:read_by_uid`,
|
|
|
|
|
`cid:${cid}:uid:watch:state`,
|
|
|
|
|
`cid:${cid}:children`,
|
|
|
|
|
`cid:${cid}:tag:whitelist`,
|
|
|
|
|
`category:${cid}`,
|
2019-07-16 00:41:42 -04:00
|
|
|
]);
|
2021-02-24 18:10:34 -05:00
|
|
|
await groups.destroy(privileges.categories.privilegeList.map(privilege => `cid:${cid}:privileges:${privilege}`));
|
2014-06-10 17:48:48 -04:00
|
|
|
}
|
2015-08-18 14:17:16 -04:00
|
|
|
|
2019-07-16 00:41:42 -04:00
|
|
|
async function removeFromParent(cid) {
|
|
|
|
|
const [parentCid, children] = await Promise.all([
|
|
|
|
|
Categories.getCategoryField(cid, 'parentCid'),
|
2021-02-03 23:59:08 -07:00
|
|
|
db.getSortedSetRange(`cid:${cid}:children`, 0, -1),
|
2019-07-16 00:41:42 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
const bulkAdd = [];
|
2021-02-04 00:01:39 -07:00
|
|
|
const childrenKeys = children.map((cid) => {
|
2019-07-16 00:41:42 -04:00
|
|
|
bulkAdd.push(['cid:0:children', cid, cid]);
|
2021-02-03 23:59:08 -07:00
|
|
|
return `category:${cid}`;
|
2015-08-28 13:04:02 -04:00
|
|
|
});
|
2019-07-16 00:41:42 -04:00
|
|
|
|
|
|
|
|
await Promise.all([
|
2021-02-03 23:59:08 -07:00
|
|
|
db.sortedSetRemove(`cid:${parentCid}:children`, cid),
|
2019-07-16 00:41:42 -04:00
|
|
|
db.setObjectField(childrenKeys, 'parentCid', 0),
|
|
|
|
|
db.sortedSetAddBulk(bulkAdd),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
cache.del([
|
|
|
|
|
'categories:cid',
|
|
|
|
|
'cid:0:children',
|
2021-02-03 23:59:08 -07:00
|
|
|
`cid:${parentCid}:children`,
|
|
|
|
|
`cid:${parentCid}:children:all`,
|
|
|
|
|
`cid:${cid}:children`,
|
|
|
|
|
`cid:${cid}:children:all`,
|
|
|
|
|
`cid:${cid}:tag:whitelist`,
|
2019-07-16 00:41:42 -04:00
|
|
|
]);
|
2015-08-18 14:17:16 -04:00
|
|
|
}
|
2020-12-06 12:36:40 -05:00
|
|
|
|
|
|
|
|
async function deleteTags(cid) {
|
2021-02-03 23:59:08 -07:00
|
|
|
const tags = await db.getSortedSetMembers(`cid:${cid}:tags`);
|
|
|
|
|
await db.deleteAll(tags.map(tag => `cid:${cid}:tag:${tag}:topics`));
|
|
|
|
|
await db.delete(`cid:${cid}:tags`);
|
2020-12-06 12:36:40 -05:00
|
|
|
}
|
2017-02-18 02:30:48 -07:00
|
|
|
};
|