Files
NodeBB/src/categories/delete.js

92 lines
2.4 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');
module.exports = function(Categories) {
2016-02-25 18:01:59 +02:00
Categories.purge = function(cid, uid, callback) {
2014-11-07 17:15:01 -05:00
batch.processSortedSet('cid:' + cid + ':tids', function(tids, next) {
async.eachLimit(tids, 10, function(tid, next) {
2016-02-25 18:01:59 +02:00
topics.purgePostsAndTopic(tid, uid, next);
2014-09-16 21:43:11 -04:00
}, next);
}, {alwaysStartAt: 0}, function(err) {
2014-09-16 21:45:46 -04:00
if (err) {
return callback(err);
}
2015-06-19 15:39:33 -04:00
async.series([
async.apply(purgeCategory, cid),
async.apply(plugins.fireHook, 'action:category.delete', cid)
], 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',
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',
2016-05-18 19:02:43 +03:00
'cid:' + cid + ':ignorers',
'cid:' + cid + ':children',
2014-11-07 17:15:01 -05:00
'category:' + cid
], next);
2016-06-08 10:46:33 +03:00
},
function(next) {
2016-08-26 19:33:16 +03:00
var privileges = require('../privileges');
2016-06-08 10:46:33 +03:00
async.each(privileges.privilegeList, function(privilege, next) {
groups.destroy('cid:' + cid + ':privileges:' + privilege, next);
}, next);
}
], callback);
}
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);
}
}, next);
},
function(results, next) {
async.parallel([
function(next) {
results.parentCid = parseInt(results.parentCid, 10) || 0;
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);
}
], next);
}, next);
}
], next);
}
2016-06-08 10:46:33 +03:00
], function(err) {
callback(err);
});
}
};