| 
									
										
										
										
											2015-01-07 16:55:14 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-02 16:20:10 -04:00
										 |  |  | var	async = require('async'), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	groups = require('../groups'), | 
					
						
							| 
									
										
										
										
											2015-01-08 16:50:31 -05:00
										 |  |  | 	meta = require('../meta'), | 
					
						
							| 
									
										
										
										
											2015-01-22 16:01:17 -05:00
										 |  |  | 	user = require('../user'), | 
					
						
							| 
									
										
										
										
											2015-06-02 16:20:10 -04:00
										 |  |  | 	groupsController = require('../controllers/groups'), | 
					
						
							| 
									
										
										
										
											2015-01-07 16:55:14 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	SocketGroups = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 19:57:47 -04: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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 19:57:47 -04: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-01-11 17:44:51 -05:00
										 |  |  | 	if (meta.config.allowPrivateGroups !== '0') { | 
					
						
							| 
									
										
										
										
											2015-01-22 16:01:17 -05:00
										 |  |  | 		async.parallel({ | 
					
						
							|  |  |  | 			isAdmin: async.apply(user.isAdministrator, socket.uid), | 
					
						
							|  |  |  | 			isPrivate: async.apply(groups.isPrivate, data.groupName) | 
					
						
							|  |  |  | 		}, function(err, checks) { | 
					
						
							|  |  |  | 			if (checks.isPrivate && !checks.isAdmin) { | 
					
						
							| 
									
										
										
										
											2015-01-08 16:50:31 -05:00
										 |  |  | 				groups.requestMembership(data.groupName, socket.uid, callback); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				groups.join(data.groupName, socket.uid, callback); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		groups.join(data.groupName, socket.uid, callback); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-01-07 16:55:14 -05: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) { | 
					
						
							|  |  |  | 		groups.ownership.isOwner(socket.uid, data.groupName, function(err, isOwner) { | 
					
						
							|  |  |  | 			if (err || !isOwner) { | 
					
						
							|  |  |  | 				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) { | 
					
						
							|  |  |  | 		groups.isInvited(socket.uid, data.groupName, function(err, invited) { | 
					
						
							|  |  |  | 			if (err || !invited) { | 
					
						
							|  |  |  | 				return callback(err || new Error('[[error:not-invited]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			next(socket, data, callback); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | SocketGroups.grant = isOwner(function(socket, data, callback) { | 
					
						
							|  |  |  | 	groups.ownership.grant(data.toUid, data.groupName, callback); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketGroups.rescind = isOwner(function(socket, data, callback) { | 
					
						
							|  |  |  | 	groups.ownership.rescind(data.toUid, data.groupName, callback); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketGroups.accept = isOwner(function(socket, data, callback) { | 
					
						
							|  |  |  | 	groups.acceptMembership(data.groupName, data.toUid, callback); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketGroups.reject = isOwner(function(socket, data, callback) { | 
					
						
							|  |  |  | 	groups.rejectMembership(data.groupName, data.toUid, callback); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketGroups.acceptAll = isOwner(function(socket, data, callback) { | 
					
						
							|  |  |  | 	acceptRejectAll(groups.acceptMembership, socket, data, callback); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketGroups.rejectAll = isOwner(function(socket, data, callback) { | 
					
						
							|  |  |  | 	acceptRejectAll(groups.rejectMembership, socket, data, callback); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function acceptRejectAll(method, socket, data, callback) { | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function(next) { | 
					
						
							|  |  |  | 			groups.getPending(data.groupName, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function(uids, next) { | 
					
						
							|  |  |  | 			async.each(uids, function(uid, next) { | 
					
						
							|  |  |  | 				method(data.groupName, uid, next); | 
					
						
							|  |  |  | 			}, next); | 
					
						
							| 
									
										
										
										
											2015-07-15 16:58:25 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2015-07-15 16:58:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | SocketGroups.issueInvite = isOwner(function(socket, data, callback) { | 
					
						
							|  |  |  | 	groups.invite(data.groupName, data.toUid, callback); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2015-07-15 16:58:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | SocketGroups.rescindInvite = isOwner(function(socket, data, callback) { | 
					
						
							|  |  |  | 	groups.rejectMembership(data.groupName, data.toUid, callback); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2015-06-09 17:47:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 20:55:18 -04:00
										 |  |  | SocketGroups.acceptInvite = isInvited(function(socket, data, callback) { | 
					
						
							|  |  |  | 	groups.acceptMembership(data.groupName, socket.uid, callback); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2015-02-23 16:46:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 20:55:18 -04:00
										 |  |  | SocketGroups.rejectInvite = isInvited(function(socket, data, callback) { | 
					
						
							|  |  |  | 	groups.rejectMembership(data.groupName, socket.uid, callback); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2015-02-23 16:46:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-17 20:48:40 -04:00
										 |  |  | SocketGroups.update = isOwner(function(socket, data, callback) { | 
					
						
							|  |  |  | 	groups.update(data.groupName, data.values, callback); | 
					
						
							|  |  |  | }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketGroups.kick = isOwner(function(socket, data, callback) { | 
					
						
							|  |  |  | 	groups.leave(data.groupName, data.uid, callback); | 
					
						
							|  |  |  | }); | 
					
						
							| 
									
										
										
										
											2015-01-09 13:51:27 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-12 13:00:23 -05: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]]')); | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-11 18:17:49 -05:00
										 |  |  | SocketGroups.delete = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-07-08 12:01:06 -04:00
										 |  |  | 	if (data.groupName === 'administrators' || data.groupName === 'registered-users') { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:not-allowed]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-23 07:27:49 -05:00
										 |  |  | 	var tasks = { | 
					
						
							| 
									
										
										
										
											2015-07-08 12:01:06 -04:00
										 |  |  | 		isOwner: async.apply(groups.ownership.isOwner, socket.uid, data.groupName), | 
					
						
							|  |  |  | 		isAdmin: async.apply(user.isAdministrator, socket.uid) | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2015-01-23 07:27:49 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	async.parallel(tasks, 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); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-21 15:43:05 -05: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; | 
					
						
							| 
									
										
										
										
											2015-06-02 16:20:10 -04:00
										 |  |  | 		groupsController.getGroupsFromSet(socket.uid, data.options.sort, 0, groupsPerPage - 1, function(err, data) { | 
					
						
							|  |  |  | 			callback(err, !err ? data.groups : null); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	groups.search(data.query, data.options || {}, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketGroups.loadMore = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-17 19:57:47 -04:00
										 |  |  | 	if (!data.sort || !data.after) { | 
					
						
							| 
									
										
										
										
											2015-06-02 16:20:10 -04:00
										 |  |  | 		return callback(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var groupsPerPage = 9; | 
					
						
							|  |  |  | 	var start = parseInt(data.after); | 
					
						
							|  |  |  | 	var stop = start + groupsPerPage - 1; | 
					
						
							|  |  |  | 	groupsController.getGroupsFromSet(socket.uid, data.sort, start, stop, callback); | 
					
						
							| 
									
										
										
										
											2015-01-21 15:43:05 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-10 14:09:24 -04:00
										 |  |  | SocketGroups.searchMembers = function(socket, data, callback) { | 
					
						
							|  |  |  | 	data.uid = socket.uid; | 
					
						
							|  |  |  | 	groups.searchMembers(data, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-03 16:42:48 -04:00
										 |  |  | SocketGroups.loadMoreMembers = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-09-17 19:57:47 -04:00
										 |  |  | 	if (!data.groupName || !parseInt(data.after, 10)) { | 
					
						
							| 
									
										
										
										
											2015-07-03 16:42:48 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	data.after = parseInt(data.after, 10); | 
					
						
							|  |  |  | 	user.getUsersFromSet('group:' + data.groupName + ':members', socket.uid, data.after, data.after + 9, function(err, users) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		callback(null, {users: users, nextStart: data.after + 10}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-12 20:34:15 -05:00
										 |  |  | SocketGroups.cover = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketGroups.cover.get = function(socket, data, callback) { | 
					
						
							|  |  |  | 	groups.getGroupFields(data.groupName, ['cover:url', 'cover:position'], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 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]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	groups.ownership.isOwner(socket.uid, data.groupName, function(err, isOwner) { | 
					
						
							|  |  |  | 		if (!isOwner) { | 
					
						
							|  |  |  | 			return callback(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		groups.updateCover(data, callback); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-07 16:55:14 -05:00
										 |  |  | module.exports = SocketGroups; |