all files / src/categories/ delete.js

18.52% Statements 5/27
0% Branches 0/4
5% Functions 1/20
18.52% Lines 5/27
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84                                                                                                                                                             
'use strict';
 
var async = require('async'),
	db = require('../database'),
	batch = require('../batch'),
	plugins = require('../plugins'),
	topics = require('../topics');
 
module.exports = function(Categories) {
 
	Categories.purge = function(cid, uid, callback) {
		batch.processSortedSet('cid:' + cid + ':tids', function(tids, next) {
			async.eachLimit(tids, 10, function(tid, next) {
				topics.purgePostsAndTopic(tid, uid, next);
			}, next);
		}, {alwaysStartAt: 0}, function(err) {
			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.series([
			function(next) {
				db.sortedSetRemove('categories:cid', cid, next);
			},
			function(next) {
				removeFromParent(cid, next);
			},
			function(next) {
				db.deleteAll([
					'cid:' + cid + ':tids',
					'cid:' + cid + ':tids:posts',
					'cid:' + cid + ':pids',
					'cid:' + cid + ':read_by_uid',
					'cid:' + cid + ':children',
					'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);
		});
	}
};