Files
NodeBB/src/categories/delete.js

117 lines
3.0 KiB
JavaScript
Raw Normal View History

'use strict';
2016-06-08 10:46:33 +03:00
var async = require('async');
var db = require('../database');
var batch = require('../batch');
var plugins = require('../plugins');
var topics = require('../topics');
var groups = require('../groups');
2016-08-27 01:52:08 +03:00
var privileges = require('../privileges');
var cache = require('../cache');
module.exports = function (Categories) {
Categories.purge = function (cid, uid, callback) {
async.waterfall([
function (next) {
batch.processSortedSet('cid:' + cid + ':tids', function (tids, next) {
async.eachLimit(tids, 10, function (tid, next) {
topics.purgePostsAndTopic(tid, uid, next);
}, next);
2017-02-18 12:30:49 -07:00
}, { alwaysStartAt: 0 }, next);
},
function (next) {
2017-06-26 16:51:45 -04:00
db.getSortedSetRevRange('cid:' + cid + ':tids:pinned', 0, -1, next);
},
function (pinnedTids, next) {
async.eachLimit(pinnedTids, 10, function (tid, next) {
topics.purgePostsAndTopic(tid, uid, next);
}, next);
},
function (next) {
purgeCategory(cid, next);
},
function (next) {
2017-02-24 12:47:46 -05:00
plugins.fireHook('action:category.delete', { cid: cid, uid: uid });
next();
2017-02-17 19:31:21 -07:00
},
], callback);
};
function purgeCategory(cid, callback) {
2015-09-30 16:29:25 -04:00
async.series([
function (next) {
db.sortedSetRemove('categories:cid', cid, next);
},
function (next) {
removeFromParent(cid, next);
},
function (next) {
2014-11-07 17:15:01 -05:00
db.deleteAll([
'cid:' + cid + ':tids',
'cid:' + cid + ':tids:pinned',
2015-01-08 13:47:15 -05:00
'cid:' + cid + ':tids:posts',
2014-11-07 17:15:01 -05:00
'cid:' + cid + ':pids',
2015-01-18 14:32:47 -05:00
'cid:' + cid + ':read_by_uid',
'cid:' + cid + ':uid:watch:state',
'cid:' + cid + ':children',
2016-12-09 18:53:08 +03:00
'cid:' + cid + ':tag:whitelist',
2017-02-17 19:31:21 -07:00
'category:' + cid,
2014-11-07 17:15:01 -05:00
], next);
2016-06-08 10:46:33 +03:00
},
function (next) {
2018-10-22 21:58:34 -04:00
groups.destroy(privileges.privilegeList.map(privilege => 'cid:' + cid + ':privileges:' + privilege), next);
2017-02-17 19:31:21 -07:00
},
], function (err) {
callback(err);
});
}
function removeFromParent(cid, callback) {
async.waterfall([
function (next) {
async.parallel({
parentCid: function (next) {
Categories.getCategoryField(cid, 'parentCid', next);
},
children: function (next) {
db.getSortedSetRange('cid:' + cid + ':children', 0, -1, next);
2017-02-17 19:31:21 -07:00
},
}, next);
},
function (results, next) {
async.parallel([
function (next) {
db.sortedSetRemove('cid:' + results.parentCid + ':children', cid, next);
},
function (next) {
async.each(results.children, function (cid, next) {
async.parallel([
function (next) {
db.setObjectField('category:' + cid, 'parentCid', 0, next);
},
function (next) {
db.sortedSetAdd('cid:0:children', cid, cid, next);
2017-02-17 19:31:21 -07:00
},
], next);
}, next);
2017-02-17 19:31:21 -07:00
},
], function (err) {
if (err) {
return next(err);
}
cache.del([
'categories:cid',
'cid:0:children',
'cid:' + results.parentCid + ':children',
'cid:' + cid + ':children',
2018-12-09 16:03:41 -05:00
'cid:' + cid + ':tag:whitelist',
]);
next();
});
2017-02-17 19:31:21 -07:00
},
], function (err) {
callback(err);
});
}
2017-02-18 02:30:48 -07:00
};