| 
									
										
										
										
											2013-04-23 15:39:23 -04:00
										 |  |  | var	config = require('../config.js'), | 
					
						
							| 
									
										
										
										
											2013-05-01 14:23:57 -04:00
										 |  |  | 	utils = require('./utils.js'), | 
					
						
							| 
									
										
										
										
											2013-04-23 15:39:23 -04:00
										 |  |  | 	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'), | 
					
						
							| 
									
										
										
										
											2013-05-03 13:05:57 -04:00
										 |  |  | 	emailjsServer = emailjs.server.connect(config.mailer), | 
					
						
							|  |  |  | 	bcrypt = require('bcrypt'); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | (function(User) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-29 12:22:02 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 	User.getUserField = function(uid, field, callback) { | 
					
						
							|  |  |  | 		RDB.db.hget(String(uid), field, function(err, data){ | 
					
						
							|  |  |  | 			if(err === null) | 
					
						
							|  |  |  | 				callback(data); | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				console.log(err); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	User.getUserFields = function(uid, fields, callback) { | 
					
						
							|  |  |  | 		RDB.db.hmget(String(uid), fields, function(err, data){ | 
					
						
							|  |  |  | 			if(err === null) { | 
					
						
							|  |  |  | 				var returnData = {}; | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 				 | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 				for(var i=0, ii=fields.length; i<ii; ++i) { | 
					
						
							|  |  |  | 					returnData[fields[i]] = data[i]; | 
					
						
							| 
									
										
										
										
											2013-04-29 12:22:02 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				callback(returnData); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			else | 
					
						
							|  |  |  | 				console.log(err); | 
					
						
							|  |  |  | 		});		 | 
					
						
							| 
									
										
										
										
											2013-04-29 12:22:02 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-04 18:20:22 -04:00
										 |  |  | 	User.getUserData = function(uid, callback) { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 		RDB.db.hgetall(String(uid), function(err, data){ | 
					
						
							|  |  |  | 			if(err === null) | 
					
						
							| 
									
										
										
										
											2013-05-05 18:40:04 -04:00
										 |  |  | 			{ | 
					
						
							| 
									
										
										
										
											2013-05-05 19:35:17 -04:00
										 |  |  | 				if(data && data['password']) | 
					
						
							|  |  |  | 					delete data['password']; | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 				callback(data); | 
					
						
							| 
									
										
										
										
											2013-05-05 18:40:04 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 			else | 
					
						
							|  |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-05-04 18:20:22 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 17:09:38 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User.get_gravatars_by_uids = function(uids, size, callback) { | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-05-05 18:07:54 -04:00
										 |  |  | 		var gravatars = []; | 
					
						
							| 
									
										
										
										
											2013-05-03 17:09:38 +00:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-05-05 18:07:54 -04:00
										 |  |  | 		for(var i=0, ii=uids.length; i<ii; ++i) { | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			User.getUserField(uids[i], 'picture', function(picture) { | 
					
						
							|  |  |  | 				gravatars.push(picture); | 
					
						
							|  |  |  | 				if(gravatars.length >= uids.length) | 
					
						
							|  |  |  | 					callback(gravatars); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-05-03 17:09:38 +00:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 21:03:37 -04:00
										 |  |  | 	User.loginViaLocal = function(username, password, next) { | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 21:03:37 -04:00
										 |  |  | 		if (!username || !password) { | 
					
						
							|  |  |  | 			return next({ | 
					
						
							|  |  |  | 				status: 'error', | 
					
						
							|  |  |  | 				message: 'invalid-user' | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			RDB.get('username:' + username + ':uid', function(uid) { | 
					
						
							|  |  |  | 				if (uid == null) { | 
					
						
							|  |  |  | 					return next({ | 
					
						
							|  |  |  | 						status: 'error', | 
					
						
							|  |  |  | 						message: 'invalid-user' | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 				 | 
					
						
							|  |  |  | 				User.getUserField(uid, 'password', function(user_password) { | 
					
						
							|  |  |  | 					 | 
					
						
							| 
									
										
										
										
											2013-05-03 13:05:57 -04:00
										 |  |  | 					bcrypt.compare(password, user_password, function(err, res) { | 
					
						
							|  |  |  | 						if (res === true) { | 
					
						
							|  |  |  | 							next({ | 
					
						
							|  |  |  | 								status: "ok", | 
					
						
							|  |  |  | 								user: { | 
					
						
							|  |  |  | 									uid: uid | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							next({ | 
					
						
							|  |  |  | 								status: 'error', | 
					
						
							|  |  |  | 								message: 'invalid-password' | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-05-01 21:03:37 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 11:11:10 -04:00
										 |  |  | 	User.loginViaTwitter = function(twid, handle, callback) { | 
					
						
							|  |  |  | 		User.get_uid_by_twitter_id(twid, function(uid) { | 
					
						
							|  |  |  | 			if (uid !== null) { | 
					
						
							|  |  |  | 				// Existing User
 | 
					
						
							|  |  |  | 				callback(null, { | 
					
						
							|  |  |  | 					uid: uid | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				// New User
 | 
					
						
							|  |  |  | 				User.create(handle, null, null, function(err, uid) { | 
					
						
							|  |  |  | 					if (err !== null) { | 
					
						
							|  |  |  | 						callback(err); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						// Save twitter-specific information to the user
 | 
					
						
							|  |  |  | 						RDB.set('uid:' + uid + ':twid', twid); | 
					
						
							|  |  |  | 						RDB.set('twid:' + twid + ':uid', uid); | 
					
						
							|  |  |  | 						callback(null, { | 
					
						
							|  |  |  | 							uid: uid | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-02 14:47:00 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.loginViaGoogle = function(gplusid, handle, email, callback) { | 
					
						
							|  |  |  | 		User.get_uid_by_google_id(gplusid, function(uid) { | 
					
						
							|  |  |  | 			if (uid !== null) { | 
					
						
							|  |  |  | 				// Existing User
 | 
					
						
							|  |  |  | 				callback(null, { | 
					
						
							|  |  |  | 					uid: uid | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				// New User
 | 
					
						
							|  |  |  | 				User.create(handle, null, email, function(err, uid) { | 
					
						
							|  |  |  | 					if (err !== null) { | 
					
						
							|  |  |  | 						callback(err); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						// Save twitter-specific information to the user
 | 
					
						
							|  |  |  | 						RDB.set('uid:' + uid + ':gplusid', gplusid); | 
					
						
							|  |  |  | 						RDB.set('gplusid:' + gplusid + ':uid', uid); | 
					
						
							|  |  |  | 						callback(null, { | 
					
						
							|  |  |  | 							uid: uid | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-02 11:11:10 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 15:57:43 -04:00
										 |  |  | 	User.loginViaFacebook = function(fbid, name, email, callback) { | 
					
						
							|  |  |  | 		User.get_uid_by_fbid(fbid, function(uid) { | 
					
						
							|  |  |  | 			if (uid !== null) { | 
					
						
							|  |  |  | 				// Existing User
 | 
					
						
							|  |  |  | 				callback(null, { | 
					
						
							|  |  |  | 					uid: uid | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				// New User
 | 
					
						
							|  |  |  | 				User.create(name, null, email, function(err, uid) { | 
					
						
							|  |  |  | 					if (err !== null) { | 
					
						
							|  |  |  | 						callback(err); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						// Save twitter-specific information to the user
 | 
					
						
							|  |  |  | 						RDB.set('uid:' + uid + ':fbid', fbid); | 
					
						
							|  |  |  | 						RDB.set('fbid:' + fbid + ':uid', uid); | 
					
						
							|  |  |  | 						callback(null, { | 
					
						
							|  |  |  | 							uid: uid | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-05-02 10:15:55 -04:00
										 |  |  | 	User.create = function(username, password, email, callback) { | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 		if(!username) { | 
					
						
							|  |  |  | 			console.log("invalid registration data! username ["+username+"], password ["+password+"], email ["+email+"]"); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 		// TODO : check if username email is unique!! -baris
 | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		RDB.incr('global:next_user_id', function(uid) { | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			console.log("Registering uid : " + uid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			User.hashPassword(password, function(hash) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				RDB.db.hmset(String(uid), { | 
					
						
							|  |  |  | 					'username' : username, | 
					
						
							|  |  |  | 					'email' : email, | 
					
						
							|  |  |  | 					'joindate' : new Date().getTime(), | 
					
						
							|  |  |  | 					'password' : hash, | 
					
						
							| 
									
										
										
										
											2013-05-05 18:07:54 -04:00
										 |  |  | 					'picture' : User.createGravatarURLFromEmail(email), | 
					
						
							|  |  |  | 					'reputation': 0 | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 				 | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 				RDB.set('username:' + username + ':uid', uid); | 
					
						
							|  |  |  | 				RDB.set('email:' + email +':uid', uid);			 | 
					
						
							| 
									
										
										
										
											2013-05-04 18:20:22 -04:00
										 |  |  | 				 | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 				if(email) | 
					
						
							|  |  |  | 					User.sendConfirmationEmail(email); | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											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-02 10:15:55 -04:00
										 |  |  | 				callback(null, uid); | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 				 | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 	User.createGravatarURLFromEmail = function(email) { | 
					
						
							|  |  |  | 		if(email) { | 
					
						
							|  |  |  | 			var md5sum = crypto.createHash('md5'); | 
					
						
							|  |  |  | 			md5sum.update(email.toLowerCase()); | 
					
						
							| 
									
										
										
										
											2013-05-05 18:07:54 -04:00
										 |  |  | 			var gravatarURL = 'http://www.gravatar.com/avatar/' + md5sum.digest('hex'); | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 			return gravatarURL; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else { | 
					
						
							| 
									
										
										
										
											2013-05-05 18:07:54 -04:00
										 |  |  | 			return "http://www.gravatar.com/avatar/d41d8cd98f00b204e9800998ecf8427e";	 | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.hashPassword = function(password, callback) { | 
					
						
							|  |  |  | 		if(!password) { | 
					
						
							|  |  |  | 			callback(password); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		bcrypt.genSalt(10, function(err, salt) { | 
					
						
							|  |  |  | 			bcrypt.hash(password, salt, function(err, hash) { | 
					
						
							|  |  |  | 				callback(hash);	 | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.sendConfirmationEmail = function (email) { | 
					
						
							|  |  |  | 		var confirm_code = utils.generateUUID(), | 
					
						
							|  |  |  | 			confirm_link = config.url + 'confirm/' + confirm_code, | 
					
						
							|  |  |  | 			confirm_email = global.templates['emails/header'] + global.templates['emails/email_confirm'].parse({'CONFIRM_LINK': confirm_link}) + global.templates['emails/footer'], | 
					
						
							|  |  |  | 			confirm_email_plaintext = global.templates['emails/email_confirm_plaintext'].parse({ 'CONFIRM_LINK': confirm_link }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Email confirmation code
 | 
					
						
							|  |  |  | 		RDB.set('email:' + email + ':confirm', confirm_code, 60*60*2); | 
					
						
							|  |  |  | 		RDB.set('confirm:' + confirm_code + ':email', email, 60*60*2);	// Expire after 2 hours
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Send intro email w/ confirm code
 | 
					
						
							|  |  |  | 		var message = emailjs.message.create({ | 
					
						
							|  |  |  | 			text: confirm_email_plaintext, | 
					
						
							|  |  |  | 			from: config.mailer.from, | 
					
						
							|  |  |  | 			to: email, | 
					
						
							|  |  |  | 			subject: '[NodeBB] Registration Email Verification', | 
					
						
							|  |  |  | 			attachment: [ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					data: confirm_email, | 
					
						
							|  |  |  | 					alternative: true | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			] | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 		emailjsServer.send(message, function(err, success) { | 
					
						
							|  |  |  | 			if (err)  | 
					
						
							|  |  |  | 				console.log(err); | 
					
						
							|  |  |  | 		});	 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 12:54:27 -04:00
										 |  |  | 	User.exists = function(socket, username, callback) { | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 		User.get_uid_by_username(username, function(exists) { | 
					
						
							|  |  |  | 			exists = !!exists; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-05 17:38:08 -04:00
										 |  |  | 			if (callback)  | 
					
						
							|  |  |  | 				callback(exists); | 
					
						
							|  |  |  | 			else  | 
					
						
							|  |  |  | 				socket.emit('user.exists', {exists: exists}); | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-02 12:54:27 -04:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	User.count = function(socket) { | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 		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
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-02 12:54:27 -04:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	User.latest = function(socket) { | 
					
						
							| 
									
										
										
										
											2013-04-22 16:51:32 +00:00
										 |  |  | 		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-05-02 16:28:04 -04:00
										 |  |  | 	User.get_username_by_uid = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-05-05 18:07:54 -04:00
										 |  |  | 		User.getUserField(uid, 'username', callback); | 
					
						
							| 
									
										
										
										
											2013-05-02 16:28:04 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 11:43:01 -04:00
										 |  |  | 	User.get_usernames_by_uids = function(uids, callback) { | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-05-05 18:07:54 -04:00
										 |  |  | 		var usernames = []; | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		for(var i=0, ii=uids.length; i<ii; ++i) { | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 			User.get_username_by_uid(uids[i], function(username){ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				usernames.push(username); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if(usernames.length >= uids.length) | 
					
						
							|  |  |  | 					callback(usernames); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-05-03 11:43:01 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 	User.get_user_postdetails = function(uids, callback) { | 
					
						
							|  |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-05-05 19:13:08 -04:00
										 |  |  | 		var usernames = []; | 
					
						
							|  |  |  | 		var reputations = []; | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		for(var i=0, ii=uids.length; i<ii; ++i) { | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 			User.getUserFields(uids[i], ['username','reputation'], function(data){ | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 				usernames.push(data['username']); | 
					
						
							|  |  |  | 				reputations.push(data['reputation']); | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 				if(usernames.length >= uids.length) { | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					callback({ | 
					
						
							|  |  |  | 						'username':usernames, | 
					
						
							|  |  |  | 						'rep':reputations | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-05 19:13:08 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-05-03 18:54:19 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-23 15:39:23 -04:00
										 |  |  | 	User.get_uid_by_email = function(email, callback) { | 
					
						
							| 
									
										
										
										
											2013-05-05 18:40:04 -04:00
										 |  |  | 		RDB.get('email:' + email + ':uid', callback) | 
					
						
							| 
									
										
										
										
											2013-04-23 15:39:23 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-05-02 11:11:10 -04:00
										 |  |  | 	User.get_uid_by_twitter_id = function(twid, callback) { | 
					
						
							|  |  |  | 		RDB.get('twid:' + twid + ':uid', function(uid) { | 
					
						
							|  |  |  | 			callback(uid); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 14:47:00 -04:00
										 |  |  | 	User.get_uid_by_google_id = function(gplusid, callback) { | 
					
						
							|  |  |  | 		RDB.get('gplusid:' + gplusid + ':uid', function(uid) { | 
					
						
							|  |  |  | 			callback(uid); | 
					
						
							|  |  |  | 		});	 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-02 15:57:43 -04:00
										 |  |  | 	User.get_uid_by_fbid = function(fbid, callback) { | 
					
						
							|  |  |  | 		RDB.get('fbid:' + fbid + ':uid', function(uid) { | 
					
						
							|  |  |  | 			callback(uid); | 
					
						
							|  |  |  | 		});	 | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-01 21:03:37 -04:00
										 |  |  | 	User.session_ping = function(sessionID, uid) { | 
					
						
							|  |  |  | 		// Start, replace, or extend a session
 | 
					
						
							|  |  |  | 		RDB.get('sess:' + sessionID, function(session) { | 
					
						
							|  |  |  | 			RDB.set('sess:' + sessionID + ':uid', uid, 60*60*24*14);	// Login valid for two weeks
 | 
					
						
							|  |  |  | 			RDB.set('uid:' + uid + ':session', sessionID, 60*60*24*14); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-04-23 21:13:01 -04:00
										 |  |  | 	User.reset = { | 
					
						
							| 
									
										
										
										
											2013-05-02 12:54:27 -04:00
										 |  |  | 		validate: function(socket, code, callback) { | 
					
						
							| 
									
										
										
										
											2013-04-23 23:28:34 -04:00
										 |  |  | 			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
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-05-02 12:54:27 -04:00
										 |  |  | 		send: function(socket, email) { | 
					
						
							| 
									
										
										
										
											2013-04-23 21:13:01 -04:00
										 |  |  | 			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
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-05-02 12:54:27 -04:00
										 |  |  | 		commit: function(socket, code, password) { | 
					
						
							| 
									
										
										
										
											2013-04-23 23:28:34 -04:00
										 |  |  | 			this.validate(code, function(validated) { | 
					
						
							|  |  |  | 				if (validated) { | 
					
						
							|  |  |  | 					RDB.get('reset:' + code + ':uid', function(uid) { | 
					
						
							| 
									
										
										
										
											2013-05-05 19:29:58 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						RDB.db.hset(String(uid), 'password', password); | 
					
						
							| 
									
										
										
										
											2013-04-23 23:28:34 -04:00
										 |  |  | 						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-05-02 12:54:27 -04:00
										 |  |  | 		exists: function(socket, 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-05-05 13:05:05 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		confirm: function(code, callback) { | 
					
						
							|  |  |  | 			RDB.get('confirm:' + code + ':email', function(email) { | 
					
						
							|  |  |  | 				if (email !== null) { | 
					
						
							|  |  |  | 					RDB.set('email:' + email + ':confirm', true); | 
					
						
							|  |  |  | 					RDB.del('confirm:' + code + ':email'); | 
					
						
							|  |  |  | 					callback({ | 
					
						
							|  |  |  | 						status: 'ok' | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					callback({ | 
					
						
							|  |  |  | 						status: 'not_ok' | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-04-22 17:31:51 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-04-25 16:27:22 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User.active = { | 
					
						
							| 
									
										
										
										
											2013-05-02 12:54:27 -04:00
										 |  |  | 		get_record : function(socket) { | 
					
						
							| 
									
										
										
										
											2013-05-01 18:01:02 +00:00
										 |  |  | 			RDB.mget(['global:active_user_record', 'global:active_user_record_date'], function(data) { | 
					
						
							|  |  |  | 				socket.emit('api:user.active.get_record', {record: data[0], timestamp: data[1]}); | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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-05-01 18:01:02 +00:00
										 |  |  | 						RDB.set('global:active_user_record_date', new Date().getTime()); | 
					
						
							| 
									
										
										
										
											2013-05-01 16:14:09 +00:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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)); |