You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
2.1 KiB
JavaScript

'use strict';
var async = require('async'),
db = require('../database'),
11 years ago
batch = require('../batch'),
plugins = require('../plugins'),
10 years ago
topics = require('../topics');
module.exports = function(Categories) {
Categories.purge = function(cid, callback) {
10 years ago
batch.processSortedSet('cid:' + cid + ':tids', function(tids, next) {
async.eachLimit(tids, 10, function(tid, next) {
10 years ago
topics.purgePostsAndTopic(tid, next);
11 years ago
}, next);
}, {alwaysStartAt: 0}, function(err) {
11 years ago
if (err) {
return callback(err);
}
async.series([
async.apply(purgeCategory, cid),
async.apply(plugins.fireHook, 'action:category.delete', cid)
], callback);
});
};
function purgeCategory(cid, callback) {
async.parallel([
function(next) {
db.sortedSetRemove('categories:cid', cid, next);
},
function(next) {
removeFromParent(cid, next);
},
function(next) {
10 years ago
db.deleteAll([
'cid:' + cid + ':tids',
10 years ago
'cid:' + cid + ':tids:posts',
10 years ago
'cid:' + cid + ':pids',
10 years ago
'cid:' + cid + ':read_by_uid',
'cid:' + cid + ':children',
10 years ago
'category:' + cid
], 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);
}
], function(err, results) {
callback(err);
});
}
};