Files
NodeBB/src/categories/delete.js

106 lines
2.7 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');
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);
}, {alwaysStartAt: 0}, next);
},
function (next) {
Categories.getPinnedTids('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) {
plugins.fireHook('action:category.delete', cid);
next();
2014-09-16 21:45:46 -04: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',
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) {
async.each(privileges.privilegeList, function (privilege, next) {
2016-06-08 10:46:33 +03:00
groups.destroy('cid:' + cid + ':privileges:' + privilege, next);
}, next);
}
], 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);
}
}, 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) {
callback(err);
});
}
};