2014-06-10 17:48:48 -04:00
|
|
|
'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');
|
2014-06-10 17:48:48 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (Categories) {
|
|
|
|
|
Categories.purge = function (cid, uid, callback) {
|
2016-11-25 17:46:29 +03:00
|
|
|
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);
|
2016-11-25 17:46:29 +03:00
|
|
|
},
|
|
|
|
|
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) {
|
2017-01-26 22:18:16 +03:00
|
|
|
plugins.fireHook('action:category.delete', {cid: cid, uid: uid});
|
2016-11-25 17:46:29 +03:00
|
|
|
next();
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-11-25 17:46:29 +03:00
|
|
|
], callback);
|
2014-06-10 17:48:48 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function purgeCategory(cid, callback) {
|
2015-09-30 16:29:25 -04:00
|
|
|
async.series([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2014-06-10 17:48:48 -04:00
|
|
|
db.sortedSetRemove('categories:cid', cid, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2015-08-18 14:17:16 -04:00
|
|
|
removeFromParent(cid, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2014-11-07 17:15:01 -05:00
|
|
|
db.deleteAll([
|
|
|
|
|
'cid:' + cid + ':tids',
|
2016-11-25 17:46:29 +03:00
|
|
|
'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',
|
2015-08-18 14:17:16 -04:00
|
|
|
'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
|
|
|
},
|
2016-10-13 11:43:39 +02: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);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-11-25 17:46:29 +03:00
|
|
|
], function (err) {
|
|
|
|
|
callback(err);
|
|
|
|
|
});
|
2014-06-10 17:48:48 -04:00
|
|
|
}
|
2015-08-18 14:17:16 -04:00
|
|
|
|
|
|
|
|
function removeFromParent(cid, callback) {
|
|
|
|
|
async.waterfall([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2015-08-28 13:04:02 -04:00
|
|
|
async.parallel({
|
2016-10-13 11:43:39 +02:00
|
|
|
parentCid: function (next) {
|
2015-08-28 13:04:02 -04:00
|
|
|
Categories.getCategoryField(cid, 'parentCid', next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
children: function (next) {
|
2015-08-28 13:04:02 -04:00
|
|
|
db.getSortedSetRange('cid:' + cid + ':children', 0, -1, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2015-08-28 13:04:02 -04:00
|
|
|
}, next);
|
2015-08-18 14:17:16 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (results, next) {
|
2015-08-28 13:04:02 -04:00
|
|
|
async.parallel([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2015-08-28 13:04:02 -04:00
|
|
|
results.parentCid = parseInt(results.parentCid, 10) || 0;
|
|
|
|
|
db.sortedSetRemove('cid:' + results.parentCid + ':children', cid, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
|
|
|
|
async.each(results.children, function (cid, next) {
|
2015-09-18 15:56:07 -04:00
|
|
|
async.parallel([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2015-09-18 15:56:07 -04:00
|
|
|
db.setObjectField('category:' + cid, 'parentCid', 0, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2015-09-18 15:56:07 -04:00
|
|
|
db.sortedSetAdd('cid:0:children', cid, cid, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2015-09-18 15:56:07 -04:00
|
|
|
], next);
|
2015-08-28 13:04:02 -04:00
|
|
|
}, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2015-08-28 13:04:02 -04:00
|
|
|
], next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
], function (err) {
|
2015-08-28 13:04:02 -04:00
|
|
|
callback(err);
|
|
|
|
|
});
|
2015-08-18 14:17:16 -04:00
|
|
|
}
|
2017-02-18 02:30:48 -07:00
|
|
|
};
|