| 
									
										
										
										
											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'), | 
					
						
							| 
									
										
										
										
											2014-01-31 15:13:52 -05:00
										 |  |  | 	socketUser = require('./user'), | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 	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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) { | 
					
						
							| 
									
										
										
										
											2014-01-28 16:14:56 -05:00
										 |  |  | 			if(err) { | 
					
						
							|  |  |  | 				winston.error(err.message); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 			sessionID = socket.handshake.signedCookies["express.sid"]; | 
					
						
							|  |  |  | 			db.sessionStore.get(sessionID, function(err, sessionData) { | 
					
						
							|  |  |  | 				if (!err && sessionData && sessionData.passport && sessionData.passport.user) { | 
					
						
							| 
									
										
										
										
											2014-02-06 16:32:11 -05:00
										 |  |  | 					uid = parseInt(sessionData.passport.user, 10); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-02 14:47:49 -05:00
										 |  |  | 						async.parallel({ | 
					
						
							|  |  |  | 							username: function(next) { | 
					
						
							| 
									
										
										
										
											2014-02-11 20:39:07 -05:00
										 |  |  | 								user.getUserField(uid, 'username', next); | 
					
						
							| 
									
										
										
										
											2014-02-02 14:47:49 -05:00
										 |  |  | 							}, | 
					
						
							|  |  |  | 							isAdmin: function(next) { | 
					
						
							| 
									
										
										
										
											2014-02-11 20:39:07 -05:00
										 |  |  | 								user.isAdministrator(uid, next); | 
					
						
							| 
									
										
										
										
											2014-02-02 14:47:49 -05:00
										 |  |  | 							} | 
					
						
							|  |  |  | 						}, function(err, userData) { | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 							socket.emit('event:connect', { | 
					
						
							|  |  |  | 								status: 1, | 
					
						
							| 
									
										
										
										
											2014-02-02 14:47:49 -05:00
										 |  |  | 								username: userData.username, | 
					
						
							|  |  |  | 								isAdmin: userData.isAdmin, | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 								uid: uid | 
					
						
							|  |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2014-02-06 16:32:11 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 							socketUser.isOnline(socket, uid, function(err, data) { | 
					
						
							|  |  |  | 								socket.broadcast.emit('user.isOnline', err, data); | 
					
						
							|  |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2014-02-06 16:32:11 -05:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					socket.broadcast.emit('user.anonConnect'); | 
					
						
							| 
									
										
										
										
											2014-02-11 20:39:07 -05:00
										 |  |  | 					socket.emit('event:connect', { | 
					
						
							|  |  |  | 						status: 1, | 
					
						
							|  |  |  | 						username: 'Anonymous', | 
					
						
							|  |  |  | 						isAdmin: false, | 
					
						
							|  |  |  | 						uid: 0 | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		socket.on('disconnect', function() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 16:32:11 -05:00
										 |  |  | 			if (uid && !Sockets.getUserSockets(uid).length <= 1) { | 
					
						
							|  |  |  | 				db.sortedSetRemove('users:online', uid, function(err) { | 
					
						
							|  |  |  | 					socketUser.isOnline(socket, uid, function(err, data) { | 
					
						
							|  |  |  | 						socket.broadcast.emit('user.isOnline', err, data); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2014-02-06 16:32:11 -05:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-02-06 16:32:11 -05:00
										 |  |  | 			if (!uid) { | 
					
						
							|  |  |  | 				socket.broadcast.emit('user.anonDisconnect'); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											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-09 21:27:50 -05:00
										 |  |  | 		socket.on('*', function(payload, callback) { | 
					
						
							| 
									
										
										
										
											2014-02-06 16:32:11 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-16 14:57:03 -05:00
										 |  |  | 			function callMethod(method) { | 
					
						
							| 
									
										
										
										
											2014-01-28 16:14:56 -05:00
										 |  |  | 				if(socket.uid) { | 
					
						
							|  |  |  | 					user.setUserField(socket.uid, 'lastonline', Date.now()); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-02-06 16:32:11 -05:00
										 |  |  | 	Sockets.getUserSockets(uid).forEach(function(socket) { | 
					
						
							|  |  |  | 		socket.emit('event:disconnect'); | 
					
						
							|  |  |  | 		socket.disconnect(); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											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-02-06 16:32:11 -05:00
										 |  |  | 	var clients = io.sockets.clients(); | 
					
						
							|  |  |  | 	var uids = []; | 
					
						
							|  |  |  | 	clients.forEach(function(client) { | 
					
						
							|  |  |  | 		if(client.uid && uids.indexOf(client.uid) === -1) { | 
					
						
							|  |  |  | 			uids.push(client.uid); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 	return uids; | 
					
						
							| 
									
										
										
										
											2014-01-13 11:23:21 -05:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2014-01-09 20:13:17 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | Sockets.getOnlineAnonCount = function () { | 
					
						
							| 
									
										
										
										
											2014-02-06 16:32:11 -05:00
										 |  |  | 	return Sockets.getUserSockets(0).length; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Sockets.getUserSockets = function(uid) { | 
					
						
							|  |  |  | 	var sockets = io.sockets.clients(); | 
					
						
							|  |  |  | 	if(!sockets || !sockets.length) { | 
					
						
							|  |  |  | 		return []; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	sockets = sockets.filter(function(s) { | 
					
						
							|  |  |  | 		return s.uid === parseInt(uid, 10); | 
					
						
							|  |  |  | 	}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return sockets; | 
					
						
							| 
									
										
										
										
											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-02-06 16:32:11 -05:00
										 |  |  | 	return Sockets.getUserSockets(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-31 20:19:59 -05:00
										 |  |  | 	user.getMultipleUserFields(uids, ['uid', 'username', 'userslug', 'picture', 'status'], function(err, users) { | 
					
						
							| 
									
										
										
										
											2014-01-19 14:27:07 -05:00
										 |  |  | 		if(!err) { | 
					
						
							| 
									
										
										
										
											2014-01-31 20:19:59 -05:00
										 |  |  | 			users = users.filter(function(user) { | 
					
						
							|  |  |  | 				return user.status !== 'offline'; | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-01-19 14:27:07 -05:00
										 |  |  | 			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-02-06 16:32:11 -05:00
										 |  |  | 	var anon = Sockets.getOnlineAnonCount(0); | 
					
						
							|  |  |  | 	var registered = Sockets.getConnectedClients().length; | 
					
						
							| 
									
										
										
										
											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; |