| 
									
										
										
										
											2017-02-18 01:56:23 -07:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2015-01-07 16:55:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-22 17:21:30 +03:00
										 |  |  | var	async = require('async'); | 
					
						
							| 
									
										
										
										
											2015-06-02 16:20:10 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-22 17:21:30 +03:00
										 |  |  | var groups = require('../groups'); | 
					
						
							|  |  |  | var meta = require('../meta'); | 
					
						
							|  |  |  | var user = require('../user'); | 
					
						
							|  |  |  | var utils = require('../../public/src/utils'); | 
					
						
							|  |  |  | var groupsController = require('../controllers/groups'); | 
					
						
							| 
									
										
										
										
											2015-01-07 16:55:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-22 17:21:30 +03:00
										 |  |  | var SocketGroups = {}; | 
					
						
							| 
									
										
										
										
											2015-01-07 16:55:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 19:57:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.before = function (socket, method, data, next) { | 
					
						
							| 
									
										
										
										
											2015-01-07 16:55:14 -05:00
										 |  |  | 	if (!data) { | 
					
						
							| 
									
										
										
										
											2015-09-17 19:57:47 -04:00
										 |  |  | 		return next(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2015-01-07 16:55:14 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-17 19:57:47 -04:00
										 |  |  | 	next(); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-01-07 16:55:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.join = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-02-04 00:46:51 -05:00
										 |  |  | 	if (!parseInt(socket.uid, 10)) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-uid]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 19:34:48 +02:00
										 |  |  | 	if (data.groupName === 'administrators' || groups.isPrivilegeGroup(data.groupName)) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:not-allowed]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-22 17:21:30 +03:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			groups.exists(data.groupName, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (exists, next) { | 
					
						
							|  |  |  | 			if (!exists) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:no-group]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-10-02 17:13:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-22 17:21:30 +03:00
										 |  |  | 			if (parseInt(meta.config.allowPrivateGroups, 10) !== 1) { | 
					
						
							|  |  |  | 				return groups.join(data.groupName, socket.uid, callback); | 
					
						
							| 
									
										
										
										
											2015-10-02 17:13:13 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-22 17:21:30 +03:00
										 |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				isAdmin: async.apply(user.isAdministrator, socket.uid), | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				groupData: async.apply(groups.getGroupData, data.groupName), | 
					
						
							| 
									
										
										
										
											2016-11-22 17:21:30 +03:00
										 |  |  | 			}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (results, next) { | 
					
						
							| 
									
										
										
										
											2015-12-29 10:34:06 +02:00
										 |  |  | 			if (results.groupData.private && results.groupData.disableJoinRequests) { | 
					
						
							| 
									
										
										
										
											2016-11-22 17:21:30 +03:00
										 |  |  | 				return next(new Error('[[error:join-requests-disabled]]')); | 
					
						
							| 
									
										
										
										
											2015-12-29 10:34:06 +02:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-01-12 10:25:20 +02:00
										 |  |  | 			if (!results.groupData.private || results.isAdmin) { | 
					
						
							| 
									
										
										
										
											2016-11-22 17:21:30 +03:00
										 |  |  | 				groups.join(data.groupName, socket.uid, next); | 
					
						
							| 
									
										
										
										
											2015-12-28 14:37:04 +02:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2016-11-22 17:21:30 +03:00
										 |  |  | 				groups.requestMembership(data.groupName, socket.uid, next); | 
					
						
							| 
									
										
										
										
											2015-01-08 16:50:31 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-11-22 17:21:30 +03:00
										 |  |  | 	], callback); | 
					
						
							| 
									
										
										
										
											2015-01-07 16:55:14 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.leave = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-02-04 12:09:54 -05:00
										 |  |  | 	if (!parseInt(socket.uid, 10)) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-uid]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-28 13:26:52 -04:00
										 |  |  | 	if (data.groupName === 'administrators') { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:cant-remove-self-as-admin]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-07 16:55:14 -05:00
										 |  |  | 	groups.leave(data.groupName, socket.uid, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | function isOwner(next) { | 
					
						
							|  |  |  | 	return function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-10-13 19:28:55 -04:00
										 |  |  | 		async.parallel({ | 
					
						
							| 
									
										
										
										
											2015-10-26 14:50:32 +01:00
										 |  |  | 			isAdmin: async.apply(user.isAdministrator, socket.uid), | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 			isOwner: async.apply(groups.ownership.isOwner, socket.uid, data.groupName), | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		}, function (err, results) { | 
					
						
							| 
									
										
										
										
											2015-10-13 19:28:55 -04:00
										 |  |  | 			if (err || (!isOwner && !results.isAdmin)) { | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | 				return callback(err || new Error('[[error:no-privileges]]')); | 
					
						
							| 
									
										
										
										
											2015-06-09 17:47:02 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | 			next(socket, data, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2015-07-28 13:26:52 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-15 16:58:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 20:55:18 -04:00
										 |  |  | function isInvited(next) { | 
					
						
							|  |  |  | 	return function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		groups.isInvited(socket.uid, data.groupName, function (err, invited) { | 
					
						
							| 
									
										
										
										
											2015-09-17 20:55:18 -04:00
										 |  |  | 			if (err || !invited) { | 
					
						
							|  |  |  | 				return callback(err || new Error('[[error:not-invited]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			next(socket, data, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.grant = isOwner(function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | 	groups.ownership.grant(data.toUid, data.groupName, callback); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.rescind = isOwner(function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | 	groups.ownership.rescind(data.toUid, data.groupName, callback); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.accept = isOwner(function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | 	groups.acceptMembership(data.groupName, data.toUid, callback); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.reject = isOwner(function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | 	groups.rejectMembership(data.groupName, data.toUid, callback); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.acceptAll = isOwner(function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | 	acceptRejectAll(groups.acceptMembership, socket, data, callback); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.rejectAll = isOwner(function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | 	acceptRejectAll(groups.rejectMembership, socket, data, callback); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function acceptRejectAll(method, socket, data, callback) { | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | 			groups.getPending(data.groupName, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		function (uids, next) { | 
					
						
							|  |  |  | 			async.each(uids, function (uid, next) { | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | 				method(data.groupName, uid, next); | 
					
						
							|  |  |  | 			}, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-15 16:58:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.issueInvite = isOwner(function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | 	groups.invite(data.groupName, data.toUid, callback); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2015-07-15 16:58:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.issueMassInvite = isOwner(function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2016-09-26 16:55:38 +03:00
										 |  |  | 	if (!data || !data.usernames || !data.groupName) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var usernames = data.usernames.split(','); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	usernames = usernames.map(function (username) { | 
					
						
							| 
									
										
										
										
											2016-09-26 16:55:38 +03:00
										 |  |  | 		return username && username.trim(); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	user.getUidsByUsernames(usernames, function (err, uids) { | 
					
						
							| 
									
										
										
										
											2016-09-26 16:55:38 +03:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		uids = uids.filter(function (uid) { | 
					
						
							| 
									
										
										
										
											2016-09-26 16:55:38 +03:00
										 |  |  | 			return !!uid && parseInt(uid, 10); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		async.eachSeries(uids, function (uid, next) { | 
					
						
							| 
									
										
										
										
											2016-09-29 21:58:18 +03:00
										 |  |  | 			groups.invite(data.groupName, uid, next); | 
					
						
							| 
									
										
										
										
											2016-09-26 16:55:38 +03:00
										 |  |  | 		}, callback); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.rescindInvite = isOwner(function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | 	groups.rejectMembership(data.groupName, data.toUid, callback); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2015-06-09 17:47:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.acceptInvite = isInvited(function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-17 20:55:18 -04:00
										 |  |  | 	groups.acceptMembership(data.groupName, socket.uid, callback); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2015-02-23 16:46:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.rejectInvite = isInvited(function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-17 20:55:18 -04:00
										 |  |  | 	groups.rejectMembership(data.groupName, socket.uid, callback); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2015-02-23 16:46:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.update = isOwner(function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | 	groups.update(data.groupName, data.values, callback); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.kick = isOwner(function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-12-18 13:32:53 +02:00
										 |  |  | 	if (socket.uid === parseInt(data.uid, 10)) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:cant-kick-self]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-03-21 16:48:07 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	groups.ownership.isOwner(data.uid, data.groupName, function (err, isOwner) { | 
					
						
							| 
									
										
										
										
											2016-03-10 18:59:49 +00:00
										 |  |  | 		if (err) { | 
					
						
							| 
									
										
										
										
											2016-03-10 19:46:50 +00:00
										 |  |  | 			return callback(err); | 
					
						
							| 
									
										
										
										
											2016-03-10 18:59:49 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		groups.kick(data.uid, data.groupName, isOwner, callback); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2015-01-09 13:51:27 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.create = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-17 19:57:47 -04:00
										 |  |  | 	if (!socket.uid) { | 
					
						
							| 
									
										
										
										
											2015-01-12 13:00:23 -05:00
										 |  |  | 		return callback(new Error('[[error:no-privileges]]')); | 
					
						
							| 
									
										
										
										
											2015-02-05 19:38:51 -05:00
										 |  |  | 	} else if (parseInt(meta.config.allowGroupCreation, 10) !== 1) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:group-creation-disabled]]')); | 
					
						
							| 
									
										
										
										
											2016-06-22 22:07:20 +03:00
										 |  |  | 	} else if (groups.isPrivilegeGroup(data.name)) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-group-name]]')); | 
					
						
							| 
									
										
										
										
											2015-01-12 13:00:23 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-05 19:38:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-12 13:00:23 -05:00
										 |  |  | 	data.ownerUid = socket.uid; | 
					
						
							|  |  |  | 	groups.create(data, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.delete = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2016-03-21 16:48:07 +02:00
										 |  |  | 	if (data.groupName === 'administrators' || | 
					
						
							|  |  |  | 		data.groupName === 'registered-users' || | 
					
						
							|  |  |  | 		data.groupName === 'Global Moderators') { | 
					
						
							| 
									
										
										
										
											2015-07-08 12:01:06 -04:00
										 |  |  | 		return callback(new Error('[[error:not-allowed]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-21 16:48:07 +02:00
										 |  |  | 	async.parallel({ | 
					
						
							| 
									
										
										
										
											2015-07-08 12:01:06 -04:00
										 |  |  | 		isOwner: async.apply(groups.ownership.isOwner, socket.uid, data.groupName), | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		isAdmin: async.apply(user.isAdministrator, socket.uid), | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	}, function (err, checks) { | 
					
						
							| 
									
										
										
										
											2015-07-08 12:01:06 -04:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-01-23 07:27:49 -05:00
										 |  |  | 		if (!checks.isOwner && !checks.isAdmin) { | 
					
						
							| 
									
										
										
										
											2015-01-11 18:17:49 -05:00
										 |  |  | 			return callback(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		groups.destroy(data.groupName, callback); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.search = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-07-09 14:01:06 -04:00
										 |  |  | 	data.options = data.options || {}; | 
					
						
							| 
									
										
										
										
											2015-09-17 19:57:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 16:20:10 -04:00
										 |  |  | 	if (!data.query) { | 
					
						
							| 
									
										
										
										
											2015-06-24 17:15:58 -04:00
										 |  |  | 		var groupsPerPage = 15; | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 		groupsController.getGroupsFromSet(socket.uid, data.options.sort, 0, groupsPerPage - 1, function (err, data) { | 
					
						
							| 
									
										
										
										
											2015-06-02 16:20:10 -04:00
										 |  |  | 			callback(err, !err ? data.groups : null); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-01 17:21:09 +03:00
										 |  |  | 	groups.search(data.query, data.options, callback); | 
					
						
							| 
									
										
										
										
											2015-06-02 16:20:10 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.loadMore = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2017-02-18 01:52:56 -07:00
										 |  |  | 	if (!data.sort || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0) { | 
					
						
							| 
									
										
										
										
											2015-06-02 16:20:10 -04:00
										 |  |  | 		return callback(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var groupsPerPage = 9; | 
					
						
							| 
									
										
										
										
											2016-08-13 12:50:06 +00:00
										 |  |  | 	var start = parseInt(data.after, 10); | 
					
						
							| 
									
										
										
										
											2015-06-02 16:20:10 -04:00
										 |  |  | 	var stop = start + groupsPerPage - 1; | 
					
						
							|  |  |  | 	groupsController.getGroupsFromSet(socket.uid, data.sort, start, stop, callback); | 
					
						
							| 
									
										
										
										
											2015-01-21 15:43:05 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.searchMembers = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-03-10 14:09:24 -04:00
										 |  |  | 	data.uid = socket.uid; | 
					
						
							|  |  |  | 	groups.searchMembers(data, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.loadMoreMembers = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2016-08-11 12:36:27 +00:00
										 |  |  | 	if (!data.groupName || !utils.isNumber(data.after) || parseInt(data.after, 10) < 0) { | 
					
						
							| 
									
										
										
										
											2015-07-03 16:42:48 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	data.after = parseInt(data.after, 10); | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 	user.getUsersFromSet('group:' + data.groupName + ':members', socket.uid, data.after, data.after + 9, function (err, users) { | 
					
						
							| 
									
										
										
										
											2015-07-03 16:42:48 -04:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 		callback(null, { users: users, nextStart: data.after + 10 }); | 
					
						
							| 
									
										
										
										
											2015-07-03 16:42:48 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-12 20:34:15 -05:00
										 |  |  | SocketGroups.cover = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.cover.update = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-17 19:57:47 -04:00
										 |  |  | 	if (!socket.uid) { | 
					
						
							| 
									
										
										
										
											2015-01-12 20:34:15 -05:00
										 |  |  | 		return callback(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-02 14:05:54 +03:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			groups.ownership.isOwner(socket.uid, data.groupName, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (isOwner, next) { | 
					
						
							|  |  |  | 			if (!isOwner) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-01-12 20:34:15 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-02 14:05:54 +03:00
										 |  |  | 			groups.updateCover(socket.uid, data, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-12-02 14:05:54 +03:00
										 |  |  | 	], callback); | 
					
						
							| 
									
										
										
										
											2015-01-12 20:34:15 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketGroups.cover.remove = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-11-06 10:50:14 -05:00
										 |  |  | 	if (!socket.uid) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-02 14:05:54 +03:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			groups.ownership.isOwner(socket.uid, data.groupName, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (isOwner, next) { | 
					
						
							|  |  |  | 			if (!isOwner) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-11-06 10:50:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-12-02 14:05:54 +03:00
										 |  |  | 			groups.removeCover(data, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-12-02 14:05:54 +03:00
										 |  |  | 	], callback); | 
					
						
							| 
									
										
										
										
											2015-12-29 10:34:06 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-11-06 10:50:14 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-07 16:55:14 -05:00
										 |  |  | module.exports = SocketGroups; |