| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var	posts = require('../posts'), | 
					
						
							|  |  |  | 	postTools = require('../postTools'), | 
					
						
							|  |  |  | 	topics = require('../topics'), | 
					
						
							|  |  |  | 	meta = require('../meta'), | 
					
						
							|  |  |  | 	Messaging = require('../messaging'), | 
					
						
							|  |  |  | 	user = require('../user'), | 
					
						
							|  |  |  | 	notifications = require('../notifications'), | 
					
						
							| 
									
										
										
										
											2014-01-18 15:35:51 -05:00
										 |  |  | 	plugins = require('../plugins'), | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	async = require('async'), | 
					
						
							|  |  |  | 	S = require('string'), | 
					
						
							|  |  |  | 	winston = require('winston'), | 
					
						
							| 
									
										
										
										
											2014-03-01 21:31:50 -05:00
										 |  |  | 	_ = require('underscore'), | 
					
						
							| 
									
										
										
										
											2014-01-16 20:14:09 -05:00
										 |  |  | 	server = require('./'), | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-14 00:34:51 -04:00
										 |  |  | 	SocketModules = { | 
					
						
							|  |  |  | 		composer: { | 
					
						
							|  |  |  | 			replyHash: {} | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		chats: {}, | 
					
						
							|  |  |  | 		notifications: {}, | 
					
						
							|  |  |  | 		sounds: {}, | 
					
						
							|  |  |  | 		settings: {} | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Posts Composer */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 21:31:50 -05:00
										 |  |  | var	stopTracking = function(replyObj) { | 
					
						
							|  |  |  | 		if (isLast(replyObj.uid, replyObj.tid)) { | 
					
						
							|  |  |  | 			server.in('topic_' + replyObj.tid).emit('event:topic.replyStop', replyObj.uid); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		clearInterval(replyObj.timer); | 
					
						
							|  |  |  | 		delete SocketModules.composer.replyHash[replyObj.uuid]; | 
					
						
							|  |  |  | 	}, | 
					
						
							|  |  |  | 	isLast = function(uid, tid) { | 
					
						
							|  |  |  | 		return _.filter(SocketModules.composer.replyHash, function(replyObj, uuid) { | 
					
						
							|  |  |  | 			if ( | 
					
						
							|  |  |  | 				parseInt(replyObj.tid, 10) === parseInt(tid, 10) && | 
					
						
							|  |  |  | 				parseInt(replyObj.uid, 10) === parseInt(uid, 10) | 
					
						
							|  |  |  | 			) { | 
					
						
							|  |  |  | 				return true; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				return false; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}).length === 1; | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 22:06:23 -05:00
										 |  |  | SocketModules.composer.push = function(socket, pid, callback) { | 
					
						
							|  |  |  | 	if (socket.uid || parseInt(meta.config.allowGuestPosting, 10)) { | 
					
						
							|  |  |  | 		if (parseInt(pid, 10) > 0) { | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			async.parallel([ | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							| 
									
										
										
										
											2014-01-16 22:06:23 -05:00
										 |  |  | 					posts.getPostFields(pid, ['content'], next); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 				}, | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							| 
									
										
										
										
											2014-02-20 02:05:49 -05:00
										 |  |  | 					topics.getTopicDataByPid(pid, next); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				function(next) { | 
					
						
							|  |  |  | 					posts.getPidIndex(pid, next); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			], function(err, results) { | 
					
						
							| 
									
										
										
										
											2014-01-17 12:55:38 -05:00
										 |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				callback(null, { | 
					
						
							| 
									
										
										
										
											2014-01-16 22:06:23 -05:00
										 |  |  | 					pid: pid, | 
					
						
							| 
									
										
										
										
											2014-02-20 02:05:49 -05:00
										 |  |  | 					body: results[0].content, | 
					
						
							|  |  |  | 					title: results[1].title, | 
					
						
							|  |  |  | 					topic_thumb: results[1].thumb, | 
					
						
							|  |  |  | 					index: results[2] | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-01-16 22:06:23 -05:00
										 |  |  | 		callback(new Error('no-uid')); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:28:21 -05:00
										 |  |  | SocketModules.composer.editCheck = function(socket, pid, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 	posts.getPostField(pid, 'tid', function(err, tid) { | 
					
						
							| 
									
										
										
										
											2014-01-16 20:14:09 -05:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 		postTools.isMain(pid, tid, function(err, isMain) { | 
					
						
							| 
									
										
										
										
											2014-01-16 20:14:09 -05:00
										 |  |  | 			callback(err, { | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 				titleEditable: isMain | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-17 16:16:00 -05:00
										 |  |  | SocketModules.composer.renderPreview = function(socket, content, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-18 15:35:51 -05:00
										 |  |  | 	plugins.fireHook('filter:post.parse', content, callback); | 
					
						
							| 
									
										
										
										
											2014-02-27 14:05:31 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketModules.composer.renderHelp = function(socket, data, callback) { | 
					
						
							|  |  |  | 	plugins.fireHook('filter:composer.help', '', callback); | 
					
						
							|  |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-17 16:16:00 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 15:46:13 -05:00
										 |  |  | SocketModules.composer.register = function(socket, data) { | 
					
						
							| 
									
										
										
										
											2014-03-01 16:53:41 -05:00
										 |  |  | 	var	now = Date.now(); | 
					
						
							| 
									
										
										
										
											2014-03-01 17:49:39 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 15:46:13 -05:00
										 |  |  | 	server.in('topic_' + data.tid).emit('event:topic.replyStart', data.uid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	data.socket = socket; | 
					
						
							| 
									
										
										
										
											2014-03-01 16:53:41 -05:00
										 |  |  | 	data.lastPing = now; | 
					
						
							|  |  |  | 	data.lastAnswer = now; | 
					
						
							| 
									
										
										
										
											2014-03-01 15:46:13 -05:00
										 |  |  | 	data.timer = setInterval(function() { | 
					
						
							| 
									
										
										
										
											2014-03-01 16:53:41 -05:00
										 |  |  | 		if (data.lastPing === data.lastAnswer) { | 
					
						
							|  |  |  | 			// Ping the socket to see if the composer is still active
 | 
					
						
							|  |  |  | 			data.lastPing = Date.now(); | 
					
						
							|  |  |  | 			socket.emit('event:composer.ping', data.uuid); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2014-03-01 21:31:50 -05:00
										 |  |  | 			stopTracking(data); | 
					
						
							| 
									
										
										
										
											2014-03-01 16:53:41 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	}, 1000*5);	// Every 5 seconds...
 | 
					
						
							| 
									
										
										
										
											2014-03-01 15:46:13 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	SocketModules.composer.replyHash[data.uuid] = data; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 16:53:41 -05:00
										 |  |  | SocketModules.composer.unregister = function(socket, uuid) { | 
					
						
							|  |  |  | 	var	replyObj = SocketModules.composer.replyHash[uuid]; | 
					
						
							|  |  |  | 	if (uuid && replyObj) { | 
					
						
							| 
									
										
										
										
											2014-03-01 21:31:50 -05:00
										 |  |  | 		stopTracking(replyObj); | 
					
						
							| 
									
										
										
										
											2014-03-01 16:53:41 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | SocketModules.composer.pingActive = function(socket, uuid) { | 
					
						
							| 
									
										
										
										
											2014-03-01 15:46:13 -05:00
										 |  |  | 	var	data = SocketModules.composer.replyHash[uuid]; | 
					
						
							| 
									
										
										
										
											2014-03-01 16:53:41 -05:00
										 |  |  | 	if (data) { | 
					
						
							|  |  |  | 		data.lastAnswer = data.lastPing; | 
					
						
							| 
									
										
										
										
											2014-03-01 15:46:13 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-01 21:31:50 -05:00
										 |  |  | SocketModules.composer.getUsersByTid = function(socket, tid, callback) { | 
					
						
							|  |  |  | 	// Return uids with active composers
 | 
					
						
							|  |  |  | 	callback(null, _.filter(SocketModules.composer.replyHash, function(replyObj, uuid) { | 
					
						
							| 
									
										
										
										
											2014-03-01 22:51:39 -05:00
										 |  |  | 		return parseInt(replyObj.tid, 10) === parseInt(tid, 10); | 
					
						
							| 
									
										
										
										
											2014-03-01 21:31:50 -05:00
										 |  |  | 	}).map(function(replyObj) { | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 		return replyObj.uid; | 
					
						
							| 
									
										
										
										
											2014-03-01 21:31:50 -05:00
										 |  |  | 	})); | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-03-01 21:31:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							|  |  |  | 		return callback(new Error('invalid data')); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 	Messaging.getMessages(socket.uid, data.touid, false, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | SocketModules.chats.send = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-17 12:55:38 -05:00
										 |  |  | 	if(!data) { | 
					
						
							|  |  |  | 		return callback(new Error('invalid data')); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var touid = data.touid; | 
					
						
							| 
									
										
										
										
											2014-01-16 15:28:21 -05:00
										 |  |  | 	if (touid === socket.uid || socket.uid === 0) { | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var msg = S(data.message).stripTags().s; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 	Messaging.addMessage(socket.uid, touid, msg, function(err, message) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return callback(err); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 		sendChatNotification(socket.uid, touid, message.user.username); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 		server.getUserSockets(touid).forEach(function(s) { | 
					
						
							|  |  |  | 			s.emit('event:chats.receive', { | 
					
						
							|  |  |  | 				withUid: socket.uid, | 
					
						
							|  |  |  | 				message: message | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-02-06 16:32:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | 		server.getUserSockets(socket.uid).forEach(function(s) { | 
					
						
							|  |  |  | 			s.emit('event:chats.receive', { | 
					
						
							|  |  |  | 				withUid: touid, | 
					
						
							|  |  |  | 				message: message | 
					
						
							| 
									
										
										
										
											2014-02-06 16:32:11 -05:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-28 12:13:01 -04:00
										 |  |  | function sendChatNotification(fromuid, touid, username) { | 
					
						
							|  |  |  | 	if (!module.parent.exports.isUserOnline(touid)) { | 
					
						
							|  |  |  | 		var notifText = 'New message from <strong>' + username + '</strong>'; | 
					
						
							|  |  |  | 		notifications.create({ | 
					
						
							|  |  |  | 			text: notifText, | 
					
						
							|  |  |  | 			path: 'javascript:app.openChat('' + username + '', ' + fromuid + ');', | 
					
						
							|  |  |  | 			uniqueId: 'notification_' + fromuid + '_' + touid, | 
					
						
							|  |  |  | 			from: fromuid | 
					
						
							|  |  |  | 		}, function(nid) { | 
					
						
							|  |  |  | 			notifications.push(nid, [touid], function(success) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:28:21 -05:00
										 |  |  | SocketModules.chats.list = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-16 20:14:09 -05:00
										 |  |  | 	Messaging.getRecentChats(socket.uid, callback); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Notifications */ | 
					
						
							| 
									
										
										
										
											2014-01-16 15:28:21 -05:00
										 |  |  | SocketModules.notifications.mark_read = function(socket, nid) { | 
					
						
							|  |  |  | 	notifications.mark_read(nid, socket.uid); | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:28:21 -05:00
										 |  |  | SocketModules.notifications.mark_all_read = function(socket, data, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-16 20:14:09 -05:00
										 |  |  | 	notifications.mark_all_read(socket.uid, 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
 | 
					
						
							|  |  |  | 	meta.sounds.getLocal(callback); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | module.exports = SocketModules; |