| 
									
										
										
										
											2014-01-13 11:23:21 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | var	SocketIO = require('socket.io'), | 
					
						
							| 
									
										
										
										
											2014-11-20 19:02:29 -05:00
										 |  |  | 	socketioWildcard = require('socketio-wildcard')(), | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 	util = require('util'), | 
					
						
							|  |  |  | 	async = require('async'), | 
					
						
							| 
									
										
										
										
											2014-01-09 21:27:50 -05:00
										 |  |  | 	path = require('path'), | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 	fs = require('fs'), | 
					
						
							|  |  |  | 	nconf = require('nconf'), | 
					
						
							| 
									
										
										
										
											2014-11-20 19:02:29 -05:00
										 |  |  | 	cookieParser = require('cookie-parser')(nconf.get('secret')), | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 	winston = require('winston'), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	db = require('../database'), | 
					
						
							|  |  |  | 	user = require('../user'), | 
					
						
							|  |  |  | 	topics = require('../topics'), | 
					
						
							|  |  |  | 	logger = require('../logger'), | 
					
						
							| 
									
										
										
										
											2014-11-19 15:55:01 -05:00
										 |  |  | 	ratelimit = require('../middleware/ratelimit'), | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Sockets = {}, | 
					
						
							|  |  |  | 	Namespaces = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-20 16:51:11 -05:00
										 |  |  | var io; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 16:39:27 -05:00
										 |  |  | Sockets.init = function(server) { | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 	requireModules(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 14:59:12 -05:00
										 |  |  | 	io = new SocketIO({ | 
					
						
							|  |  |  | 		path: nconf.get('relative_path') + '/socket.io' | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-11-20 19:02:29 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	addRedisAdapter(io); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-20 16:51:11 -05:00
										 |  |  | 	io.use(socketioWildcard); | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 	io.use(authorize); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	io.on('connection', onConnection); | 
					
						
							| 
									
										
										
										
											2014-11-20 16:51:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-12-01 14:59:12 -05:00
										 |  |  | 	io.listen(server, { | 
					
						
							|  |  |  | 		transports: ['websocket', 'polling'] | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 	Sockets.server = io; | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | function onConnection(socket) { | 
					
						
							|  |  |  | 	socket.ip = socket.request.connection.remoteAddress; | 
					
						
							| 
									
										
										
										
											2014-01-09 21:27:50 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 	logger.io_one(socket, socket.uid); | 
					
						
							| 
									
										
										
										
											2014-04-15 02:33:48 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 	onConnect(socket); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	socket.on('disconnect', function() { | 
					
						
							|  |  |  | 		onDisconnect(socket); | 
					
						
							| 
									
										
										
										
											2014-01-09 21:27:50 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 	socket.on('*', function(payload) { | 
					
						
							|  |  |  | 		onMessage(socket, payload); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function onConnect(socket) { | 
					
						
							|  |  |  | 	if (socket.uid) { | 
					
						
							|  |  |  | 		socket.join('uid_' + socket.uid); | 
					
						
							|  |  |  | 		socket.join('online_users'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		async.parallel({ | 
					
						
							|  |  |  | 			user: function(next) { | 
					
						
							| 
									
										
										
										
											2014-12-03 19:36:19 -05:00
										 |  |  | 				user.getUserFields(socket.uid, ['username', 'userslug', 'picture', 'status', 'email:confirmed'], next); | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			isAdmin: function(next) { | 
					
						
							|  |  |  | 				user.isAdministrator(socket.uid, next); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, function(err, userData) { | 
					
						
							|  |  |  | 			if (err || !userData.user) { | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-12-03 19:36:19 -05:00
										 |  |  | 			userData.user.uid = socket.uid; | 
					
						
							|  |  |  | 			userData.user.isAdmin = userData.isAdmin; | 
					
						
							|  |  |  | 			userData.user['email:confirmed'] = parseInt(userData.user['email:confirmed'], 10) === 1; | 
					
						
							|  |  |  | 			socket.emit('event:connect', userData.user); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 			socket.broadcast.emit('event:user_status_change', {uid: socket.uid, status: userData.user.status}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		socket.join('online_guests'); | 
					
						
							|  |  |  | 		socket.emit('event:connect', { | 
					
						
							|  |  |  | 			username: '[[global:guest]]', | 
					
						
							|  |  |  | 			isAdmin: false, | 
					
						
							|  |  |  | 			uid: 0 | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function onDisconnect(socket) { | 
					
						
							| 
									
										
										
										
											2014-11-28 21:04:12 -05:00
										 |  |  | 	if (socket.uid) { | 
					
						
							|  |  |  | 		var socketCount = Sockets.getUserSocketCount(socket.uid); | 
					
						
							|  |  |  | 		if (socketCount <= 0) { | 
					
						
							|  |  |  | 			socket.broadcast.emit('event:user_status_change', {uid: socket.uid, status: 'offline'}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 21:04:12 -05:00
										 |  |  | 		// TODO: if we can get socket.rooms here this can be made more efficient,
 | 
					
						
							|  |  |  | 		// see https://github.com/Automattic/socket.io/issues/1897
 | 
					
						
							|  |  |  | 		io.sockets.in('online_users').emit('event:user_leave', socket.uid); | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function onMessage(socket, payload) { | 
					
						
							|  |  |  | 	if (!payload.data.length) { | 
					
						
							|  |  |  | 		return winston.warn('[socket.io] Empty payload'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var eventName = payload.data[0]; | 
					
						
							|  |  |  | 	var params = payload.data[1]; | 
					
						
							|  |  |  | 	var callback = typeof payload.data[payload.data.length - 1] === 'function' ? payload.data[payload.data.length - 1] : function() {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!eventName) { | 
					
						
							|  |  |  | 		return winston.warn('[socket.io] Empty method name'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (ratelimit.isFlooding(socket)) { | 
					
						
							|  |  |  | 		winston.warn('[socket.io] Too many emits! Disconnecting uid : ' + socket.uid + '. Message : ' + payload.name); | 
					
						
							|  |  |  | 		return socket.disconnect(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	var parts = eventName.toString().split('.'), | 
					
						
							|  |  |  | 		namespace = parts[0], | 
					
						
							|  |  |  | 		methodToCall = parts.reduce(function(prev, cur) { | 
					
						
							|  |  |  | 			if (prev !== null && prev[cur]) { | 
					
						
							|  |  |  | 				return prev[cur]; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				return null; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}, Namespaces); | 
					
						
							| 
									
										
										
										
											2014-11-20 19:02:29 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 	if(!methodToCall) { | 
					
						
							|  |  |  | 		if (process.env.NODE_ENV === 'development') { | 
					
						
							|  |  |  | 			winston.warn('[socket.io] Unrecognized message: ' + eventName); | 
					
						
							| 
									
										
										
										
											2014-10-27 20:23:16 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-10-27 20:23:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 	if (Namespaces[namespace].before) { | 
					
						
							|  |  |  | 		Namespaces[namespace].before(socket, eventName, function() { | 
					
						
							|  |  |  | 			callMethod(methodToCall, socket, params, callback); | 
					
						
							| 
									
										
										
										
											2014-11-20 19:02:29 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		callMethod(methodToCall, socket, params, callback); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | function requireModules() { | 
					
						
							|  |  |  | 	fs.readdir(__dirname, function(err, files) { | 
					
						
							|  |  |  | 		files.splice(files.indexOf('index.js'), 1); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 		async.each(files, function(lib, next) { | 
					
						
							|  |  |  | 			if (lib.substr(lib.length - 3) === '.js') { | 
					
						
							|  |  |  | 				lib = lib.slice(0, -3); | 
					
						
							|  |  |  | 				Namespaces[lib] = require('./' + lib); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 			next(); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | function authorize(socket, next) { | 
					
						
							|  |  |  | 	var handshake = socket.request, | 
					
						
							| 
									
										
										
										
											2014-11-28 21:43:44 -05:00
										 |  |  | 		sessionID; | 
					
						
							| 
									
										
										
										
											2014-11-20 19:02:29 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 	if (!handshake) { | 
					
						
							| 
									
										
										
										
											2014-11-28 21:43:44 -05:00
										 |  |  | 		return next(new Error('[[error:not-authorized]]')); | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-11-20 19:02:29 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 	cookieParser(handshake, {}, function(err) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return next(err); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-01-16 17:07:34 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 		var sessionID = handshake.signedCookies['express.sid']; | 
					
						
							| 
									
										
										
										
											2014-01-16 17:07:34 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 		db.sessionStore.get(sessionID, function(err, sessionData) { | 
					
						
							|  |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return next(err); | 
					
						
							| 
									
										
										
										
											2014-01-16 18:50:45 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-11-28 21:43:44 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 			if (sessionData && sessionData.passport && sessionData.passport.user) { | 
					
						
							|  |  |  | 				socket.uid = parseInt(sessionData.passport.user, 10); | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 				socket.uid = 0; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | 			next(); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-11-28 19:33:07 -05:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-20 19:02:29 -05:00
										 |  |  | function addRedisAdapter(io) { | 
					
						
							|  |  |  | 	if (nconf.get('redis')) { | 
					
						
							|  |  |  | 		var redisAdapter = require('socket.io-redis'); | 
					
						
							|  |  |  | 		var redis = require('../database/redis'); | 
					
						
							| 
									
										
										
										
											2014-11-24 13:44:59 -05:00
										 |  |  | 		var pub = redis.connect({return_buffers: true}); | 
					
						
							|  |  |  | 		var sub = redis.connect({return_buffers: true}); | 
					
						
							| 
									
										
										
										
											2014-11-20 19:02:29 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		io.adapter(redisAdapter({pubClient: pub, subClient: sub})); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		winston.warn('[socket.io] Clustering detected, you are advised to configure Redis as a websocket store.'); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | function callMethod(method, socket, params, callback) { | 
					
						
							|  |  |  | 	method.call(null, socket, params, function(err, result) { | 
					
						
							|  |  |  | 		callback(err ? {message: err.message} : null, result); | 
					
						
							| 
									
										
										
										
											2014-11-19 15:55:01 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | Sockets.logoutUser = function(uid) { | 
					
						
							| 
									
										
										
										
											2014-11-28 21:35:34 -05:00
										 |  |  | 	io.sockets.in('uid_' + uid).emit('event:disconnect'); | 
					
						
							| 
									
										
										
										
											2014-01-13 11:23:21 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | Sockets.in = function(room) { | 
					
						
							|  |  |  | 	return io.sockets.in(room); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-05 00:28:27 -04:00
										 |  |  | Sockets.getSocketCount = function() { | 
					
						
							| 
									
										
										
										
											2014-11-28 19:06:07 -05:00
										 |  |  | 	// TODO: io.sockets.adapter.sids is local to this worker
 | 
					
						
							|  |  |  | 	// use redis-adapter
 | 
					
						
							| 
									
										
										
										
											2014-11-20 16:51:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:06:07 -05:00
										 |  |  | 	var clients = Object.keys(io.sockets.adapter.sids || {}); | 
					
						
							| 
									
										
										
										
											2014-09-25 16:45:56 -04:00
										 |  |  | 	return Array.isArray(clients) ? clients.length : 0; | 
					
						
							| 
									
										
										
										
											2014-09-06 22:08:55 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-09 18:05:21 -04:00
										 |  |  | Sockets.getUserSocketCount = function(uid) { | 
					
						
							| 
									
										
										
										
											2014-11-28 21:04:12 -05:00
										 |  |  | 	// TODO: io.sockets.adapter.rooms is local to this worker
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:06:07 -05:00
										 |  |  | 	// use .clients('uid_' + uid, fn)
 | 
					
						
							| 
									
										
										
										
											2014-11-20 16:51:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:06:07 -05:00
										 |  |  | 	var roomClients = Object.keys(io.sockets.adapter.rooms['uid_' + uid] || {}); | 
					
						
							|  |  |  | 	return Array.isArray(roomClients) ? roomClients.length : 0; | 
					
						
							| 
									
										
										
										
											2014-09-09 18:05:21 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | Sockets.getOnlineAnonCount = function () { | 
					
						
							| 
									
										
										
										
											2014-11-28 19:06:07 -05:00
										 |  |  | 	// TODO: io.sockets.adapter.rooms is local to this worker
 | 
					
						
							|  |  |  | 	// use .clients()
 | 
					
						
							| 
									
										
										
										
											2014-11-20 16:51:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-28 19:06:07 -05:00
										 |  |  | 	var guestSocketIds = Object.keys(io.sockets.adapter.rooms.online_guests || {}); | 
					
						
							|  |  |  | 	return Array.isArray(guestSocketIds) ? guestSocketIds.length : 0; | 
					
						
							| 
									
										
										
										
											2014-02-06 16:32:11 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-27 00:47:08 -04:00
										 |  |  | Sockets.reqFromSocket = function(socket) { | 
					
						
							| 
									
										
										
										
											2014-11-28 17:46:00 -05:00
										 |  |  | 	var headers = socket.request.headers, | 
					
						
							| 
									
										
										
										
											2014-11-28 21:46:50 -05:00
										 |  |  | 		host = headers.host, | 
					
						
							|  |  |  | 		referer = headers.referer || ''; | 
					
						
							| 
									
										
										
										
											2014-11-28 17:46:00 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	return { | 
					
						
							| 
									
										
										
										
											2014-11-28 21:46:50 -05:00
										 |  |  | 		ip: socket.ip, | 
					
						
							|  |  |  | 		host: host, | 
					
						
							|  |  |  | 		protocol: socket.request.connection.encrypted ? 'https' : 'http', | 
					
						
							|  |  |  | 		secure: !!socket.request.connection.encrypted, | 
					
						
							|  |  |  | 		url: referer, | 
					
						
							|  |  |  | 		path: referer.substr(referer.indexOf(host) + host.length), | 
					
						
							|  |  |  | 		headers: headers | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-04-27 00:47:08 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-19 23:37:55 -05:00
										 |  |  | Sockets.isUserOnline = function(uid) { | 
					
						
							| 
									
										
										
										
											2014-11-28 19:06:07 -05:00
										 |  |  | 	// TODO: io.sockets.adapter.rooms is local to this worker
 | 
					
						
							|  |  |  | 	// use .clients('uid_' + uid, fn)
 | 
					
						
							|  |  |  | 	return io ? !!io.sockets.adapter.rooms['uid_' + uid] : false; | 
					
						
							| 
									
										
										
										
											2014-11-19 23:37:55 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-02 05:04:39 -04:00
										 |  |  | Sockets.isUsersOnline = function(uids, callback) { | 
					
						
							| 
									
										
										
										
											2014-11-28 19:06:07 -05:00
										 |  |  | 	callback(null, uids.map(Sockets.isUserOnline)); | 
					
						
							| 
									
										
										
										
											2014-09-02 05:04:39 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-11-19 23:37:55 -05:00
										 |  |  | Sockets.updateRoomBrowsingText = function (roomName, selfUid) { | 
					
						
							| 
									
										
										
										
											2014-03-01 22:51:39 -05:00
										 |  |  | 	if (!roomName) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 00:19:46 -04:00
										 |  |  | 	var	uids = Sockets.getUidsInRoom(roomName); | 
					
						
							| 
									
										
										
										
											2014-10-16 18:04:04 -04:00
										 |  |  | 	var total = uids.length; | 
					
						
							| 
									
										
										
										
											2014-09-06 00:19:46 -04:00
										 |  |  | 	uids = uids.slice(0, 9); | 
					
						
							|  |  |  | 	if (selfUid) { | 
					
						
							|  |  |  | 		uids = [selfUid].concat(uids); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-09-06 01:55:56 -04:00
										 |  |  | 	if (!uids.length) { | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-31 20:19:59 -05:00
										 |  |  | 	user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture', 'status'], function(err, users) { | 
					
						
							| 
									
										
										
										
											2014-10-16 18:04:04 -04:00
										 |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return; | 
					
						
							| 
									
										
										
										
											2014-01-19 14:27:07 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-10-16 18:04:04 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		users = users.filter(function(user) { | 
					
						
							|  |  |  | 			return user && user.status !== 'offline'; | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		io.sockets.in(roomName).emit('event:update_users_in_room', { | 
					
						
							|  |  |  | 			users: users, | 
					
						
							|  |  |  | 			room: roomName, | 
					
						
							|  |  |  | 			total: Math.max(0, total - uids.length) | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2014-01-19 14:27:07 -05:00
										 |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-11-19 23:37:55 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-13 18:44:40 -04:00
										 |  |  | Sockets.getUidsInRoom = function(roomName) { | 
					
						
							| 
									
										
										
										
											2014-11-28 19:06:07 -05:00
										 |  |  | 	// TODO : doesnt work in cluster
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-13 18:44:40 -04:00
										 |  |  | 	var uids = []; | 
					
						
							| 
									
										
										
										
											2014-11-28 19:06:07 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var socketids = Object.keys(io.sockets.adapter.rooms[roomName] || {}); | 
					
						
							|  |  |  | 	if (!Array.isArray(socketids) || !socketids.length) { | 
					
						
							| 
									
										
										
										
											2014-09-04 20:20:58 -04:00
										 |  |  | 		return []; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(var i=0; i<socketids.length; ++i) { | 
					
						
							| 
									
										
										
										
											2014-11-28 19:06:07 -05:00
										 |  |  | 		var socketRooms = Object.keys(io.sockets.adapter.sids[socketids[i]]); | 
					
						
							| 
									
										
										
										
											2014-09-09 18:32:09 -04:00
										 |  |  | 		if (Array.isArray(socketRooms)) { | 
					
						
							|  |  |  | 			socketRooms.forEach(function(roomName) { | 
					
						
							| 
									
										
										
										
											2014-11-28 19:06:07 -05:00
										 |  |  | 				if (roomName.indexOf('uid_') === 0 ) { | 
					
						
							| 
									
										
										
										
											2014-09-09 18:32:09 -04:00
										 |  |  | 					uids.push(roomName.split('_')[1]); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-08-13 18:44:40 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-09-04 20:20:58 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-13 18:44:40 -04:00
										 |  |  | 	return uids; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Exporting */ | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | module.exports = Sockets; |