| 
									
										
										
										
											2013-05-27 14:02:57 -04:00
										 |  |  | var utils = require('./../public/src/utils.js'), | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	RDB = require('./redis.js'), | 
					
						
							|  |  |  | 	crypto = require('crypto'), | 
					
						
							|  |  |  | 	emailjs = require('emailjs'), | 
					
						
							|  |  |  | 	emailjsServer = emailjs.server.connect(config.mailer), | 
					
						
							| 
									
										
										
										
											2013-05-17 14:48:05 -04:00
										 |  |  | 	bcrypt = require('bcrypt'), | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 	marked = require('marked'), | 
					
						
							|  |  |  | 	notifications = require('./notifications.js'), | 
					
						
							| 
									
										
										
										
											2013-06-18 10:47:43 -04:00
										 |  |  | 	topics = require('./topics.js'), | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 	async = require('async'); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | (function(User) { | 
					
						
							| 
									
										
										
										
											2013-07-05 19:11:27 -04:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 	User.create = function(username, password, email, callback) { | 
					
						
							|  |  |  | 		username = username.trim(), email = email.trim(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// @todo return a proper error? use node-validator?
 | 
					
						
							|  |  |  | 		if(!utils.isEmailValid(email) || !utils.isUserNameValid(username) || !utils.isPasswordValid(password)) { | 
					
						
							|  |  |  | 			console.log('Invalid email/username/password!'); | 
					
						
							|  |  |  | 			callback(null, 0); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		var userslug = utils.slugify(username); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		User.exists(userslug, function(exists) { | 
					
						
							|  |  |  | 			if(exists) { | 
					
						
							|  |  |  | 				callback(null, 0); | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 			 | 
					
						
							|  |  |  | 			User.isEmailAvailable(email, function(available) { | 
					
						
							|  |  |  | 				if(!available) { | 
					
						
							|  |  |  | 					callback(null, 0); | 
					
						
							|  |  |  | 					return; | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 				RDB.incr('global:next_user_id', function(err, uid) { | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 					RDB.handle(err); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 					var gravatar = User.createGravatarURLFromEmail(email); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					RDB.hmset('user:'+uid, { | 
					
						
							|  |  |  | 						'uid': uid, | 
					
						
							|  |  |  | 						'username' : username, | 
					
						
							|  |  |  | 						'userslug' : userslug, | 
					
						
							|  |  |  | 						'fullname': '', | 
					
						
							|  |  |  | 						'location':'', | 
					
						
							|  |  |  | 						'birthday':'', | 
					
						
							|  |  |  | 						'website':'', | 
					
						
							|  |  |  | 						'email' : email, | 
					
						
							|  |  |  | 						'signature':'', | 
					
						
							|  |  |  | 						'joindate' : Date.now(), | 
					
						
							|  |  |  | 						'picture': gravatar, | 
					
						
							|  |  |  | 						'gravatarpicture' : gravatar, | 
					
						
							|  |  |  | 						'uploadedpicture': '', | 
					
						
							|  |  |  | 						'reputation': 0, | 
					
						
							|  |  |  | 						'postcount': 0, | 
					
						
							|  |  |  | 						'lastposttime': 0, | 
					
						
							|  |  |  | 						'administrator': (uid == 1) ? 1 : 0 | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					RDB.set('username:' + username + ':uid', uid); | 
					
						
							|  |  |  | 					RDB.set('email:' + email +':uid', uid); | 
					
						
							|  |  |  | 					RDB.set('userslug:'+ userslug +':uid', uid); | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 					if(email) { | 
					
						
							|  |  |  | 						User.sendConfirmationEmail(email); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 					RDB.incr('usercount', function(err, count) { | 
					
						
							|  |  |  | 						RDB.handle(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						io.sockets.emit('user.count', {count: count}); | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 					RDB.lpush('userlist', uid); | 
					
						
							|  |  |  | 					 | 
					
						
							|  |  |  | 					io.sockets.emit('user.latest', {userslug: userslug, username: username}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (password) { | 
					
						
							|  |  |  | 						User.hashPassword(password, function(hash) { | 
					
						
							|  |  |  | 							User.setUserField(uid, 'password', hash); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					callback(null, uid); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	User.delete = function(uid, callback) { | 
					
						
							|  |  |  | 		RDB.exists('user:'+uid, function(err, exists) { | 
					
						
							|  |  |  | 			if(exists === 1) { | 
					
						
							|  |  |  | 				console.log('deleting uid ' + uid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				User.getUserData(uid, function(data) { | 
					
						
							|  |  |  | 					RDB.del('username:' + data['username'] + ':uid'); | 
					
						
							|  |  |  | 					RDB.del('email:' + data['email'] +':uid'); | 
					
						
							|  |  |  | 					RDB.del('userslug:'+ data['userslug'] +':uid'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					RDB.del('user:' + uid); | 
					
						
							|  |  |  | 					RDB.del('followers:' + uid); | 
					
						
							|  |  |  | 					RDB.del('following:' + uid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					RDB.lrem('userlist', 1, uid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					callback(true); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				callback(false); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	User.getUserField = function(uid, field, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 		RDB.hget('user:' + uid, field, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 			if(err === null) { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				callback(data); | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	User.getUserFields = function(uid, fields, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-02 17:01:49 -04:00
										 |  |  | 		RDB.hmget('user:' + uid, fields, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 			if(err === null) { | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 				for(var i = 0, returnData = {}, ii=fields.length; i<ii; ++i) { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 					returnData[fields[i]] = data[i]; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				callback(returnData); | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.getMultipleUserFields = function(uids, fields, callback) { | 
					
						
							| 
									
										
										
										
											2013-05-16 13:13:46 -04:00
										 |  |  | 		if(uids.length === 0) { | 
					
						
							|  |  |  | 			callback({}); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 19:08:59 -04:00
										 |  |  | 		var returnData = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		uuids = uids.filter(function(value, index, self) { | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 			return self.indexOf(value) === index; | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 19:08:59 -04:00
										 |  |  | 		function iterator(uid, callback) { | 
					
						
							|  |  |  | 			User.getUserFields(uid, fields, function(userData) { | 
					
						
							|  |  |  | 				returnData.push(userData); | 
					
						
							|  |  |  | 				callback(null); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-07-05 19:08:59 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		async.each(uuids, iterator, function(err) { | 
					
						
							|  |  |  | 			if(!err) { | 
					
						
							|  |  |  | 				callback(returnData); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				console.log(err); | 
					
						
							|  |  |  | 				callback(null); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.getUserData = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 		RDB.hgetall('user:' + uid, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-06-21 12:53:50 -04:00
										 |  |  | 			if(err === null) { | 
					
						
							| 
									
										
										
										
											2013-06-20 11:20:37 -04:00
										 |  |  | 				if(data) { | 
					
						
							|  |  |  | 					if(data['password']) | 
					
						
							|  |  |  | 						delete data['password']; | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-06-20 11:22:10 -04:00
										 |  |  | 				callback(data); | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 	User.updateProfile = function(socket, uid, data) { | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-17 14:48:05 -04:00
										 |  |  | 		var fields = ['email', 'fullname', 'website', 'location', 'birthday', 'signature']; | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 		function isSignatureValid(next) { | 
					
						
							|  |  |  | 			if(data['signature'] !== undefined && data['signature'].length > 150) { | 
					
						
							|  |  |  | 				next({error:'Signature can\'t be longer than 150 characters!'}, false); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				next(null, true);	 | 
					
						
							|  |  |  | 			}			 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		 | 
					
						
							|  |  |  | 		function isEmailAvailable(next) { | 
					
						
							|  |  |  | 			if(data['email'] !== undefined) { | 
					
						
							|  |  |  | 				User.getUserField(uid, 'email', function(email) { | 
					
						
							|  |  |  | 					if(email !== data['email']) { | 
					
						
							|  |  |  | 						User.isEmailAvailable(data['email'], function(available) { | 
					
						
							|  |  |  | 							if(!available) {						 | 
					
						
							|  |  |  | 								next({error:'Email not available!'}, false); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						next(null, true);		 | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				next(null, true); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-21 13:44:53 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		async.series([isSignatureValid, isEmailAvailable], function(err, results) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				socket.emit('event:alert', { | 
					
						
							|  |  |  | 					title: 'Error', | 
					
						
							|  |  |  | 					message: err.error, | 
					
						
							|  |  |  | 					type: 'error', | 
					
						
							|  |  |  | 					timeout: 2000 | 
					
						
							|  |  |  | 				});	 | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				updateFields(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 		function updateFields() { | 
					
						
							|  |  |  | 			for(var i = 0, key, ii = fields.length; i < ii; ++i) { | 
					
						
							|  |  |  | 				key = fields[i]; | 
					
						
							| 
									
										
										
										
											2013-05-16 11:53:27 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 				if(data[key] !== undefined) { | 
					
						
							|  |  |  | 					if(key === 'email') { | 
					
						
							|  |  |  | 						User.setUserField(uid, 'gravatarpicture', User.createGravatarURLFromEmail(data[key])); | 
					
						
							|  |  |  | 						RDB.set('email:' + data['email'] +':uid', uid); | 
					
						
							|  |  |  | 					} else if(key === 'signature') { | 
					
						
							|  |  |  | 						data[key] = utils.strip_tags(data[key]); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 					User.setUserField(uid, key, data[key]); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-13 16:41:28 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			socket.emit('event:alert', { | 
					
						
							|  |  |  | 				title: 'Success', | 
					
						
							|  |  |  | 				message: 'Your profile has been updated successfully!', | 
					
						
							|  |  |  | 				type: 'success', | 
					
						
							|  |  |  | 				timeout: 2000 | 
					
						
							|  |  |  | 			});	 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 	User.isEmailAvailable = function(email, callback) { | 
					
						
							|  |  |  | 		RDB.exists('email:' + email + ':uid' , function(err, exists) { | 
					
						
							|  |  |  | 			if(!err) { | 
					
						
							|  |  |  | 				callback(exists !== 1); | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				console.log(err); | 
					
						
							|  |  |  | 				callback(false); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 12:10:21 -04:00
										 |  |  | 	User.changePassword = function(socket, uid, data, callback) { | 
					
						
							|  |  |  | 		if(!utils.isPasswordValid(data.newPassword)) { | 
					
						
							|  |  |  | 			socket.emit('event:alert', { | 
					
						
							|  |  |  | 				title: 'Error', | 
					
						
							|  |  |  | 				message: 'Invalid password!', | 
					
						
							|  |  |  | 				type: 'error', | 
					
						
							|  |  |  | 				timeout: 2000 | 
					
						
							|  |  |  | 			});		 | 
					
						
							|  |  |  | 			callback(false); | 
					
						
							|  |  |  | 			return;		 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		User.getUserField(uid, 'password', function(user_password) { | 
					
						
							|  |  |  | 			bcrypt.compare(data.currentPassword, user_password, function(err, res) { | 
					
						
							|  |  |  | 				if(err) { | 
					
						
							|  |  |  | 					console.log(err); | 
					
						
							|  |  |  | 					callback(false); | 
					
						
							|  |  |  | 					return; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (res) { | 
					
						
							|  |  |  | 					User.hashPassword(data.newPassword, function(hash) { | 
					
						
							|  |  |  | 						User.setUserField(uid, 'password', hash); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						socket.emit('event:alert', { | 
					
						
							|  |  |  | 							title: 'Success', | 
					
						
							|  |  |  | 							message: 'Your password is updated!', | 
					
						
							|  |  |  | 							type: 'success', | 
					
						
							|  |  |  | 							timeout: 2000 | 
					
						
							|  |  |  | 						});	 | 
					
						
							|  |  |  | 						callback(true); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					socket.emit('event:alert', { | 
					
						
							|  |  |  | 						title: 'Warning', | 
					
						
							|  |  |  | 						message: 'Your current password is not correct!', | 
					
						
							|  |  |  | 						type: 'warning', | 
					
						
							|  |  |  | 						timeout: 2000 | 
					
						
							|  |  |  | 					});	 | 
					
						
							|  |  |  | 					callback(false); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	User.setUserField = function(uid, field, value) { | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 		RDB.hset('user:' + uid, field, value); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.incrementUserFieldBy = function(uid, field, value) { | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 		RDB.hincrby('user:' + uid, field, value); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 15:46:13 -04:00
										 |  |  | 	User.getUserList = function(callback) { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		var data = []; | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 23:46:32 -04:00
										 |  |  | 		RDB.lrange('userlist', 0, -1, function(err, uids) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 23:46:32 -04:00
										 |  |  | 			RDB.handle(err); | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			function iterator(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-06-20 14:45:38 -04:00
										 |  |  | 				User.getUserData(uid, function(userData) { | 
					
						
							|  |  |  | 					data.push(userData); | 
					
						
							| 
									
										
										
										
											2013-07-05 23:46:32 -04:00
										 |  |  | 					callback(null); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-05 23:46:32 -04:00
										 |  |  | 			 | 
					
						
							|  |  |  | 			async.each(uids, iterator, function(err) { | 
					
						
							|  |  |  | 				if(!err) { | 
					
						
							|  |  |  | 					callback(data); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					console.log(err); | 
					
						
							|  |  |  | 					callback(null); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.createGravatarURLFromEmail = function(email) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:02:59 -05:00
										 |  |  | 		var forceDefault = '' | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 		if (!email) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:02:59 -05:00
										 |  |  | 			email = '0000', | 
					
						
							|  |  |  | 			forceDefault = '&forceDefault=y'; | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-07-05 16:02:59 -05:00
										 |  |  | 		var emailHash = crypto.createHash('md5').update(email.toLowerCase().trim()).digest('hex'); | 
					
						
							|  |  |  | 		// @todo: https asset support?
 | 
					
						
							|  |  |  | 		return 'http://www.gravatar.com/avatar/' + emailHash + '?default=identicon&s=128' + forceDefault; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.hashPassword = function(password, callback) { | 
					
						
							|  |  |  | 		if(!password) { | 
					
						
							|  |  |  | 			callback(password); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-10 11:51:15 -04:00
										 |  |  | 		bcrypt.genSalt(config.bcrypt_rounds, function(err, salt) { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 			bcrypt.hash(password, salt, function(err, hash) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 				callback(hash); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-26 12:32:30 -04:00
										 |  |  | 	User.search = function(username, callback) { | 
					
						
							| 
									
										
										
										
											2013-06-26 15:19:18 -04:00
										 |  |  | 		if(!username) { | 
					
						
							|  |  |  | 			callback([]); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-26 13:45:40 -04:00
										 |  |  | 		RDB.keys('username:*'+ username + '*:uid', function(err, keys) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if(!err) { | 
					
						
							| 
									
										
										
										
											2013-06-26 13:45:40 -04:00
										 |  |  | 				if(keys && keys.length) { | 
					
						
							|  |  |  | 					RDB.mget(keys, function(err, uids) { | 
					
						
							|  |  |  | 						User.getDataForUsers(uids, function(userdata) { | 
					
						
							|  |  |  | 							callback(userdata); | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2013-06-26 13:45:40 -04:00
										 |  |  | 					callback([]); | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-06-26 12:32:30 -04:00
										 |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-26 12:32:30 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 	User.onNewPostMade = function(uid, tid, pid, timestamp) { | 
					
						
							| 
									
										
										
										
											2013-07-10 11:51:15 -04:00
										 |  |  | 		User.addPostIdToUser(uid, pid); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 		User.incrementUserFieldBy(uid, 'postcount', 1); | 
					
						
							|  |  |  | 		User.setUserField(uid, 'lastposttime', timestamp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		User.sendPostNotificationToFollowers(uid, tid, pid); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.addPostIdToUser = function(uid, pid) { | 
					
						
							|  |  |  | 		RDB.lpush('uid:' + uid + ':posts', pid); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.addTopicIdToUser = function(uid, tid) { | 
					
						
							|  |  |  | 		RDB.lpush('uid:' + uid + ':topics', tid); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.getPostIds = function(uid, start, end, callback) { | 
					
						
							|  |  |  | 		RDB.lrange('uid:' + uid + ':posts', start, end, function(err, pids) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if(!err) { | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 				if(pids && pids.length) | 
					
						
							|  |  |  | 					callback(pids); | 
					
						
							|  |  |  | 				else | 
					
						
							|  |  |  | 					callback([]); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 				console.log(err); | 
					
						
							|  |  |  | 				callback([]); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	User.sendConfirmationEmail = function (email) { | 
					
						
							| 
									
										
										
										
											2013-05-28 11:50:09 -04:00
										 |  |  | 		if (global.config['email:host'] && global.config['email:port'] && global.config['email:from']) { | 
					
						
							|  |  |  | 			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
 | 
					
						
							|  |  |  | 			var expiry_time = 60*60*2,	// Expire after 2 hours
 | 
					
						
							|  |  |  | 				email_key = 'email:' + email + ':confirm', | 
					
						
							|  |  |  | 				confirm_key = 'confirm:' + confirm_code + ':email'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			RDB.set(email_key, confirm_code); | 
					
						
							|  |  |  | 			RDB.expire(email_key, expiry_time); | 
					
						
							|  |  |  | 			RDB.set(confirm_key, email); | 
					
						
							|  |  |  | 			RDB.expire(confirm_key, expiry_time); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				// 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 | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				] | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-28 11:50:09 -04:00
										 |  |  | 			emailjsServer.send(message, function(err, success) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				if (err) { | 
					
						
							| 
									
										
										
										
											2013-05-28 11:50:09 -04:00
										 |  |  | 					console.log(err); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-28 11:50:09 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 	User.follow = function(uid, followid, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 		RDB.sadd('following:' + uid, followid, function(err, data) { | 
					
						
							|  |  |  | 			if(!err) { | 
					
						
							|  |  |  | 				RDB.sadd('followers:' + followid, uid, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-07-10 11:20:31 -04:00
										 |  |  | 					if(!err) { | 
					
						
							|  |  |  | 						callback(true); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						console.log(err); | 
					
						
							|  |  |  | 						callback(false); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-05-14 16:44:43 -04:00
										 |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-07-10 11:20:31 -04:00
										 |  |  | 				callback(false); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-14 16:44:43 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 	User.unfollow = function(uid, unfollowid, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 		RDB.srem('following:' + uid, unfollowid, function(err, data){ | 
					
						
							|  |  |  | 			if(!err) { | 
					
						
							|  |  |  | 				RDB.srem('followers:' + unfollowid, uid, function(err, data){ | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 					callback(data); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.getFollowing = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 		RDB.smembers('following:' + uid, function(err, userIds) { | 
					
						
							|  |  |  | 			if(!err) { | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 				User.getDataForUsers(userIds, callback); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				console.log(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-14 16:44:43 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 	User.getFollowers = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 		RDB.smembers('followers:' + uid, function(err, userIds) { | 
					
						
							|  |  |  | 			if(!err) { | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 				User.getDataForUsers(userIds, callback); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-15 11:46:41 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 15:46:13 -04:00
										 |  |  | 	User.getFollowingCount = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 		RDB.smembers('following:' + uid, function(err, userIds) { | 
					
						
							|  |  |  | 			if(!err) { | 
					
						
							| 
									
										
										
										
											2013-06-17 15:46:13 -04:00
										 |  |  | 				callback(userIds.length); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-17 15:46:13 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 15:46:13 -04:00
										 |  |  | 	User.getFollowerCount = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 		RDB.smembers('followers:' + uid, function(err, userIds) { | 
					
						
							|  |  |  | 			// @note why are error-handling styles being mixed?
 | 
					
						
							|  |  |  | 			// either go with not-error-dosomething-else-dosomethingelse, or
 | 
					
						
							|  |  |  | 			// go with if-error-dosomething-return
 | 
					
						
							|  |  |  | 			// also why is console.log(err) being used when below we're using RDB.handle()?
 | 
					
						
							|  |  |  | 			if(!err) { | 
					
						
							| 
									
										
										
										
											2013-06-17 15:46:13 -04:00
										 |  |  | 				callback(userIds.length); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				console.log(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-17 15:46:13 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 19:08:59 -04:00
										 |  |  | 	User.getDataForUsers = function(uids, callback) { | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 		var returnData = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 19:08:59 -04:00
										 |  |  | 		if(!uids || !Array.isArray(uids) || uids.length === 0) { | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 			callback(returnData); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 19:11:27 -04:00
										 |  |  | 		function iterator(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-05 19:08:59 -04:00
										 |  |  | 			User.getUserData(uid, function(userData) { | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 				returnData.push(userData); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 19:08:59 -04:00
										 |  |  | 				callback(null); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-07-05 19:08:59 -04:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		async.each(uids, iterator, function(err) { | 
					
						
							|  |  |  | 			callback(returnData); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-18 10:01:42 -04:00
										 |  |  | 	User.sendPostNotificationToFollowers = function(uid, tid, pid) { | 
					
						
							|  |  |  | 		User.getUserField(uid, 'username', function(username) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			RDB.smembers('followers:' + uid, function(err, followers) { | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 				topics.getTopicField(tid, 'slug', function(slug) { | 
					
						
							| 
									
										
										
										
											2013-06-18 10:47:43 -04:00
										 |  |  | 					var message = username + ' made a new post'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-19 21:39:41 -04:00
										 |  |  | 					notifications.create(message, 5, global.config.url + 'topic/' + slug + '#' + pid, 'notification_'+ Date.now(), function(nid) { | 
					
						
							| 
									
										
										
										
											2013-06-18 10:47:43 -04:00
										 |  |  | 		 				notifications.push(nid, followers); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-06-18 10:01:42 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-15 11:46:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 	User.isFollowing = function(uid, theirid, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 		RDB.sismember('following:' + uid, theirid, function(err, data) { | 
					
						
							|  |  |  | 			if(!err) { | 
					
						
							| 
									
										
										
										
											2013-05-15 14:30:03 -04:00
										 |  |  | 				callback(data === 1); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-05-15 14:30:03 -04:00
										 |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-15 14:30:03 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-24 16:16:50 -04:00
										 |  |  | 	User.exists = function(userslug, callback) { | 
					
						
							|  |  |  | 		User.get_uid_by_userslug(userslug, function(exists) { | 
					
						
							| 
									
										
										
										
											2013-07-05 19:08:59 -04:00
										 |  |  | 			callback(!!exists); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	User.count = function(socket) { | 
					
						
							|  |  |  | 		RDB.get('usercount', function(err, count) { | 
					
						
							| 
									
										
										
										
											2013-07-05 23:46:32 -04:00
										 |  |  | 			RDB.handle(err); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			socket.emit('user.count', { count: count ? count : 0 }); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	User.latest = function(socket) { | 
					
						
							| 
									
										
										
										
											2013-07-05 23:46:32 -04:00
										 |  |  | 		RDB.lrange('userlist', 0, 0, function(err, uid) { | 
					
						
							|  |  |  | 			RDB.handle(err); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 23:46:32 -04:00
										 |  |  | 			User.getUserFields(uid, ['username', 'userslug'], function(userData) { | 
					
						
							|  |  |  | 				socket.emit('user.latest', {userslug: userData.userslug, username: userData.username}); | 
					
						
							| 
									
										
										
										
											2013-06-26 23:31:41 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.get_uid_by_username = function(username, callback) { | 
					
						
							|  |  |  | 		RDB.get('username:' + username + ':uid', function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				RDB.handle(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 			callback(data); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-24 14:33:53 -04:00
										 |  |  | 	User.get_uid_by_userslug = function(userslug, callback) { | 
					
						
							|  |  |  | 		RDB.get('userslug:' + userslug + ':uid', function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				RDB.handle(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-24 14:33:53 -04:00
										 |  |  | 			callback(data); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	User.get_usernames_by_uids = function(uids, callback) { | 
					
						
							|  |  |  | 		var usernames = []; | 
					
						
							| 
									
										
										
										
											2013-05-17 13:28:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 		if (!Array.isArray(uids)) { | 
					
						
							|  |  |  | 			return callback([]); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 19:11:27 -04:00
										 |  |  | 		function iterator(uid, callback) { | 
					
						
							|  |  |  | 			User.getUserField(uid, 'username', function(username) { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				usernames.push(username); | 
					
						
							| 
									
										
										
										
											2013-07-05 19:11:27 -04:00
										 |  |  | 				callback(null); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-07-05 19:11:27 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		async.each(uids, iterator, function(err) { | 
					
						
							|  |  |  | 			callback(usernames);			 | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-06-24 14:33:53 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.get_userslugs_by_uids = function(uids, callback) { | 
					
						
							|  |  |  | 		var userslugs = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 		if (!Array.isArray(uids)) { | 
					
						
							|  |  |  | 			return callback([]); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 18:19:55 -04:00
										 |  |  | 		function iterator(uid, callback) { | 
					
						
							|  |  |  | 			User.getUserField(uid, 'userslug', function(userslug) { | 
					
						
							|  |  |  | 				userslugs.push(userslug); | 
					
						
							|  |  |  | 				callback(null); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		async.each(uids, iterator, function(err) { | 
					
						
							|  |  |  | 			callback(userslugs);			 | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-06-24 14:33:53 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User.get_uid_by_email = function(email, callback) { | 
					
						
							|  |  |  | 		RDB.get('email:' + email + ':uid', function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				RDB.handle(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 			callback(data); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.get_uid_by_session = function(session, callback) { | 
					
						
							|  |  |  | 		RDB.get('sess:' + session + ':uid', function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				RDB.handle(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 			callback(data); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.get_uid_by_twitter_id = function(twid, callback) { | 
					
						
							| 
									
										
										
										
											2013-05-24 12:12:59 -04:00
										 |  |  | 		RDB.hget('twid:uid', twid, function(err, uid) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				RDB.handle(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 			callback(uid); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.get_uid_by_google_id = function(gplusid, callback) { | 
					
						
							| 
									
										
										
										
											2013-05-24 12:12:59 -04:00
										 |  |  | 		RDB.hget('gplusid:uid', gplusid, function(err, uid) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				RDB.handle(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 			callback(uid); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.get_uid_by_fbid = function(fbid, callback) { | 
					
						
							| 
									
										
										
										
											2013-05-24 12:12:59 -04:00
										 |  |  | 		RDB.hget('fbid:uid', fbid, function(err, uid) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				RDB.handle(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 			callback(uid); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.session_ping = function(sessionID, uid) { | 
					
						
							|  |  |  | 		// Start, replace, or extend a session
 | 
					
						
							|  |  |  | 		RDB.get('sess:' + sessionID, function(err, session) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				RDB.handle(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-09 05:48:55 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			var expiry = 60*60*24*14, // Login valid for two weeks
 | 
					
						
							|  |  |  | 				sess_key = 'sess:' + sessionID + ':uid', | 
					
						
							|  |  |  | 				uid_key = 'uid:' + uid + ':session'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			RDB.set(sess_key, uid); | 
					
						
							|  |  |  | 			RDB.expire(sess_key, expiry); | 
					
						
							|  |  |  | 			RDB.set(uid_key, sessionID); | 
					
						
							|  |  |  | 			RDB.expire(uid_key, expiry); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 12:49:39 -04:00
										 |  |  | 	User.isModerator = function(uid, cid, callback) { | 
					
						
							| 
									
										
										
										
											2013-05-16 15:55:17 -04:00
										 |  |  | 		RDB.sismember('cid:' + cid + ':moderators', uid, function(err, exists) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			// @todo handle error
 | 
					
						
							| 
									
										
										
										
											2013-05-17 21:14:58 -04:00
										 |  |  | 			callback(!!exists); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.isAdministrator = function(uid, callback) { | 
					
						
							|  |  |  | 		RDB.sismember('administrators', uid, function(err, exists) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			// @todo handle error
 | 
					
						
							| 
									
										
										
										
											2013-05-17 21:14:58 -04:00
										 |  |  | 			callback(!!exists); | 
					
						
							| 
									
										
										
										
											2013-05-16 12:49:39 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-20 14:45:38 -04:00
										 |  |  | 	User.makeAdministrator = function(uid, callback) { | 
					
						
							|  |  |  | 		RDB.sadd('administrators', uid, function(err, data){ | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if(!err) { | 
					
						
							| 
									
										
										
										
											2013-06-20 14:45:38 -04:00
										 |  |  | 				User.setUserField(uid, 'administrator', 1); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if(callback) { | 
					
						
							|  |  |  | 				// @todo address why we're only sending back a boolean in the callback and not an error if it occurred
 | 
					
						
							| 
									
										
										
										
											2013-06-20 14:45:38 -04:00
										 |  |  | 				callback(err === null); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-20 14:45:38 -04:00
										 |  |  | 		}); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.removeAdministrator = function(uid, callback) { | 
					
						
							|  |  |  | 		RDB.srem('administrators', uid, function(err, data){ | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if(!err) { | 
					
						
							| 
									
										
										
										
											2013-06-20 14:45:38 -04:00
										 |  |  | 				User.setUserField(uid, 'administrator', 0); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			if(callback) { | 
					
						
							|  |  |  | 				// @todo address why we're only sending back a boolean in the callback and not an error if it occurred
 | 
					
						
							| 
									
										
										
										
											2013-06-20 14:45:38 -04:00
										 |  |  | 				callback(err === null); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-20 14:45:38 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-06-20 14:45:38 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	User.reset = { | 
					
						
							|  |  |  | 		validate: function(socket, code, callback) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (typeof callback !== 'function') { | 
					
						
							|  |  |  | 				callback = null; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			RDB.get('reset:' + code + ':uid', function(err, uid) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					RDB.handle(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				if (uid !== null) { | 
					
						
							|  |  |  | 					RDB.get('reset:' + code + ':expiry', function(err, expiry) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 						if (err) { | 
					
						
							|  |  |  | 							RDB.handle(err); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 						if (expiry >= +Date.now()/1000|0) { | 
					
						
							|  |  |  | 							if (!callback) { | 
					
						
							|  |  |  | 								socket.emit('user:reset.valid', { valid: true }); | 
					
						
							|  |  |  | 							} else { | 
					
						
							|  |  |  | 								callback(true); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 						} else { | 
					
						
							|  |  |  | 							// Expired, delete from db
 | 
					
						
							|  |  |  | 							RDB.del('reset:' + code + ':uid'); | 
					
						
							|  |  |  | 							RDB.del('reset:' + code + ':expiry'); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 							if (!callback) { | 
					
						
							|  |  |  | 								socket.emit('user:reset.valid', { valid: false }); | 
					
						
							|  |  |  | 							} else { | 
					
						
							|  |  |  | 								callback(false); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 					if (!callback) { | 
					
						
							|  |  |  | 						socket.emit('user:reset.valid', { valid: false }); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						callback(false); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		send: function(socket, 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 | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						] | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 					emailjsServer.send(message, function(err, success) { | 
					
						
							|  |  |  | 						if (err === null) { | 
					
						
							|  |  |  | 							socket.emit('user.send_reset', { | 
					
						
							|  |  |  | 								status: "ok", | 
					
						
							|  |  |  | 								message: "code-sent", | 
					
						
							|  |  |  | 								email: email | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							socket.emit('user.send_reset', { | 
					
						
							|  |  |  | 								status: "error", | 
					
						
							|  |  |  | 								message: "send-failed" | 
					
						
							|  |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 							// @todo handle error properly
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 							throw new Error(err); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					socket.emit('user.send_reset', { | 
					
						
							|  |  |  | 						status: "error", | 
					
						
							|  |  |  | 						message: "invalid-email", | 
					
						
							|  |  |  | 						email: email | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		commit: function(socket, code, password) { | 
					
						
							|  |  |  | 			this.validate(code, function(validated) { | 
					
						
							|  |  |  | 				if (validated) { | 
					
						
							|  |  |  | 					RDB.get('reset:' + code + ':uid', function(err, uid) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 						if (err) { | 
					
						
							|  |  |  | 							RDB.handle(err); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						User.setUserField(uid, 'password', password); | 
					
						
							|  |  |  | 						RDB.del('reset:' + code + ':uid'); | 
					
						
							|  |  |  | 						RDB.del('reset:' + code + ':expiry'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						socket.emit('user:reset.commit', { status: 'ok' }); | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.email = { | 
					
						
							|  |  |  | 		exists: function(socket, email, callback) { | 
					
						
							|  |  |  | 			User.get_uid_by_email(email, function(exists) { | 
					
						
							|  |  |  | 				exists = !!exists; | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				if (typeof callback !== 'function') { | 
					
						
							|  |  |  | 					socket.emit('user.email.exists', { exists: exists }); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					callback(exists); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		confirm: function(code, callback) { | 
					
						
							|  |  |  | 			RDB.get('confirm:' + code + ':email', function(err, email) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					RDB.handle(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				if (email !== null) { | 
					
						
							|  |  |  | 					RDB.set('email:' + email + ':confirm', true); | 
					
						
							|  |  |  | 					RDB.del('confirm:' + code + ':email'); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 					callback({ status: 'ok' }); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 					callback({ status: 'not_ok' }); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-05-21 17:02:04 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.get_online_users = function(socket, uids) { | 
					
						
							|  |  |  | 		RDB.sismembers('users:online', uids, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 			RDB.handle(err); | 
					
						
							| 
									
										
										
										
											2013-05-21 17:02:04 -04:00
										 |  |  | 			socket.emit('api:user.get_online_users', data); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-21 17:02:04 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.go_online = function(uid) { | 
					
						
							| 
									
										
										
										
											2013-06-21 13:28:55 -04:00
										 |  |  | 		RDB.sadd('users:online', uid, function(err) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				RDB.handle(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-21 13:28:55 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-21 17:02:04 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.go_offline = function(uid) { | 
					
						
							| 
									
										
										
										
											2013-06-21 13:28:55 -04:00
										 |  |  | 		RDB.srem('users:online', uid, function(err) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				RDB.handle(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-21 13:28:55 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-05-21 17:02:04 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User.active = { | 
					
						
							|  |  |  | 		get_record : function(socket) { | 
					
						
							|  |  |  | 			RDB.mget(['global:active_user_record', 'global:active_user_record_date'], function(err, data) { | 
					
						
							|  |  |  | 				RDB.handle(err); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				socket.emit('api:user.active.get_record', { record: data[0], timestamp: data[1] }); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		get: function(callback) { | 
					
						
							|  |  |  | 			function user_record(total) { | 
					
						
							|  |  |  | 				RDB.get('global:active_user_record', function(err, record) { | 
					
						
							|  |  |  | 					RDB.handle(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (total > record) { | 
					
						
							|  |  |  | 						RDB.set('global:active_user_record', total); | 
					
						
							| 
									
										
										
										
											2013-06-19 21:39:41 -04:00
										 |  |  | 						RDB.set('global:active_user_record_date', Date.now()); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			RDB.keys('active:*', function(err, active) { | 
					
						
							|  |  |  | 				RDB.handle(err); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 				var returnObj = { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 						users: 0, | 
					
						
							|  |  |  | 						anon: 0, | 
					
						
							|  |  |  | 						uids: [] | 
					
						
							|  |  |  | 					}, | 
					
						
							|  |  |  | 					keys = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (active.length > 0) { | 
					
						
							|  |  |  | 					for(var a in active) { | 
					
						
							|  |  |  | 						keys.push('sess:' + active[a].split(':')[1] + ':uid'); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					RDB.mget(keys, function(err, uids) { | 
					
						
							|  |  |  | 						RDB.handle(err); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						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++; | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						user_record(returnObj.anon + returnObj.users); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						if (callback === undefined) { | 
					
						
							|  |  |  | 							io.sockets.emit('api:user.active.get', returnObj) | 
					
						
							|  |  |  | 						} else { | 
					
						
							|  |  |  | 							callback(returnObj); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					io.sockets.emit('api:user.active.get', returnObj) | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		register: function(sessionID) { | 
					
						
							|  |  |  | 			// Active state persists for 10 minutes
 | 
					
						
							|  |  |  | 			var active_session = 'active:' + sessionID; | 
					
						
							|  |  |  | 			RDB.set(active_session, ''); | 
					
						
							|  |  |  | 			RDB.expire(active_session, 60*10) | 
					
						
							|  |  |  | 			this.get(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User.notifications = { | 
					
						
							|  |  |  | 		get: function(uid, callback) { | 
					
						
							|  |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				unread: function(next) { | 
					
						
							| 
									
										
										
										
											2013-06-24 16:02:39 -04:00
										 |  |  | 					RDB.zrevrangebyscore('uid:' + uid + ':notifications:unread', 10, 0, function(err, nids) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 						// @todo handle err
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 						var unread = []; | 
					
						
							|  |  |  | 						if (nids && nids.length > 0) { | 
					
						
							|  |  |  | 							async.eachSeries(nids, function(nid, next) { | 
					
						
							|  |  |  | 								notifications.get(nid, function(notif_data) { | 
					
						
							|  |  |  | 									unread.push(notif_data); | 
					
						
							|  |  |  | 									next(); | 
					
						
							|  |  |  | 								}); | 
					
						
							|  |  |  | 							}, function(err) { | 
					
						
							|  |  |  | 								next(null, unread); | 
					
						
							|  |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 						} else { | 
					
						
							|  |  |  | 							next(null, unread); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				read: function(next) { | 
					
						
							| 
									
										
										
										
											2013-06-24 16:02:39 -04:00
										 |  |  | 					RDB.zrevrangebyscore('uid:' + uid + ':notifications:read', 10, 0, function(err, nids) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 						// @todo handle err
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 						var read = []; | 
					
						
							|  |  |  | 						if (nids && nids.length > 0) { | 
					
						
							|  |  |  | 							async.eachSeries(nids, function(nid, next) { | 
					
						
							|  |  |  | 								notifications.get(nid, function(notif_data) { | 
					
						
							|  |  |  | 									read.push(notif_data); | 
					
						
							|  |  |  | 									next(); | 
					
						
							|  |  |  | 								}); | 
					
						
							|  |  |  | 							}, function(err) { | 
					
						
							|  |  |  | 								next(null, read); | 
					
						
							|  |  |  | 							}); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 						} else { | 
					
						
							|  |  |  | 							next(null, read); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, function(err, notifications) { | 
					
						
							| 
									
										
										
										
											2013-06-24 16:02:39 -04:00
										 |  |  | 				// While maintaining score sorting, sort by time
 | 
					
						
							|  |  |  | 				notifications.read.sort(function(a, b) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 					if (a.score === b.score) { | 
					
						
							|  |  |  | 						return (a.datetime - b.datetime) > 0 ? -1 : 1; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-06-24 16:02:39 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 				notifications.unread.sort(function(a, b) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 					if (a.score === b.score) { | 
					
						
							|  |  |  | 						return (a.datetime - b.datetime) > 0 ? -1 : 1; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-06-24 16:02:39 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 				callback(notifications); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-23 12:54:57 -04:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-06-24 16:35:50 -04:00
										 |  |  | 		hasFlag: function(uid, callback) { | 
					
						
							|  |  |  | 			RDB.get('uid:1:notifications:flag', function(err, flag) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					RDB.handle(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-06-24 16:35:50 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				callback(flag === 1); | 
					
						
							| 
									
										
										
										
											2013-06-24 16:35:50 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		removeFlag: function(uid) { | 
					
						
							|  |  |  | 			RDB.del('uid:' + uid + ':notifications:flag', function(err) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				if (err) { | 
					
						
							|  |  |  | 					RDB.handle(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-23 12:54:57 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | }(exports)); |