| 
									
										
										
										
											2013-04-23 15:39:23 -04:00
										 |  |  | var	config = require('../config.js'), | 
					
						
							|  |  |  | 	utils = require('../utils.js'), | 
					
						
							|  |  |  | 	RDB = require('./redis.js'), | 
					
						
							| 
									
										
										
										
											2013-04-29 12:22:02 -04:00
										 |  |  | 	crypto = require('crypto'), | 
					
						
							| 
									
										
										
										
											2013-04-23 15:39:23 -04:00
										 |  |  | 	emailjs = require('emailjs'), | 
					
						
							|  |  |  | 	emailjsServer = emailjs.server.connect(config.mailer); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | (function(User) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-29 12:22:02 -04:00
										 |  |  | 	User.get = function(uid, fields) { | 
					
						
							|  |  |  | 		if (uid > 0) { | 
					
						
							|  |  |  | 			var	keys = [], | 
					
						
							|  |  |  | 				returnData = {}, | 
					
						
							|  |  |  | 				removeEmail = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (!(fields instanceof Array)) fields = ['username', 'email']; | 
					
						
							|  |  |  | 			if (fields.indexOf('picture') !== -1 && fields.indexOf('email') === -1) { | 
					
						
							|  |  |  | 				fields.push('email'); | 
					
						
							|  |  |  | 				removeEmail = true; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			for(var f=0,numFields=fields.length;f<numFields;f++) { | 
					
						
							|  |  |  | 				keys.push('uid:' + uid + ':' + fields[f]); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			RDB.mget(keys, function(data) { | 
					
						
							|  |  |  | 				for(var x=0,numData=data.length;x<numData;x++) { | 
					
						
							|  |  |  | 					returnData[fields[x]] = data[x]; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 				 | 
					
						
							| 
									
										
										
										
											2013-04-29 12:22:02 -04:00
										 |  |  | 				if (returnData.picture !== undefined) { | 
					
						
							|  |  |  | 					var	md5sum = crypto.createHash('md5'); | 
					
						
							|  |  |  | 					md5sum.update(returnData.email.toLowerCase()); | 
					
						
							|  |  |  | 					returnData.picture = 'http://www.gravatar.com/avatar/' + md5sum.digest('hex') + '?s=24'; | 
					
						
							|  |  |  | 					if (removeEmail) delete returnData.email; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				socket.emit('api:user.get', returnData); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			socket.emit('api:user.get', { | 
					
						
							|  |  |  | 				username: "Anonymous User", | 
					
						
							|  |  |  | 				email: '', | 
					
						
							|  |  |  | 				picture: 'http://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e?s=24' | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-22 19:01:45 +00:00
										 |  |  | 	User.login = function(user) { | 
					
						
							|  |  |  | 		if (user.username == null || user.password == null) { | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 			return socket.emit('user.login', {'status': 0, 'message': 'Missing fields'}); | 
					
						
							| 
									
										
										
										
											2013-04-22 19:01:45 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		RDB.get('username:' + user.username + ':uid', function(uid) { | 
					
						
							|  |  |  | 			if (uid == null) { | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 				return socket.emit('user.login', {'status': 0, 'message': 'Username does not exist.'}); | 
					
						
							| 
									
										
										
										
											2013-04-22 19:01:45 +00:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			RDB.get('uid:' + uid + ':password', function(password) { | 
					
						
							|  |  |  | 				if (user.password != password) { | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 					return socket.emit('user.login', {'status': 0, 'message': 'Incorrect username / password combination.'}); | 
					
						
							| 
									
										
										
										
											2013-04-22 19:01:45 +00:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2013-04-24 16:42:12 -04:00
										 |  |  | 					// Start, replace, or extend a session
 | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 					RDB.get('sess:' + user.sessionID, function(session) { | 
					
						
							| 
									
										
										
										
											2013-04-25 11:04:08 -04:00
										 |  |  | 						if (session !== user.sessionID) { | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 							RDB.set('sess:' + user.sessionID + ':uid', uid, 60*60*24*14);	// Login valid for two weeks
 | 
					
						
							|  |  |  | 							RDB.set('uid:' + uid + ':session', user.sessionID, 60*60*24*14); | 
					
						
							| 
									
										
										
										
											2013-04-25 11:04:08 -04:00
										 |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 							RDB.expire('sess:' + user.sessionID + ':uid', 60*60*24*14);	// Defer expiration to two weeks from now
 | 
					
						
							|  |  |  | 							RDB.expire('uid:' + uid + ':session', 60*60*24*14); | 
					
						
							| 
									
										
										
										
											2013-04-25 11:04:08 -04:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-04-24 16:42:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-28 21:11:39 -04:00
										 |  |  | 					return socket.emit('user.login', {'status': 1, 'message': 'Logged in!'}); | 
					
						
							| 
									
										
										
										
											2013-04-22 19:01:45 +00:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-28 13:28:20 -04:00
										 |  |  | 	User.logout = function(sessionID, callback) { | 
					
						
							|  |  |  | 		User.get_uid_by_session(sessionID, function(uid) { | 
					
						
							|  |  |  | 			if (uid) { | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 				RDB.del('sess:' + sessionID + ':uid'); | 
					
						
							| 
									
										
										
										
											2013-04-28 13:28:20 -04:00
										 |  |  | 				RDB.del('uid:' + uid + ':session'); | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 				callback(true); | 
					
						
							|  |  |  | 			} else callback(false); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-22 19:01:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-22 17:31:51 -04:00
										 |  |  | 	User.create = function(username, password, email) { | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 		if (username == null || password == null) { | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 			return; socket.emit('user.create', {'status': 0, 'message': 'Missing fields'}); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		User.exists(username, function(exists) { | 
					
						
							|  |  |  | 			if (exists) { | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			RDB.incr('global:next_user_id', function(uid) { | 
					
						
							|  |  |  | 				RDB.set('username:' + username + ':uid', uid); | 
					
						
							|  |  |  | 				RDB.set('uid:' + uid + ':username', username); | 
					
						
							|  |  |  | 				RDB.set('uid:' + uid + ':password', password); | 
					
						
							| 
									
										
										
										
											2013-04-22 17:31:51 -04:00
										 |  |  | 				RDB.set('uid:' + uid + ':email', email); | 
					
						
							|  |  |  | 				RDB.set('email:' + email, uid); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 				 | 
					
						
							|  |  |  | 				RDB.incr('user:count', function(count) { | 
					
						
							| 
									
										
										
										
											2013-04-22 18:10:24 +00:00
										 |  |  | 					io.sockets.emit('user.count', {count: count}); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				RDB.lpush('user:users', username); | 
					
						
							| 
									
										
										
										
											2013-04-22 18:10:24 +00:00
										 |  |  | 				io.sockets.emit('user.latest', {username: username}); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 				socket.emit('user.create', {'status': 1}); | 
					
						
							| 
									
										
										
										
											2013-04-23 20:17:12 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 				socket.emit('event:alert', { | 
					
						
							| 
									
										
										
										
											2013-04-23 20:17:12 +00:00
										 |  |  | 					title: 'Thank you for registering', | 
					
						
							|  |  |  | 					message: 'You have successfully registered - welcome to nodebb!', | 
					
						
							|  |  |  | 					type: 'notify', | 
					
						
							|  |  |  | 					timeout: 2000 | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.exists = function(username, callback) { | 
					
						
							|  |  |  | 		User.get_uid_by_username(username, function(exists) { | 
					
						
							|  |  |  | 			exists = !!exists; | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 			socket.emit('user.exists', {exists: exists}) | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if (callback) { | 
					
						
							|  |  |  | 				callback(exists); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 	User.count = function() { | 
					
						
							|  |  |  | 		RDB.get('user:count', function(count) { | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 			socket.emit('user.count', {count: (count === null) ? 0 : count}); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 	User.latest = function() { | 
					
						
							|  |  |  | 		RDB.lrange('user:users', 0, 0, function(username) { | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 			socket.emit('user.latest', {username: username}); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 		});	 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.get_uid_by_username = function(username, callback) { | 
					
						
							|  |  |  | 		RDB.get('username:' + username + ':uid', callback); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-23 15:39:23 -04:00
										 |  |  | 	User.get_uid_by_email = function(email, callback) { | 
					
						
							|  |  |  | 		RDB.get('email:' + email, callback) | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-24 17:14:05 -04:00
										 |  |  | 	User.get_uid_by_session = function(session, callback) { | 
					
						
							| 
									
										
										
										
											2013-04-25 12:59:31 -04:00
										 |  |  | 		RDB.get('sess:' + session + ':uid', callback); | 
					
						
							| 
									
										
										
										
											2013-04-24 17:14:05 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-23 21:13:01 -04:00
										 |  |  | 	User.reset = { | 
					
						
							| 
									
										
										
										
											2013-04-23 23:28:34 -04:00
										 |  |  | 		validate: function(code, callback) { | 
					
						
							|  |  |  | 			if (typeof callback !== 'function') callback = undefined; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-23 21:13:01 -04:00
										 |  |  | 			RDB.get('reset:' + code + ':uid', function(uid) { | 
					
						
							|  |  |  | 				if (uid !== null) { | 
					
						
							|  |  |  | 					RDB.get('reset:' + code + ':expiry', function(expiry) { | 
					
						
							| 
									
										
										
										
											2013-04-23 23:28:34 -04:00
										 |  |  | 						if (expiry >= +new Date()/1000|0) { | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 							if (!callback) socket.emit('user:reset.valid', { valid: true }); | 
					
						
							| 
									
										
										
										
											2013-04-23 23:28:34 -04:00
										 |  |  | 							else callback(true); | 
					
						
							|  |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2013-04-23 22:35:03 -04:00
										 |  |  | 							// Expired, delete from db
 | 
					
						
							|  |  |  | 							RDB.del('reset:' + code + ':uid'); | 
					
						
							|  |  |  | 							RDB.del('reset:' + code + ':expiry'); | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 							if (!callback) socket.emit('user:reset.valid', { valid: false }); | 
					
						
							| 
									
										
										
										
											2013-04-23 23:28:34 -04:00
										 |  |  | 							else callback(false); | 
					
						
							| 
									
										
										
										
											2013-04-23 22:35:03 -04:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-04-23 21:13:01 -04:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-04-23 23:28:34 -04:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 					if (!callback) socket.emit('user:reset.valid', { valid: false }); | 
					
						
							| 
									
										
										
										
											2013-04-23 23:28:34 -04:00
										 |  |  | 					else callback(false); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-04-23 21:13:01 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		send: function(email) { | 
					
						
							|  |  |  | 			User.get_uid_by_email(email, function(uid) { | 
					
						
							|  |  |  | 				if (uid !== null) { | 
					
						
							|  |  |  | 					// Generate a new reset code
 | 
					
						
							|  |  |  | 					var reset_code = utils.generateUUID(); | 
					
						
							|  |  |  | 					RDB.set('reset:' + reset_code + ':uid', uid); | 
					
						
							|  |  |  | 					RDB.set('reset:' + reset_code + ':expiry', (60*60)+new Date()/1000|0);	// Active for one hour
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					var reset_link = config.url + 'reset/' + reset_code, | 
					
						
							|  |  |  | 						reset_email = global.templates['emails/reset'].parse({'RESET_LINK': reset_link}), | 
					
						
							|  |  |  | 						reset_email_plaintext = global.templates['emails/reset_plaintext'].parse({ 'RESET_LINK': reset_link }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					var message = emailjs.message.create({ | 
					
						
							|  |  |  | 						text: reset_email_plaintext, | 
					
						
							|  |  |  | 						from: config.mailer.from, | 
					
						
							|  |  |  | 						to: email, | 
					
						
							|  |  |  | 						subject: 'Password Reset Requested', | 
					
						
							|  |  |  | 						attachment: [ | 
					
						
							|  |  |  | 							{ | 
					
						
							|  |  |  | 								data: reset_email, | 
					
						
							|  |  |  | 								alternative: true | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						] | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					emailjsServer.send(message, function(err, success) { | 
					
						
							|  |  |  | 						if (err === null) { | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 							socket.emit('user.send_reset', { | 
					
						
							| 
									
										
										
										
											2013-04-23 21:13:01 -04:00
										 |  |  | 								status: "ok", | 
					
						
							|  |  |  | 								message: "code-sent", | 
					
						
							|  |  |  | 								email: email | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 							socket.emit('user.send_reset', { | 
					
						
							| 
									
										
										
										
											2013-04-23 21:13:01 -04:00
										 |  |  | 								status: "error", | 
					
						
							|  |  |  | 								message: "send-failed" | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 							throw new Error(err); | 
					
						
							| 
									
										
										
										
											2013-04-23 15:39:23 -04:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-04-23 21:13:01 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 					socket.emit('user.send_reset', { | 
					
						
							| 
									
										
										
										
											2013-04-23 21:13:01 -04:00
										 |  |  | 						status: "error", | 
					
						
							|  |  |  | 						message: "invalid-email", | 
					
						
							|  |  |  | 						email: email | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-04-23 23:28:34 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		commit: function(code, password) { | 
					
						
							|  |  |  | 			this.validate(code, function(validated) { | 
					
						
							|  |  |  | 				if (validated) { | 
					
						
							|  |  |  | 					RDB.get('reset:' + code + ':uid', function(uid) { | 
					
						
							|  |  |  | 						RDB.set('uid:' + uid + ':password', password); | 
					
						
							|  |  |  | 						RDB.del('reset:' + code + ':uid'); | 
					
						
							|  |  |  | 						RDB.del('reset:' + code + ':expiry'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 						socket.emit('user:reset.commit', { status: 'ok' }); | 
					
						
							| 
									
										
										
										
											2013-04-23 23:28:34 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-04-23 21:13:01 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-04-23 12:06:22 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-22 17:31:51 -04:00
										 |  |  | 	User.email = { | 
					
						
							| 
									
										
										
										
											2013-04-23 12:06:22 -04:00
										 |  |  | 		exists: function(email, callback) { | 
					
						
							| 
									
										
										
										
											2013-04-23 15:39:23 -04:00
										 |  |  | 			User.get_uid_by_email(email, function(exists) { | 
					
						
							| 
									
										
										
										
											2013-04-22 17:31:51 -04:00
										 |  |  | 				exists = !!exists; | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 				if (typeof callback !== 'function') socket.emit('user.email.exists', { exists: exists }); | 
					
						
							| 
									
										
										
										
											2013-04-23 12:06:22 -04:00
										 |  |  | 				else callback(exists); | 
					
						
							| 
									
										
										
										
											2013-04-22 17:31:51 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-25 16:27:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User.active = { | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 		get_record : function() { | 
					
						
							|  |  |  | 			RDB.get('global:active_user_record', function(record) { | 
					
						
							|  |  |  | 				socket.emit('api:user.active.get_record', record); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 16:27:22 -04:00
										 |  |  | 		get: function(callback) { | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 			function user_record(total) { | 
					
						
							|  |  |  | 				RDB.get('global:active_user_record', function(record) { | 
					
						
							|  |  |  | 					if (total > record) { | 
					
						
							|  |  |  | 						RDB.set('global:active_user_record', total); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-25 16:27:22 -04:00
										 |  |  | 			RDB.keys('active:*', function(active) { | 
					
						
							|  |  |  | 				var	returnObj = { | 
					
						
							|  |  |  | 						users: 0, | 
					
						
							|  |  |  | 						anon: 0, | 
					
						
							|  |  |  | 						uids: [] | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					keys = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-28 13:28:20 -04:00
										 |  |  | 				if (active.length > 0) { | 
					
						
							|  |  |  | 					for(var a in active) { | 
					
						
							|  |  |  | 						keys.push('sess:' + active[a].split(':')[1] + ':uid'); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-04-25 16:27:22 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-28 13:28:20 -04:00
										 |  |  | 					RDB.mget(keys, function(uids) { | 
					
						
							|  |  |  | 						for(var u in uids) { | 
					
						
							|  |  |  | 							if (uids[u] !== null) { | 
					
						
							|  |  |  | 								if (returnObj.uids.indexOf(uids[u]) === -1) { | 
					
						
							|  |  |  | 									returnObj.users++; | 
					
						
							|  |  |  | 									returnObj.uids.push(uids[u]); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							} else { | 
					
						
							|  |  |  | 								returnObj.anon++; | 
					
						
							| 
									
										
										
										
											2013-04-25 16:27:22 -04:00
										 |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 						user_record(returnObj.anon + returnObj.users); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-28 13:28:20 -04:00
										 |  |  | 						if (callback === undefined) { | 
					
						
							| 
									
										
										
										
											2013-04-28 16:04:00 -04:00
										 |  |  | 							io.sockets.emit('api:user.active.get', returnObj) | 
					
						
							| 
									
										
										
										
											2013-04-28 13:28:20 -04:00
										 |  |  | 						} else { | 
					
						
							|  |  |  | 							callback(returnObj); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2013-04-28 16:04:00 -04:00
										 |  |  | 					io.sockets.emit('api:user.active.get', returnObj) | 
					
						
							| 
									
										
										
										
											2013-04-28 13:28:20 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-04-25 16:27:22 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		register: function(sessionID) { | 
					
						
							| 
									
										
										
										
											2013-04-28 13:28:20 -04:00
										 |  |  | 			RDB.set('active:' + sessionID, '', 60*10);	// Active state persists for 10 minutes
 | 
					
						
							| 
									
										
										
										
											2013-04-28 15:56:44 -04:00
										 |  |  | 			this.get(); | 
					
						
							| 
									
										
										
										
											2013-04-25 16:27:22 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | }(exports)); |