| 
									
										
										
										
											2017-02-18 01:56:23 -07:00
										 |  |  | 'use strict'; | 
					
						
							| 
									
										
										
										
											2017-02-18 02:28:54 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | var async = require('async'); | 
					
						
							| 
									
										
										
										
											2015-12-23 11:27:34 +02:00
										 |  |  | var validator = require('validator'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-14 10:08:50 -04:00
										 |  |  | var db = require('../database'); | 
					
						
							| 
									
										
										
										
											2015-12-21 10:36:44 +02:00
										 |  |  | var meta = require('../meta'); | 
					
						
							| 
									
										
										
										
											2016-01-18 13:57:02 -05:00
										 |  |  | var notifications = require('../notifications'); | 
					
						
							| 
									
										
										
										
											2016-07-13 17:33:04 -04:00
										 |  |  | var plugins = require('../plugins'); | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | var Messaging = require('../messaging'); | 
					
						
							| 
									
										
										
										
											2017-04-08 20:22:21 -06:00
										 |  |  | var utils = require('../utils'); | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | var server = require('./'); | 
					
						
							|  |  |  | var user = require('../user'); | 
					
						
							| 
									
										
										
										
											2017-12-18 15:43:57 -05:00
										 |  |  | var privileges = require('../privileges'); | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-20 20:03:41 -04:00
										 |  |  | var SocketModules = module.exports; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketModules.chats = {}; | 
					
						
							|  |  |  | SocketModules.sounds = {}; | 
					
						
							|  |  |  | SocketModules.settings = {}; | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Chat */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.getRaw = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2017-11-29 11:46:30 -05:00
										 |  |  | 	if (!data || !data.hasOwnProperty('mid')) { | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-17 11:22:15 +02:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2017-11-29 11:46:30 -05:00
										 |  |  | 			Messaging.getMessageField(data.mid, 'roomId', next); | 
					
						
							| 
									
										
										
										
											2015-12-17 11:22:15 +02:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-11-29 11:46:30 -05:00
										 |  |  | 		function (roomId, next) { | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				isAdmin: function (next) { | 
					
						
							|  |  |  | 					user.isAdministrator(socket.uid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				hasMessage: function (next) { | 
					
						
							|  |  |  | 					db.isSortedSetMember('uid:' + socket.uid + ':chat:room:' + roomId + ':mids', data.mid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				inRoom: function (next) { | 
					
						
							|  |  |  | 					Messaging.isUserInRoom(socket.uid, roomId, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (results, next) { | 
					
						
							|  |  |  | 			if (!results.isAdmin && (!results.inRoom || !results.hasMessage)) { | 
					
						
							| 
									
										
										
										
											2015-12-17 11:22:15 +02:00
										 |  |  | 				return next(new Error('[[error:not-allowed]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-11-29 11:46:30 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-17 11:22:15 +02:00
										 |  |  | 			Messaging.getMessageField(data.mid, 'content', next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2015-12-17 11:22:15 +02:00
										 |  |  | 	], callback); | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-14 10:08:50 -04:00
										 |  |  | SocketModules.chats.isDnD = function (socket, uid, callback) { | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			db.getObjectField('user:' + uid, 'status', next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (status, next) { | 
					
						
							|  |  |  | 			next(null, status === 'dnd'); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.newRoom = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 	if (!data) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-07 18:48:00 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (rateLimitExceeded(socket)) { | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 		return callback(new Error('[[error:too-many-messages]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-20 20:03:41 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2017-12-18 15:43:57 -05:00
										 |  |  | 			privileges.global.can('chat', socket.uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (canChat, next) { | 
					
						
							|  |  |  | 			if (!canChat) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2017-05-20 20:03:41 -04:00
										 |  |  | 			Messaging.canMessageUser(socket.uid, data.touid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			Messaging.newRoom(socket.uid, [data.touid], next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.send = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | 	if (!data || !data.roomId || !socket.uid) { | 
					
						
							| 
									
										
										
										
											2014-04-09 21:36:57 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-01-17 12:55:38 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 18:48:00 +03:00
										 |  |  | 	if (rateLimitExceeded(socket)) { | 
					
						
							| 
									
										
										
										
											2014-10-15 14:49:11 -04:00
										 |  |  | 		return callback(new Error('[[error:too-many-messages]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-23 18:23:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-02-01 21:09:35 +02:00
										 |  |  | 	async.waterfall([ | 
					
						
							| 
									
										
										
										
											2016-07-13 17:33:04 -04:00
										 |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2017-12-18 15:43:57 -05:00
										 |  |  | 			privileges.global.can('chat', socket.uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (canChat, next) { | 
					
						
							|  |  |  | 			if (!canChat) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-07-13 17:33:04 -04:00
										 |  |  | 			plugins.fireHook('filter:messaging.send', { | 
					
						
							|  |  |  | 				data: data, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				uid: socket.uid, | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			}, function (err, results) { | 
					
						
							| 
									
										
										
										
											2016-07-13 17:33:04 -04:00
										 |  |  | 				data = results.data; | 
					
						
							|  |  |  | 				next(err); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-02-01 21:09:35 +02:00
										 |  |  | 		function (next) { | 
					
						
							|  |  |  | 			Messaging.canMessageRoom(socket.uid, data.roomId, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2018-05-31 15:05:12 -04:00
										 |  |  | 			Messaging.sendMessage({ | 
					
						
							|  |  |  | 				uid: socket.uid, | 
					
						
							|  |  |  | 				roomId: data.roomId, | 
					
						
							|  |  |  | 				content: data.message, | 
					
						
							|  |  |  | 				timestamp: Date.now(), | 
					
						
							|  |  |  | 				ip: socket.ip, | 
					
						
							|  |  |  | 			}, next); | 
					
						
							| 
									
										
										
										
											2016-02-01 21:09:35 +02:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		function (message, next) { | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | 			Messaging.notifyUsersInRoom(socket.uid, data.roomId, message); | 
					
						
							| 
									
										
										
										
											2016-02-01 21:09:35 +02:00
										 |  |  | 			user.updateOnlineUsers(socket.uid); | 
					
						
							| 
									
										
										
										
											2016-11-08 19:59:19 +03:00
										 |  |  | 			next(null, message); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-02-01 21:09:35 +02:00
										 |  |  | 	], callback); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:50:07 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-10-03 15:28:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 18:48:00 +03:00
										 |  |  | function rateLimitExceeded(socket) { | 
					
						
							|  |  |  | 	var now = Date.now(); | 
					
						
							|  |  |  | 	socket.lastChatMessageTime = socket.lastChatMessageTime || 0; | 
					
						
							|  |  |  | 	var delay = meta.config.hasOwnProperty('chatMessageDelay') ? parseInt(meta.config.chatMessageDelay, 10) : 200; | 
					
						
							|  |  |  | 	if (now - socket.lastChatMessageTime < delay) { | 
					
						
							|  |  |  | 		return true; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-02-18 14:27:26 -07:00
										 |  |  | 	socket.lastChatMessageTime = now; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-07 18:48:00 +03:00
										 |  |  | 	return false; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.loadRoom = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 	if (!data || !data.roomId) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-12-23 11:27:34 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-01 13:12:28 -04:00
										 |  |  | 	Messaging.loadRoom(socket.uid, data, callback); | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-06 12:03:52 -05:00
										 |  |  | SocketModules.chats.getUsersInRoom = function (socket, data, callback) { | 
					
						
							|  |  |  | 	if (!data || !data.roomId) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-30 12:35:17 -04:00
										 |  |  | 	async.parallel({ | 
					
						
							|  |  |  | 		users: async.apply(Messaging.getUsersInRoom, data.roomId, 0, -1), | 
					
						
							|  |  |  | 		isOwner: async.apply(Messaging.isRoomOwner, socket.uid, data.roomId), | 
					
						
							|  |  |  | 	}, function (err, payload) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		payload.users = payload.users.map((user) => { | 
					
						
							| 
									
										
										
										
											2018-04-30 12:49:42 -04:00
										 |  |  | 			user.canKick = (parseInt(user.uid, 10) !== parseInt(socket.uid, 10)) && payload.isOwner; | 
					
						
							| 
									
										
										
										
											2018-04-30 12:35:17 -04:00
										 |  |  | 			return user; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		callback(null, payload.users); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2018-01-06 12:03:52 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.addUserToRoom = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 	if (!data || !data.roomId || !data.username) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-14 09:20:51 +03:00
										 |  |  | 	var uid; | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 	async.waterfall([ | 
					
						
							| 
									
										
										
										
											2015-12-24 09:52:25 +02:00
										 |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2017-12-18 15:43:57 -05:00
										 |  |  | 			privileges.global.can('chat', socket.uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (canChat, next) { | 
					
						
							|  |  |  | 			if (!canChat) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:no-privileges]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-24 09:52:25 +02:00
										 |  |  | 			Messaging.getUserCountInRoom(data.roomId, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (userCount, next) { | 
					
						
							|  |  |  | 			var maxUsers = parseInt(meta.config.maximumUsersInChatRoom, 10) || 0; | 
					
						
							|  |  |  | 			if (maxUsers && userCount >= maxUsers) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:cant-add-more-users-to-chat-room]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			next(); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 		function (next) { | 
					
						
							|  |  |  | 			user.getUidByUsername(data.username, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-10-14 09:20:51 +03:00
										 |  |  | 		function (_uid, next) { | 
					
						
							|  |  |  | 			uid = _uid; | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 			if (!uid) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:no-user]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2015-12-23 13:00:43 +02:00
										 |  |  | 			if (socket.uid === parseInt(uid, 10)) { | 
					
						
							| 
									
										
										
										
											2018-01-09 16:30:16 -05:00
										 |  |  | 				return next(new Error('[[error:cant-chat-with-yourself]]')); | 
					
						
							| 
									
										
										
										
											2015-12-23 13:00:43 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2016-10-14 09:20:51 +03:00
										 |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				settings: async.apply(user.getSettings, uid), | 
					
						
							| 
									
										
										
										
											2016-10-22 10:24:25 +03:00
										 |  |  | 				isAdminOrGlobalMod: async.apply(user.isAdminOrGlobalMod, socket.uid), | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 				isFollowing: async.apply(user.isFollowing, uid, socket.uid), | 
					
						
							| 
									
										
										
										
											2016-10-14 09:20:51 +03:00
										 |  |  | 			}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (results, next) { | 
					
						
							| 
									
										
										
										
											2016-10-22 10:24:25 +03:00
										 |  |  | 			if (results.settings.restrictChat && !results.isAdminOrGlobalMod && !results.isFollowing) { | 
					
						
							| 
									
										
										
										
											2016-10-14 09:20:51 +03:00
										 |  |  | 				return next(new Error('[[error:chat-restricted]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 			Messaging.addUsersToRoom(socket.uid, [uid], data.roomId, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.removeUserFromRoom = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 	if (!data || !data.roomId) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-30 12:35:17 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2018-04-30 12:35:17 -04:00
										 |  |  | 			user.exists(data.uid, next); | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2018-04-30 12:35:17 -04:00
										 |  |  | 		function (exists, next) { | 
					
						
							|  |  |  | 			if (!exists) { | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 				return next(new Error('[[error:no-user]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-30 12:35:17 -04:00
										 |  |  | 			Messaging.removeUsersFromRoom(socket.uid, [data.uid], data.roomId, next); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2015-12-16 13:35:24 +02:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.leave = function (socket, roomid, callback) { | 
					
						
							| 
									
										
										
										
											2016-01-08 12:34:09 +02:00
										 |  |  | 	if (!socket.uid || !roomid) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Messaging.leaveRoom([socket.uid], roomid, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.edit = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2017-05-20 20:03:41 -04:00
										 |  |  | 	if (!data || !data.roomId || !data.message) { | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-20 20:03:41 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			Messaging.canEdit(data.mid, socket.uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-12-11 10:53:29 -05:00
										 |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2017-05-20 20:03:41 -04:00
										 |  |  | 			Messaging.editMessage(socket.uid, data.mid, data.roomId, data.message, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.delete = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 	if (!data || !data.roomId || !data.messageId) { | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-20 20:03:41 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2017-12-11 10:53:29 -05:00
										 |  |  | 			Messaging.canDelete(data.messageId, socket.uid, next); | 
					
						
							| 
									
										
										
										
											2017-05-20 20:03:41 -04:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2017-12-11 10:53:29 -05:00
										 |  |  | 		function (next) { | 
					
						
							| 
									
										
										
										
											2017-05-20 20:03:41 -04:00
										 |  |  | 			Messaging.deleteMessage(data.messageId, data.roomId, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							| 
									
										
										
										
											2015-12-16 11:15:43 +02:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-12-18 14:45:06 -05:00
										 |  |  | SocketModules.chats.restore = function (socket, data, callback) { | 
					
						
							|  |  |  | 	if (!data || !data.roomId || !data.messageId) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			Messaging.canDelete(data.messageId, socket.uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			Messaging.restoreMessage(data.messageId, data.roomId, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.canMessage = function (socket, roomId, callback) { | 
					
						
							| 
									
										
										
										
											2016-02-01 21:09:35 +02:00
										 |  |  | 	Messaging.canMessageRoom(socket.uid, roomId, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.markRead = function (socket, roomId, callback) { | 
					
						
							| 
									
										
										
										
											2017-05-20 20:03:41 -04:00
										 |  |  | 	if (!socket.uid || !roomId) { | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2017-05-20 20:03:41 -04:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				uidsInRoom: async.apply(Messaging.getUidsInRoom, roomId, 0, -1), | 
					
						
							|  |  |  | 				markRead: async.apply(Messaging.markRead, socket.uid, roomId), | 
					
						
							|  |  |  | 			}, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (results, next) { | 
					
						
							|  |  |  | 			Messaging.pushUnreadCount(socket.uid); | 
					
						
							|  |  |  | 			server.in('uid_' + socket.uid).emit('event:chats.markedAsRead', { roomId: roomId }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-20 14:40:48 -04:00
										 |  |  | 			if (!results.uidsInRoom.includes(String(socket.uid))) { | 
					
						
							| 
									
										
										
										
											2017-05-20 20:03:41 -04:00
										 |  |  | 				return callback(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Mark notification read
 | 
					
						
							|  |  |  | 			var nids = results.uidsInRoom.filter(function (uid) { | 
					
						
							|  |  |  | 				return parseInt(uid, 10) !== socket.uid; | 
					
						
							|  |  |  | 			}).map(function (uid) { | 
					
						
							|  |  |  | 				return 'chat_' + uid + '_' + roomId; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			notifications.markReadMultiple(nids, socket.uid, function () { | 
					
						
							|  |  |  | 				user.notifications.pushCount(socket.uid); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			next(); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.markAllRead = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2016-03-11 13:38:52 +02:00
										 |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			Messaging.markAllRead(socket.uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			Messaging.pushUnreadCount(socket.uid); | 
					
						
							|  |  |  | 			next(); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2016-03-11 13:38:52 +02:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.renameRoom = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2017-05-20 20:03:41 -04:00
										 |  |  | 	if (!data || !data.roomId || !data.newName) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2015-12-23 11:27:34 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			Messaging.renameRoom(socket.uid, data.roomId, data.newName, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			Messaging.getUidsInRoom(data.roomId, 0, -1, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (uids, next) { | 
					
						
							| 
									
										
										
										
											2017-02-18 12:30:49 -07:00
										 |  |  | 			var eventData = { roomId: data.roomId, newName: validator.escape(String(data.newName)) }; | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | 			uids.forEach(function (uid) { | 
					
						
							| 
									
										
										
										
											2015-12-23 11:27:34 +02:00
										 |  |  | 				server.in('uid_' + uid).emit('event:chats.roomRename', eventData); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 			next(); | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2015-12-23 11:27:34 +02:00
										 |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.getRecentChats = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | 	if (!data || !utils.isNumber(data.after) || !utils.isNumber(data.uid)) { | 
					
						
							| 
									
										
										
										
											2014-09-15 16:26:25 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-20 14:41:53 +03:00
										 |  |  | 	var start = parseInt(data.after, 10); | 
					
						
							|  |  |  | 	var stop = start + 9; | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | 	Messaging.getRecentChats(socket.uid, data.uid, start, stop, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.hasPrivateChat = function (socket, uid, callback) { | 
					
						
							| 
									
										
										
										
											2016-01-18 15:35:24 +02:00
										 |  |  | 	if (!socket.uid || !uid) { | 
					
						
							| 
									
										
										
										
											2017-05-20 20:03:41 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2016-01-18 15:35:24 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	Messaging.hasPrivateChat(socket.uid, uid, callback); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-13 11:43:39 +02:00
										 |  |  | SocketModules.chats.getMessages = function (socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2017-05-20 20:03:41 -04:00
										 |  |  | 	if (!socket.uid || !data || !data.uid || !data.roomId) { | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:50 +03:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-10-02 15:17:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:50 +03:00
										 |  |  | 	var params = { | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | 		callerUid: socket.uid, | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:50 +03:00
										 |  |  | 		uid: data.uid, | 
					
						
							|  |  |  | 		roomId: data.roomId, | 
					
						
							| 
									
										
										
										
											2016-10-02 15:17:22 +03:00
										 |  |  | 		start: parseInt(data.start, 10) || 0, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		count: 50, | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:50 +03:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2016-10-02 15:17:22 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-03 20:35:36 +03:00
										 |  |  | 	Messaging.getMessages(params, callback); | 
					
						
							| 
									
										
										
										
											2016-09-20 16:58:50 +03:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-31 15:05:12 -04:00
										 |  |  | SocketModules.chats.getIP = function (socket, mid, callback) { | 
					
						
							|  |  |  | 	async.waterfall([ | 
					
						
							|  |  |  | 		function (next) { | 
					
						
							|  |  |  | 			user.isAdminOrGlobalMod(socket.uid, next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		function (allowed, next) { | 
					
						
							|  |  |  | 			if (!allowed) { | 
					
						
							|  |  |  | 				return next(new Error('[[error:no-privilege]]')); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			Messaging.getMessageField(mid, 'ip', next); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	], callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-13 17:15:07 -04:00
										 |  |  | /* Sounds */ | 
					
						
							| 
									
										
										
										
											2017-02-16 22:56:25 -07:00
										 |  |  | SocketModules.sounds.getUserSoundMap = function getUserSoundMap(socket, data, callback) { | 
					
						
							|  |  |  | 	meta.sounds.getUserSoundMap(socket.uid, callback); | 
					
						
							| 
									
										
										
										
											2015-10-20 18:47:34 -04:00
										 |  |  | }; |