| 
									
										
										
										
											2014-01-13 11:23:21 -05:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | var	SocketIO = require('socket.io'), | 
					
						
							|  |  |  | 	socketioWildcard = require('socket.io-wildcard'), | 
					
						
							|  |  |  | 	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'), | 
					
						
							|  |  |  | 	express = require('express'), | 
					
						
							|  |  |  | 	socketCookieParser = express.cookieParser(nconf.get('secret')), | 
					
						
							|  |  |  | 	winston = require('winston'), | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	db = require('../database'), | 
					
						
							|  |  |  | 	user = require('../user'), | 
					
						
							|  |  |  | 	topics = require('../topics'), | 
					
						
							|  |  |  | 	logger = require('../logger'), | 
					
						
							| 
									
										
										
										
											2014-01-13 11:23:21 -05:00
										 |  |  | 	meta = require('../meta'), | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	Sockets = {}, | 
					
						
							|  |  |  | 	Namespaces = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* === */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-26 21:32:53 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | var	io; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | Sockets.userSockets = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 16:39:27 -05:00
										 |  |  | Sockets.init = function(server) { | 
					
						
							| 
									
										
										
										
											2014-01-14 18:04:54 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-25 16:39:27 -05:00
										 |  |  | 	io = socketioWildcard(SocketIO).listen(server, { | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 		log: false, | 
					
						
							|  |  |  | 		transports: ['websocket', 'xhr-polling', 'jsonp-polling', 'flashsocket'], | 
					
						
							|  |  |  | 		'browser client minification': true | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 	Sockets.server = io; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-09 21:27:50 -05:00
										 |  |  | 	fs.readdir(__dirname, function(err, files) { | 
					
						
							|  |  |  | 		files.splice(files.indexOf('index.js'), 1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		async.each(files, function(lib, next) { | 
					
						
							|  |  |  | 			lib = lib.slice(0, -3); | 
					
						
							|  |  |  | 			Namespaces[lib] = require('./' + lib); | 
					
						
							|  |  |  | 			next(); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 	io.sockets.on('connection', function(socket) { | 
					
						
							|  |  |  | 		var hs = socket.handshake, | 
					
						
							|  |  |  | 			sessionID, uid, lastPostTime = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Validate the session, if present
 | 
					
						
							|  |  |  | 		socketCookieParser(hs, {}, function(err) { | 
					
						
							|  |  |  | 			sessionID = socket.handshake.signedCookies["express.sid"]; | 
					
						
							|  |  |  | 			db.sessionStore.get(sessionID, function(err, sessionData) { | 
					
						
							|  |  |  | 				if (!err && sessionData && sessionData.passport && sessionData.passport.user) { | 
					
						
							| 
									
										
										
										
											2014-01-26 21:32:53 -05:00
										 |  |  | 					uid = sessionData.passport.user; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2014-01-26 21:32:53 -05:00
										 |  |  | 					uid = 0; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 15:46:37 -05:00
										 |  |  | 				socket.uid = parseInt(uid, 10); | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				Sockets.userSockets[uid] = Sockets.userSockets[uid] || []; | 
					
						
							|  |  |  | 				Sockets.userSockets[uid].push(socket); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				/* Need to save some state for the logger & maybe some other modules later on */ | 
					
						
							|  |  |  | 				socket.state = { | 
					
						
							|  |  |  | 					user : { | 
					
						
							|  |  |  | 						uid : uid | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				/* If meta.config.loggerIOStatus > 0, logger.io_one will hook into this socket */ | 
					
						
							|  |  |  | 				logger.io_one(socket,uid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (uid) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					db.sortedSetAdd('users:online', Date.now(), uid, function(err, data) { | 
					
						
							|  |  |  | 						socket.join('uid_' + uid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						user.getUserField(uid, 'username', function(err, username) { | 
					
						
							|  |  |  | 							socket.emit('event:connect', { | 
					
						
							|  |  |  | 								status: 1, | 
					
						
							|  |  |  | 								username: username, | 
					
						
							|  |  |  | 								uid: uid | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 14:27:07 -05:00
										 |  |  | 				socket.broadcast.emit('user.isOnline', null, { | 
					
						
							|  |  |  | 					online: isUserOnline(uid), | 
					
						
							|  |  |  | 					uid: uid, | 
					
						
							|  |  |  | 					timestamp: Date.now() | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		socket.on('disconnect', function() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 			var index = (Sockets.userSockets[uid] || []).indexOf(socket); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 			if (index !== -1) { | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 				Sockets.userSockets[uid].splice(index, 1); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 			if (Sockets.userSockets[uid] && Sockets.userSockets[uid].length === 0) { | 
					
						
							|  |  |  | 				delete Sockets.userSockets[uid]; | 
					
						
							| 
									
										
										
										
											2014-01-26 21:32:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 				if (uid) { | 
					
						
							|  |  |  | 					db.sortedSetRemove('users:online', uid, function(err, data) { | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 14:27:07 -05:00
										 |  |  | 			socket.broadcast.emit('user.isOnline', null, { | 
					
						
							|  |  |  | 				online: isUserOnline(uid), | 
					
						
							|  |  |  | 				uid: uid, | 
					
						
							|  |  |  | 				timestamp: Date.now() | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			emitOnlineUserCount(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-26 21:32:53 -05:00
										 |  |  | 			for(var roomName in io.sockets.manager.roomClients[socket.id]) { | 
					
						
							|  |  |  | 				updateRoomBrowsingText(roomName.slice(1)); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-01-26 21:32:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-09 21:27:50 -05:00
										 |  |  | 		socket.on('*', function(payload, callback) { | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 			function callMethod(method) { | 
					
						
							| 
									
										
										
										
											2014-01-16 15:57:13 -05:00
										 |  |  | 				method.call(null, socket, payload.args.length ? payload.args[0] : null, function(err, result) { | 
					
						
							| 
									
										
										
										
											2014-01-16 17:25:14 -05:00
										 |  |  | 					if (callback) { | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 						callback(err?{message:err.message}:null, result); | 
					
						
							| 
									
										
										
										
											2014-01-13 11:40:08 -05:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-01-16 17:07:34 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 18:50:45 -05:00
										 |  |  | 			if(!payload.name) { | 
					
						
							|  |  |  | 				return winston.warn('[socket.io] Empty method name'); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-01-16 17:07:34 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 18:50:45 -05:00
										 |  |  | 			var parts = payload.name.toString().split('.'), | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 				namespace = parts.slice(0, 1), | 
					
						
							|  |  |  | 				methodToCall = parts.reduce(function(prev, cur) { | 
					
						
							|  |  |  | 					if (prev !== null && prev[cur]) { | 
					
						
							|  |  |  | 						return prev[cur]; | 
					
						
							| 
									
										
										
										
											2014-01-13 12:01:42 -05:00
										 |  |  | 					} else { | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 						return null; | 
					
						
							| 
									
										
										
										
											2014-01-13 12:01:42 -05:00
										 |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 				}, Namespaces); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 18:50:45 -05:00
										 |  |  | 			if(!methodToCall) { | 
					
						
							|  |  |  | 				return winston.warn('[socket.io] Unrecognized message: ' + payload.name); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 18:50:45 -05:00
										 |  |  | 			if (Namespaces[namespace].before) { | 
					
						
							|  |  |  | 				Namespaces[namespace].before(socket, function() { | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 					callMethod(methodToCall); | 
					
						
							| 
									
										
										
										
											2014-01-16 18:50:45 -05:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2014-01-16 18:50:45 -05:00
										 |  |  | 				callMethod(methodToCall); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-01-13 11:23:21 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | Sockets.logoutUser = function(uid) { | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 	if(Sockets.userSockets[uid] && Sockets.userSockets[uid].length) { | 
					
						
							|  |  |  | 		for(var i=0; i< Sockets.userSockets[uid].length; ++i) { | 
					
						
							|  |  |  | 			Sockets.userSockets[uid][i].emit('event:disconnect'); | 
					
						
							|  |  |  | 			Sockets.userSockets[uid][i].disconnect(); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 			if(!Sockets.userSockets[uid]) { | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-13 11:23:21 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | Sockets.emitUserCount = function() { | 
					
						
							|  |  |  | 	db.getObjectField('global', 'userCount', function(err, count) { | 
					
						
							| 
									
										
										
										
											2014-01-16 17:07:34 -05:00
										 |  |  | 		io.sockets.emit('user.count', err?{message:err.message}:null, { | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 			count: count | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Sockets.in = function(room) { | 
					
						
							|  |  |  | 	return io.sockets.in(room); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Sockets.getConnectedClients = function() { | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 	return Sockets.userSockets; | 
					
						
							| 
									
										
										
										
											2014-01-13 11:23:21 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | Sockets.getOnlineAnonCount = function () { | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 	return Sockets.userSockets[0] ? Sockets.userSockets[0].length : 0; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Helpers */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-13 11:23:21 -05:00
										 |  |  | Sockets.isUserOnline = isUserOnline; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | function isUserOnline(uid) { | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 	return !!Sockets.userSockets[uid] && Sockets.userSockets[uid].length > 0; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | Sockets.updateRoomBrowsingText = updateRoomBrowsingText; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | function updateRoomBrowsingText(roomName) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function getUidsInRoom(room) { | 
					
						
							|  |  |  | 		var uids = []; | 
					
						
							| 
									
										
										
										
											2014-01-26 21:32:53 -05:00
										 |  |  | 		var clients = io.sockets.clients(roomName); | 
					
						
							|  |  |  | 		for(var i=0; i<clients.length; ++i) { | 
					
						
							|  |  |  | 			if (uids.indexOf(clients[i].uid) === -1 && clients[i].uid !== 0) { | 
					
						
							|  |  |  | 				uids.push(clients[i].uid); | 
					
						
							| 
									
										
										
										
											2014-01-13 11:23:21 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		return uids; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	function getAnonymousCount(roomName) { | 
					
						
							|  |  |  | 		var clients = io.sockets.clients(roomName); | 
					
						
							|  |  |  | 		var anonCount = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		for (var i = 0; i < clients.length; ++i) { | 
					
						
							| 
									
										
										
										
											2014-01-26 21:32:53 -05:00
										 |  |  | 			if(clients[i].uid === 0) { | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 				++anonCount; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return anonCount; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-26 21:32:53 -05:00
										 |  |  | 	var	uids = getUidsInRoom(roomName), | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 		anonymousCount = getAnonymousCount(roomName); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 14:27:07 -05:00
										 |  |  | 	user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture'], function(err, users) { | 
					
						
							|  |  |  | 		if(!err) { | 
					
						
							|  |  |  | 			io.sockets.in(roomName).emit('get_users_in_room', { | 
					
						
							|  |  |  | 				users: users, | 
					
						
							|  |  |  | 				anonymousCount: anonymousCount, | 
					
						
							|  |  |  | 				room: roomName | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | Sockets.emitTopicPostStats = emitTopicPostStats; | 
					
						
							|  |  |  | function emitTopicPostStats(callback) { | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 	db.getObjectFields('global', ['topicCount', 'postCount'], function(err, data) { | 
					
						
							|  |  |  | 		if (err) { | 
					
						
							|  |  |  | 			return winston.err(err); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var stats = { | 
					
						
							|  |  |  | 			topics: data.topicCount ? data.topicCount : 0, | 
					
						
							|  |  |  | 			posts: data.postCount ? data.postCount : 0 | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 		if (!callback) { | 
					
						
							| 
									
										
										
										
											2014-01-16 17:07:34 -05:00
										 |  |  | 			io.sockets.emit('post.stats', null, stats); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2014-01-16 17:07:34 -05:00
										 |  |  | 			callback(null, stats); | 
					
						
							| 
									
										
										
										
											2014-01-10 10:46:26 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-09 21:27:50 -05:00
										 |  |  | Sockets.emitOnlineUserCount = emitOnlineUserCount; | 
					
						
							|  |  |  | function emitOnlineUserCount(callback) { | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 	var anon = Sockets.userSockets[0] ? Sockets.userSockets[0].length : 0; | 
					
						
							|  |  |  | 	var registered = Object.keys(Sockets.userSockets).length; | 
					
						
							| 
									
										
										
										
											2014-01-13 11:23:21 -05:00
										 |  |  | 	if (anon) { | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 		registered = registered - 1; | 
					
						
							| 
									
										
										
										
											2014-01-13 11:23:21 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	var returnObj = { | 
					
						
							|  |  |  | 		users: registered + anon, | 
					
						
							|  |  |  | 		anon: anon | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-01-09 21:27:50 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (callback) { | 
					
						
							| 
									
										
										
										
											2014-01-16 17:07:34 -05:00
										 |  |  | 		callback(null, returnObj); | 
					
						
							| 
									
										
										
										
											2014-01-09 21:27:50 -05:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											2014-01-16 17:07:34 -05:00
										 |  |  | 		io.sockets.emit('user.getActiveUsers', null, returnObj); | 
					
						
							| 
									
										
										
										
											2014-01-09 21:27:50 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | Sockets.emitAlert = emitAlert; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | function emitAlert(socket, title, message) { | 
					
						
							|  |  |  | 	socket.emit('event:alert', { | 
					
						
							|  |  |  | 		type: 'danger', | 
					
						
							|  |  |  | 		timeout: 2000, | 
					
						
							|  |  |  | 		title: title, | 
					
						
							|  |  |  | 		message: message, | 
					
						
							|  |  |  | 		alert_id: 'post_error' | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | Sockets.emitContentTooShortAlert = emitContentTooShortAlert; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | function emitContentTooShortAlert(socket) { | 
					
						
							|  |  |  | 	socket.emit('event:alert', { | 
					
						
							|  |  |  | 		type: 'danger', | 
					
						
							|  |  |  | 		timeout: 2000, | 
					
						
							|  |  |  | 		title: 'Content too short', | 
					
						
							|  |  |  | 		message: "Please enter a longer post. At least " + meta.config.minimumPostLength + " characters.", | 
					
						
							|  |  |  | 		alert_id: 'post_error' | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-10 16:00:03 -05:00
										 |  |  | Sockets.emitTooManyPostsAlert = emitTooManyPostsAlert; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | function emitTooManyPostsAlert(socket) { | 
					
						
							|  |  |  | 	socket.emit('event:alert', { | 
					
						
							|  |  |  | 		title: 'Too many posts!', | 
					
						
							|  |  |  | 		message: 'You can only post every ' + meta.config.postDelay + ' seconds.', | 
					
						
							|  |  |  | 		type: 'danger', | 
					
						
							|  |  |  | 		timeout: 2000 | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Exporting */ | 
					
						
							|  |  |  | module.exports = Sockets; |