| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-06 12:34:35 -04:00
										 |  |  | var	meta = require('../meta'), | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 	Messaging = require('../messaging'), | 
					
						
							| 
									
										
										
										
											2014-07-08 11:55:55 -04:00
										 |  |  | 	utils = require('../../public/src/utils'), | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-24 14:25:23 -04:00
										 |  |  | 	async = require('async'), | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 20:14:09 -05:00
										 |  |  | 	server = require('./'), | 
					
						
							| 
									
										
										
										
											2014-11-25 23:08:28 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-14 00:34:51 -04:00
										 |  |  | 	SocketModules = { | 
					
						
							|  |  |  | 		chats: {}, | 
					
						
							|  |  |  | 		sounds: {}, | 
					
						
							|  |  |  | 		settings: {} | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Chat */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:28:21 -05:00
										 |  |  | SocketModules.chats.get = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-17 12:55:38 -05:00
										 |  |  | 	if(!data) { | 
					
						
							| 
									
										
										
										
											2014-04-09 21:36:57 -04:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							| 
									
										
										
										
											2014-01-17 12:55:38 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-27 17:32:33 -04:00
										 |  |  | 	Messaging.getMessages({ | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 		uid: socket.uid, | 
					
						
							|  |  |  | 		roomId: data.roomId, | 
					
						
							| 
									
										
										
										
											2015-08-27 17:32:33 -04:00
										 |  |  | 		since: data.since, | 
					
						
							|  |  |  | 		isNew: false | 
					
						
							|  |  |  | 	}, callback); | 
					
						
							| 
									
										
										
										
											2015-12-14 14:13:18 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 17:50:30 +02:00
										 |  |  | 	Messaging.markRead(socket.uid, data.roomId); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | SocketModules.chats.getRaw = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if(!data || !data.hasOwnProperty('mid')) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Messaging.getMessageField(data.mid, 'content', callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | SocketModules.chats.send = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-12-15 20:00:51 +02:00
										 |  |  | 	if (!data || !data.roomId) { | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 20:00:51 +02:00
										 |  |  | 	var now = Date.now(); | 
					
						
							| 
									
										
										
										
											2014-11-13 15:47:25 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-16 20:32:16 -04:00
										 |  |  | 	// Websocket rate limiting
 | 
					
						
							| 
									
										
										
										
											2014-10-15 14:49:11 -04:00
										 |  |  | 	socket.lastChatMessageTime = socket.lastChatMessageTime || 0; | 
					
						
							|  |  |  | 	if (now - socket.lastChatMessageTime < 200) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:too-many-messages]]')); | 
					
						
							| 
									
										
										
										
											2015-04-16 20:32:16 -04:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		socket.lastChatMessageTime = now; | 
					
						
							| 
									
										
										
										
											2014-10-15 14:49:11 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-07-23 18:23:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 20:00:51 +02:00
										 |  |  | 	Messaging.canMessage(socket.uid, data.roomId, function(err, allowed) { | 
					
						
							| 
									
										
										
										
											2015-04-16 20:32:16 -04:00
										 |  |  | 		if (err || !allowed) { | 
					
						
							|  |  |  | 			return callback(err || new Error('[[error:chat-restricted]]')); | 
					
						
							| 
									
										
										
										
											2014-11-13 16:03:11 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 20:00:51 +02:00
										 |  |  | 		Messaging.sendMessage(socket.uid, data.roomId, data.message, now, function(err, message) { | 
					
						
							| 
									
										
										
										
											2015-04-16 20:32:16 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2014-10-03 15:28:46 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-11-13 15:49:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-15 20:00:51 +02:00
										 |  |  | 			Messaging.notifyUser(socket.uid, data.roomId, message); | 
					
						
							| 
									
										
										
										
											2014-11-13 15:49:43 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-16 20:32:16 -04:00
										 |  |  | 			callback(); | 
					
						
							| 
									
										
										
										
											2014-11-13 15:49:43 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-10-30 17:50:07 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-10-03 15:28:46 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | SocketModules.chats.edit = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2015-12-15 19:39:45 +02:00
										 |  |  | 	if (!data || !data.roomId) { | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Messaging.canEdit(data.mid, socket.uid, function(err, allowed) { | 
					
						
							| 
									
										
										
										
											2015-12-15 19:39:45 +02:00
										 |  |  | 		if (err || !allowed) { | 
					
						
							|  |  |  | 			return callback(err || new Error('[[error:cant-edit-chat-message]]')); | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-12-15 19:39:45 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Messaging.editMessage(socket.uid, data.mid, data.roomId, data.message, callback); | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05: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]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Messaging.canEdit(data.messageId, socket.uid, function(err, allowed) { | 
					
						
							|  |  |  | 		if (allowed) { | 
					
						
							| 
									
										
										
										
											2015-12-15 14:10:32 +02:00
										 |  |  | 			Messaging.deleteMessage(data.messageId, data.roomId, callback); | 
					
						
							| 
									
										
										
										
											2015-12-11 12:07:02 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-30 17:50:07 -04:00
										 |  |  | SocketModules.chats.canMessage = function(socket, toUid, callback) { | 
					
						
							|  |  |  | 	Messaging.canMessage(socket.uid, toUid, function(err, allowed) { | 
					
						
							|  |  |  | 		callback(!allowed ? new Error('[[error:chat-restricted]]') : undefined); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | SocketModules.chats.markRead = function(socket, touid, callback) { | 
					
						
							|  |  |  | 	Messaging.markRead(socket.uid, touid, function(err) { | 
					
						
							| 
									
										
										
										
											2015-10-02 17:31:18 -04:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-10-02 17:31:18 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		Messaging.pushUnreadCount(socket.uid); | 
					
						
							|  |  |  | 		callback(); | 
					
						
							| 
									
										
										
										
											2014-07-19 10:33:27 -04:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-15 12:48:28 -04:00
										 |  |  | SocketModules.chats.userStartTyping = function(socket, data, callback) { | 
					
						
							|  |  |  | 	sendTypingNotification('event:chats.userStartTyping', socket, data, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketModules.chats.userStopTyping = function(socket, data, callback) { | 
					
						
							|  |  |  | 	sendTypingNotification('event:chats.userStopTyping', socket, data, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function sendTypingNotification(event, socket, data, callback) { | 
					
						
							|  |  |  | 	if (!socket.uid || !data) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-09-03 18:07:56 -04:00
										 |  |  | 	server.in('uid_' + data.touid).emit(event, data.fromUid); | 
					
						
							| 
									
										
										
										
											2014-04-15 12:48:28 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-15 16:26:25 -04:00
										 |  |  | SocketModules.chats.getRecentChats = function(socket, data, callback) { | 
					
						
							|  |  |  | 	if (!data || !utils.isNumber(data.after)) { | 
					
						
							|  |  |  | 		return callback(new Error('[[error:invalid-data]]')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	var start = parseInt(data.after, 10), | 
					
						
							| 
									
										
										
										
											2015-03-31 23:40:58 -04:00
										 |  |  | 		stop = start + 9; | 
					
						
							| 
									
										
										
										
											2014-09-15 16:26:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-31 23:40:58 -04:00
										 |  |  | 	Messaging.getRecentChats(socket.uid, start, stop, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-13 17:15:07 -04:00
										 |  |  | /* Sounds */ | 
					
						
							| 
									
										
										
										
											2014-03-14 00:34:51 -04:00
										 |  |  | SocketModules.sounds.getSounds = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-03-13 17:15:07 -04:00
										 |  |  | 	// Read sounds from local directory
 | 
					
						
							| 
									
										
										
										
											2014-04-12 18:33:52 -04:00
										 |  |  | 	meta.sounds.getFiles(callback); | 
					
						
							| 
									
										
										
										
											2014-03-13 17:15:07 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-14 00:34:51 -04:00
										 |  |  | SocketModules.sounds.getMapping = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-03-13 17:15:07 -04:00
										 |  |  | 	meta.sounds.getMapping(callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-10-20 18:47:34 -04:00
										 |  |  | SocketModules.sounds.getData = function(socket, data, callback) { | 
					
						
							|  |  |  | 	async.parallel({ | 
					
						
							|  |  |  | 		mapping: async.apply(meta.sounds.getMapping), | 
					
						
							|  |  |  | 		files: async.apply(meta.sounds.getFiles) | 
					
						
							|  |  |  | 	}, callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | module.exports = SocketModules; |