2014-06-10 17:48:48 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
var async = require('async'),
|
|
|
|
|
db = require('../database'),
|
2014-09-16 21:43:11 -04:00
|
|
|
batch = require('../batch'),
|
2015-06-19 15:39:33 -04:00
|
|
|
plugins = require('../plugins'),
|
2014-06-10 17:48:48 -04:00
|
|
|
threadTools = require('../threadTools');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = function(Categories) {
|
|
|
|
|
|
|
|
|
|
Categories.purge = function(cid, callback) {
|
2014-11-07 17:15:01 -05:00
|
|
|
batch.processSortedSet('cid:' + cid + ':tids', function(tids, next) {
|
2014-06-21 22:11:44 -04:00
|
|
|
async.eachLimit(tids, 10, function(tid, next) {
|
2014-06-10 17:48:48 -04:00
|
|
|
threadTools.purge(tid, 0, 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);
|
2014-06-10 17:48:48 -04:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function purgeCategory(cid, callback) {
|
|
|
|
|
async.parallel([
|
|
|
|
|
function(next) {
|
|
|
|
|
db.sortedSetRemove('categories:cid', cid, next);
|
|
|
|
|
},
|
2015-08-18 14:17:16 -04:00
|
|
|
function(next) {
|
|
|
|
|
removeFromParent(cid, next);
|
|
|
|
|
},
|
2014-06-10 17:48:48 -04:00
|
|
|
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',
|
2015-08-18 14:17:16 -04:00
|
|
|
'cid:' + cid + ':children',
|
2014-11-07 17:15:01 -05:00
|
|
|
'category:' + cid
|
|
|
|
|
], next);
|
2014-06-10 17:48:48 -04:00
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
2015-08-18 14:17:16 -04:00
|
|
|
|
|
|
|
|
function removeFromParent(cid, callback) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function(next) {
|
2015-08-28 13:04:02 -04:00
|
|
|
async.parallel({
|
|
|
|
|
parentCid: function(next) {
|
|
|
|
|
Categories.getCategoryField(cid, 'parentCid', next);
|
|
|
|
|
},
|
|
|
|
|
children: function(next) {
|
|
|
|
|
db.getSortedSetRange('cid:' + cid + ':children', 0, -1, next);
|
|
|
|
|
}
|
|
|
|
|
}, next);
|
2015-08-18 14:17:16 -04:00
|
|
|
},
|
2015-08-28 13:04:02 -04:00
|
|
|
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) {
|
|
|
|
|
db.setObjectField('category:' + cid, 'parentCid', 0, next);
|
|
|
|
|
}, next);
|
|
|
|
|
}
|
|
|
|
|
], next);
|
2015-08-18 14:17:16 -04:00
|
|
|
}
|
2015-08-28 13:04:02 -04:00
|
|
|
], function(err, results) {
|
|
|
|
|
callback(err);
|
|
|
|
|
});
|
2015-08-18 14:17:16 -04:00
|
|
|
}
|
2014-06-10 17:48:48 -04:00
|
|
|
};
|