all files / src/socket.io/topics/ move.js

22.86% Statements 8/35
0% Branches 0/16
7.69% Functions 1/13
22.86% Lines 8/35
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                                                                                                                                   
'use strict';
 
var async = require('async');
var topics = require('../../topics');
var categories = require('../../categories');
var privileges = require('../../privileges');
var socketHelpers = require('../helpers');
 
module.exports = function(SocketTopics) {
 
	SocketTopics.move = function(socket, data, callback) {
		if (!data || !Array.isArray(data.tids) || !data.cid) {
			return callback(new Error('[[error:invalid-data]]'));
		}
 
		async.eachLimit(data.tids, 10, function(tid, next) {
			var topicData;
			async.waterfall([
				function(next) {
					privileges.topics.isAdminOrMod(tid, socket.uid, next);
				},
				function(canMove, next) {
					if (!canMove) {
						return next(new Error('[[error:no-privileges]]'));
					}
					next();
				},
				function(next) {
					topics.getTopicFields(tid, ['cid', 'slug'], next);
				},
				function(_topicData, next) {
					topicData = _topicData;
					topicData.tid = tid;
					topics.tools.move(tid, data.cid, socket.uid, next);
				}
			], function(err) {
				if (err) {
					return next(err);
				}
 
				socketHelpers.emitToTopicAndCategory('event:topic_moved', topicData);
 
				socketHelpers.sendNotificationToTopicOwner(tid, socket.uid, 'notifications:moved_your_topic');
 
				next();
			});
		}, callback);
	};
 
 
	SocketTopics.moveAll = function(socket, data, callback) {
		if (!data || !data.cid || !data.currentCid) {
			return callback(new Error('[[error:invalid-data]]'));
		}
 
		async.waterfall([
			function (next) {
				privileges.categories.canMoveAllTopics(data.currentCid, data.cid, socket.uid, next);
			},
			function (canMove, next) {
				if (!canMove) {
					return callback(new Error('[[error:no-privileges]]'));
				}
 
				categories.getTopicIds('cid:' + data.currentCid + ':tids', true, 0, -1, next);
			},
			function (tids, next) {
				async.eachLimit(tids, 50, function(tid, next) {
					topics.tools.move(tid, data.cid, socket.uid, next);
				}, next);
			}
		], callback);
	};
};