| 
									
										
										
										
											2013-11-18 16:22:43 -05:00
										 |  |  | var bcrypt = require('bcrypt'), | 
					
						
							| 
									
										
										
										
											2013-08-29 13:40:04 -04:00
										 |  |  | 	async = require('async'), | 
					
						
							| 
									
										
										
										
											2013-11-18 16:22:43 -05:00
										 |  |  | 	emailjs = require('emailjs'), | 
					
						
							| 
									
										
										
										
											2013-09-17 15:38:50 -04:00
										 |  |  | 	nconf = require('nconf'), | 
					
						
							| 
									
										
										
										
											2013-11-18 16:22:43 -05:00
										 |  |  | 	winston = require('winston'), | 
					
						
							| 
									
										
										
										
											2013-11-30 13:13:18 -05:00
										 |  |  | 	gravatar = require('gravatar'), | 
					
						
							| 
									
										
										
										
											2013-11-22 14:08:02 -05:00
										 |  |  | 	check = require('validator').check, | 
					
						
							|  |  |  | 	sanitize = require('validator').sanitize, | 
					
						
							| 
									
										
										
										
											2013-12-26 21:10:26 -05:00
										 |  |  | 	S = require('string'), | 
					
						
							| 
									
										
										
										
											2013-12-01 16:21:19 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-18 16:22:43 -05:00
										 |  |  | 	utils = require('./../public/src/utils'), | 
					
						
							| 
									
										
										
										
											2013-12-01 10:52:50 -05:00
										 |  |  | 	plugins = require('./plugins'), | 
					
						
							| 
									
										
										
										
											2013-12-02 17:10:26 -05:00
										 |  |  | 	db = require('./database'), | 
					
						
							| 
									
										
										
										
											2013-11-18 16:22:43 -05:00
										 |  |  | 	meta = require('./meta'), | 
					
						
							|  |  |  | 	emailjsServer = emailjs.server.connect(meta.config['email:smtp:host'] || '127.0.0.1'), | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 	groups = require('./groups'), | 
					
						
							| 
									
										
										
										
											2013-11-18 16:22:43 -05:00
										 |  |  | 	notifications = require('./notifications'), | 
					
						
							| 
									
										
										
										
											2013-12-10 22:05:37 -05:00
										 |  |  | 	topics = require('./topics'), | 
					
						
							| 
									
										
										
										
											2013-12-21 19:42:07 -05:00
										 |  |  | 	events = require('./events'), | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-10 22:05:37 -05:00
										 |  |  | 	websockets = require('./websockets'); | 
					
						
							| 
									
										
										
										
											2013-11-22 14:08:02 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | (function(User) { | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	'use strict'; | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 	User.create = function(username, password, email, callback) { | 
					
						
							|  |  |  | 		var userslug = utils.slugify(username); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-22 00:24:37 -04:00
										 |  |  | 		username = username.trim(); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 		if (email !== undefined) { | 
					
						
							|  |  |  | 			email = email.trim(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-07-22 00:24:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		async.parallel([ | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 				if (email !== undefined) { | 
					
						
							|  |  |  | 					next(!utils.isEmailValid(email) ? new Error('Invalid Email!') : null); | 
					
						
							| 
									
										
										
										
											2013-11-04 23:30:30 +02:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 					next(); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-07-22 00:24:37 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							| 
									
										
										
										
											2013-11-07 12:17:23 -05:00
										 |  |  | 				next((!utils.isUserNameValid(username) || !userslug) ? new Error('Invalid Username!') : null); | 
					
						
							| 
									
										
										
										
											2013-07-22 00:24:37 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 				if (password !== undefined) { | 
					
						
							|  |  |  | 					next(!utils.isPasswordValid(password) ? new Error('Invalid Password!') : null); | 
					
						
							| 
									
										
										
										
											2013-11-04 23:30:30 +02:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 					next(); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-07-22 00:24:37 -04:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							|  |  |  | 				User.exists(userslug, function(exists) { | 
					
						
							|  |  |  | 					next(exists ? new Error('Username taken!') : null); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			function(next) { | 
					
						
							| 
									
										
										
										
											2013-07-24 12:04:32 -04:00
										 |  |  | 				if (email !== undefined) { | 
					
						
							| 
									
										
										
										
											2013-09-11 15:24:01 -04:00
										 |  |  | 					User.isEmailAvailable(email, function(err, available) { | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 						if (err) { | 
					
						
							| 
									
										
										
										
											2013-09-11 15:24:01 -04:00
										 |  |  | 							return next(err); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-07-24 12:04:32 -04:00
										 |  |  | 						next(!available ? new Error('Email taken!') : null); | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					next(); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-22 00:24:37 -04:00
										 |  |  | 		], function(err, results) { | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2013-12-02 17:10:26 -05:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 21:58:37 -05:00
										 |  |  | 			db.incrObjectField('global', 'nextUid', function(err, uid) { | 
					
						
							| 
									
										
										
										
											2013-12-02 17:10:26 -05:00
										 |  |  | 				if(err) { | 
					
						
							|  |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-22 00:24:37 -04:00
										 |  |  | 				var gravatar = User.createGravatarURLFromEmail(email); | 
					
						
							| 
									
										
										
										
											2013-08-12 14:32:56 -04:00
										 |  |  | 				var timestamp = Date.now(); | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 17:10:26 -05:00
										 |  |  | 				db.setObject('user:' + uid, { | 
					
						
							| 
									
										
										
										
											2013-07-22 00:24:37 -04:00
										 |  |  | 					'uid': uid, | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 					'username': username, | 
					
						
							|  |  |  | 					'userslug': userslug, | 
					
						
							| 
									
										
										
										
											2013-07-22 00:24:37 -04:00
										 |  |  | 					'fullname': '', | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 					'location': '', | 
					
						
							|  |  |  | 					'birthday': '', | 
					
						
							|  |  |  | 					'website': '', | 
					
						
							|  |  |  | 					'email': email || '', | 
					
						
							|  |  |  | 					'signature': '', | 
					
						
							|  |  |  | 					'joindate': timestamp, | 
					
						
							| 
									
										
										
										
											2013-07-22 00:24:37 -04:00
										 |  |  | 					'picture': gravatar, | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 					'gravatarpicture': gravatar, | 
					
						
							| 
									
										
										
										
											2013-07-22 00:24:37 -04:00
										 |  |  | 					'uploadedpicture': '', | 
					
						
							| 
									
										
										
										
											2013-08-16 13:03:56 -04:00
										 |  |  | 					'profileviews': 0, | 
					
						
							| 
									
										
										
										
											2013-07-22 00:24:37 -04:00
										 |  |  | 					'reputation': 0, | 
					
						
							|  |  |  | 					'postcount': 0, | 
					
						
							|  |  |  | 					'lastposttime': 0, | 
					
						
							| 
									
										
										
										
											2013-08-14 13:32:07 -04:00
										 |  |  | 					'banned': 0, | 
					
						
							| 
									
										
										
										
											2013-07-25 14:30:12 -04:00
										 |  |  | 					'showemail': 0 | 
					
						
							| 
									
										
										
										
											2013-07-22 00:24:37 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 17:10:26 -05:00
										 |  |  | 				db.setObjectField('username:uid', username, uid); | 
					
						
							|  |  |  | 				db.setObjectField('userslug:uid', userslug, uid); | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-24 12:04:32 -04:00
										 |  |  | 				if (email !== undefined) { | 
					
						
							| 
									
										
										
										
											2013-12-02 17:10:26 -05:00
										 |  |  | 					db.setObjectField('email:uid', email, uid); | 
					
						
							| 
									
										
										
										
											2013-12-03 13:36:44 -05:00
										 |  |  | 					User.sendConfirmationEmail(email); | 
					
						
							| 
									
										
										
										
											2013-07-24 12:04:32 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-01 10:52:50 -05:00
										 |  |  | 				plugins.fireHook('action:user.create', {uid: uid, username: username, email: email, picture: gravatar, timestamp: timestamp}); | 
					
						
							| 
									
										
										
										
											2013-12-02 21:33:35 -05:00
										 |  |  | 				db.incrObjectField('global', 'userCount'); | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 17:10:26 -05:00
										 |  |  | 				db.sortedSetAdd('users:joindate', timestamp, uid); | 
					
						
							|  |  |  | 				db.sortedSetAdd('users:postcount', 0, uid); | 
					
						
							|  |  |  | 				db.sortedSetAdd('users:reputation', 0, uid); | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-05 20:06:36 -05:00
										 |  |  | 				db.searchIndex('user', username, uid); | 
					
						
							| 
									
										
										
										
											2013-08-29 13:40:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-24 12:04:32 -04:00
										 |  |  | 				if (password !== undefined) { | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 					User.hashPassword(password, function(err, hash) { | 
					
						
							| 
									
										
										
										
											2013-07-22 00:24:37 -04:00
										 |  |  | 						User.setUserField(uid, 'password', hash); | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 						callback(null, uid); | 
					
						
							| 
									
										
										
										
											2013-07-22 00:24:37 -04:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					callback(null, uid); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-07-07 16:31:37 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 13:32:07 -04:00
										 |  |  | 	User.ban = function(uid, callback) { | 
					
						
							|  |  |  | 		User.setUserField(uid, 'banned', 1, callback); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-14 13:32:07 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User.unban = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 		User.setUserField(uid, 'banned', 0, callback); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	User.getUserField = function(uid, field, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 17:10:26 -05:00
										 |  |  | 		db.getObjectField('user:' + uid, field, callback); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	User.getUserFields = function(uid, fields, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 17:10:26 -05:00
										 |  |  | 		db.getObjectFields('user:' + uid, fields, callback); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User.getMultipleUserFields = function(uids, fields, callback) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 		if (uids.length === 0) { | 
					
						
							| 
									
										
										
										
											2013-09-11 12:49:54 -04:00
										 |  |  | 			return callback(null, []); | 
					
						
							| 
									
										
										
										
											2013-05-16 13:13:46 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 19:08:59 -04:00
										 |  |  | 		var returnData = []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 		var 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-09-11 12:49:54 -04:00
										 |  |  | 		function iterator(uid, next) { | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 			User.getUserFields(uid, fields, function(err, userData) { | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 				if (err) { | 
					
						
							| 
									
										
										
										
											2013-09-11 12:49:54 -04:00
										 |  |  | 					return next(err); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-07-05 19:08:59 -04:00
										 |  |  | 				returnData.push(userData); | 
					
						
							| 
									
										
										
										
											2013-09-11 12:49:54 -04:00
										 |  |  | 				next(null); | 
					
						
							| 
									
										
										
										
											2013-07-05 19:08:59 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-07-05 19:08:59 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-19 15:26:29 -04:00
										 |  |  | 		async.eachSeries(uuids, iterator, function(err) { | 
					
						
							| 
									
										
										
										
											2013-09-11 12:49:54 -04:00
										 |  |  | 			callback(err, returnData); | 
					
						
							| 
									
										
										
										
											2013-07-05 19:08:59 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User.getUserData = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 17:10:26 -05:00
										 |  |  | 		db.getObject('user:' + uid, function(err, data) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-09-11 13:02:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 			if (data && data.password) { | 
					
						
							|  |  |  | 				delete data.password; | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-09-11 13:02:55 -04:00
										 |  |  | 			callback(err, data); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 	User.updateProfile = function(uid, data, callback) { | 
					
						
							| 
									
										
										
										
											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-09-17 13:09:37 -04:00
										 |  |  | 		var returnData = { | 
					
						
							|  |  |  | 			success: false | 
					
						
							|  |  |  | 		}; | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 		function isSignatureValid(next) { | 
					
						
							| 
									
										
										
										
											2013-12-19 14:57:59 -05:00
										 |  |  | 			if (data.signature !== undefined && data.signature.length > meta.config.maximumSignatureLength) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 				next({ | 
					
						
							| 
									
										
										
										
											2013-12-19 14:57:59 -05:00
										 |  |  | 					error: 'Signature can\'t be longer than ' + meta.config.maximumSignatureLength + ' characters!' | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 				}, false); | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 				next(null, true); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 		function isEmailAvailable(next) { | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 			if (!data.email) { | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 				return next(null, true); | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 11:58:52 -04:00
										 |  |  | 			User.getUserField(uid, 'email', function(err, email) { | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 				if (email !== data.email) { | 
					
						
							|  |  |  | 					User.isEmailAvailable(data.email, function(err, available) { | 
					
						
							|  |  |  | 						if (err) { | 
					
						
							| 
									
										
										
										
											2013-09-11 15:24:01 -04:00
										 |  |  | 							return next(err, null); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 						if (!available) { | 
					
						
							|  |  |  | 							next({ | 
					
						
							|  |  |  | 								error: 'Email not available!' | 
					
						
							|  |  |  | 							}, false); | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 							next(null, true); | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 					next(null, true); | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-21 13:44:53 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 		async.series([isSignatureValid, isEmailAvailable], function(err, results) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2013-09-11 13:53:42 -04:00
										 |  |  | 				callback(err, returnData); | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 				async.each(fields, updateField, function(err) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 					if (err) { | 
					
						
							| 
									
										
										
										
											2013-09-11 13:53:42 -04:00
										 |  |  | 						callback(err, returnData); | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 					} else { | 
					
						
							|  |  |  | 						returnData.success = true; | 
					
						
							| 
									
										
										
										
											2013-09-11 13:53:42 -04:00
										 |  |  | 						callback(null, returnData); | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-11 13:53:42 -04:00
										 |  |  | 		function updateField(field, next) { | 
					
						
							| 
									
										
										
										
											2013-10-04 13:39:44 -04:00
										 |  |  | 			if (data[field] !== undefined && typeof data[field] === 'string') { | 
					
						
							| 
									
										
										
										
											2013-11-22 14:08:02 -05:00
										 |  |  | 				data[field] = data[field].trim(); | 
					
						
							|  |  |  | 				data[field] = sanitize(data[field]).escape(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 				if (field === 'email') { | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 					var gravatarpicture = User.createGravatarURLFromEmail(data[field]); | 
					
						
							|  |  |  | 					User.setUserField(uid, 'gravatarpicture', gravatarpicture); | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 					User.getUserFields(uid, ['email', 'picture', 'uploadedpicture'], function(err, userData) { | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 						if (err) { | 
					
						
							| 
									
										
										
										
											2013-09-11 13:53:42 -04:00
										 |  |  | 							return next(err); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 17:10:26 -05:00
										 |  |  | 						db.deleteObjectField('email:uid', userData.email); | 
					
						
							|  |  |  | 						db.setObjectField('email:uid', data.email, uid); | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 						User.setUserField(uid, field, data[field]); | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 						if (userData.picture !== userData.uploadedpicture) { | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 							returnData.picture = gravatarpicture; | 
					
						
							|  |  |  | 							User.setUserField(uid, 'picture', gravatarpicture); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						returnData.gravatarpicture = gravatarpicture; | 
					
						
							| 
									
										
										
										
											2013-12-21 19:42:07 -05:00
										 |  |  | 						events.logEmailChange(uid, userData.email, data.email); | 
					
						
							| 
									
										
										
										
											2013-09-11 13:53:42 -04:00
										 |  |  | 						next(null); | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 					return; | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 				} else if (field === 'signature') { | 
					
						
							| 
									
										
										
										
											2013-12-26 21:10:26 -05:00
										 |  |  | 					data[field] = S(data[field]).stripTags().s; | 
					
						
							| 
									
										
										
										
											2013-11-22 14:08:02 -05:00
										 |  |  | 				} else if (field === 'website') { | 
					
						
							|  |  |  | 					if(data[field].substr(0, 7) !== 'http://' && data[field].substr(0, 8) !== 'https://') { | 
					
						
							|  |  |  | 						data[field] = 'http://' + data[field]; | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 				User.setUserField(uid, field, data[field]); | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-11 13:53:42 -04:00
										 |  |  | 				next(null); | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-09-11 13:53:42 -04:00
										 |  |  | 				next(null); | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 	User.isEmailAvailable = function(email, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.isObjectField('email:uid', email, function(err, exists) { | 
					
						
							| 
									
										
										
										
											2013-09-12 13:37:03 +00:00
										 |  |  | 			callback(err, !exists); | 
					
						
							| 
									
										
										
										
											2013-07-08 14:14:18 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-30 18:30:43 -04:00
										 |  |  | 	User.changePassword = function(uid, data, callback) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 		if (!utils.isPasswordValid(data.newPassword)) { | 
					
						
							|  |  |  | 			return callback({ | 
					
						
							|  |  |  | 				error: 'Invalid password!' | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-07-08 12:10:21 -04:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-26 11:58:52 -04:00
										 |  |  | 		User.getUserField(uid, 'password', function(err, user_password) { | 
					
						
							| 
									
										
										
										
											2013-07-08 12:10:21 -04:00
										 |  |  | 			bcrypt.compare(data.currentPassword, user_password, function(err, res) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 				if (err) { | 
					
						
							| 
									
										
										
										
											2013-09-11 15:24:01 -04:00
										 |  |  | 					return callback(err); | 
					
						
							| 
									
										
										
										
											2013-07-08 12:10:21 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				if (res) { | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 					User.hashPassword(data.newPassword, function(err, hash) { | 
					
						
							| 
									
										
										
										
											2013-07-08 12:10:21 -04:00
										 |  |  | 						User.setUserField(uid, 'password', hash); | 
					
						
							| 
									
										
										
										
											2013-12-21 19:42:07 -05:00
										 |  |  | 						events.logPasswordChange(uid); | 
					
						
							| 
									
										
										
										
											2013-09-11 15:24:01 -04:00
										 |  |  | 						callback(null); | 
					
						
							| 
									
										
										
										
											2013-07-08 12:10:21 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 					callback({ | 
					
						
							|  |  |  | 						error: 'Your current password is not correct!' | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-07-08 12:10:21 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-08 12:10:21 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-14 13:32:07 -04:00
										 |  |  | 	User.setUserField = function(uid, field, value, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.setObjectField('user:' + uid, field, value, callback); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-27 15:02:09 -05:00
										 |  |  | 	User.setUserFields = function(uid, data, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.setObject('user:' + uid, data, callback); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-24 13:12:56 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-12 14:32:56 -04:00
										 |  |  | 	User.incrementUserFieldBy = function(uid, field, value, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.incrObjectFieldBy('user:' + uid, field, value, callback); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-12 14:32:56 -04:00
										 |  |  | 	User.decrementUserFieldBy = function(uid, field, value, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.incrObjectFieldBy('user:' + uid, field, -value, callback); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-19 10:59:24 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-12 14:32:56 -04:00
										 |  |  | 	User.getUsers = function(set, start, stop, callback) { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		var data = []; | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.getSortedSetRevRange(set, start, stop, function(err, uids) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2013-08-12 14:32:56 -04:00
										 |  |  | 				return callback(err, null); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-30 16:24:00 -04:00
										 |  |  | 			function iterator(uid, callback) { | 
					
						
							|  |  |  | 				User.getUserData(uid, function(err, userData) { | 
					
						
							| 
									
										
										
										
											2013-11-27 12:47:00 -05:00
										 |  |  | 					User.isAdministrator(uid, function(err, isAdmin) { | 
					
						
							| 
									
										
										
										
											2013-09-30 16:24:00 -04:00
										 |  |  | 						if (userData) { | 
					
						
							|  |  |  | 							userData.administrator = isAdmin?"1":"0"; | 
					
						
							|  |  |  | 							data.push(userData); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 						callback(null); | 
					
						
							| 
									
										
										
										
											2013-09-30 16:19:20 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-09-30 16:24:00 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			async.eachSeries(uids, iterator, function(err) { | 
					
						
							|  |  |  | 				callback(err, data); | 
					
						
							| 
									
										
										
										
											2013-07-05 23:46:32 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User.createGravatarURLFromEmail = function(email) { | 
					
						
							| 
									
										
										
										
											2013-07-12 16:23:35 -04:00
										 |  |  | 		var options = { | 
					
						
							|  |  |  | 			size: '128', | 
					
						
							|  |  |  | 			default: 'identicon', | 
					
						
							|  |  |  | 			rating: 'pg' | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		https = nconf.get('https'); | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 		if (!email) { | 
					
						
							| 
									
										
										
										
											2013-07-12 16:23:35 -04:00
										 |  |  | 			email = ''; | 
					
						
							|  |  |  | 			options.forcedefault = 'y'; | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-07-12 16:23:35 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-30 13:13:18 -05:00
										 |  |  | 		return gravatar.url(email, options, https); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User.hashPassword = function(password, callback) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 		if (!password) { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 			callback(password); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 		bcrypt.genSalt(nconf.get('bcrypt_rounds'), function(err, salt) { | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 			bcrypt.hash(password, salt, callback); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-29 13:40:04 -04:00
										 |  |  | 	User.reIndexAll = function(callback) { | 
					
						
							|  |  |  | 		User.getUsers('users:joindate', 0, -1, function(err, usersData) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2013-08-29 13:40:04 -04:00
										 |  |  | 				return callback(err, null); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			function reIndexUser(uid, username) { | 
					
						
							| 
									
										
										
										
											2013-12-05 20:06:36 -05:00
										 |  |  | 				db.searchRemove('user', uid, function() { | 
					
						
							|  |  |  | 					db.searchIndex('user', username, uid); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-08-29 13:40:04 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			for (var i = 0; i < usersData.length; ++i) { | 
					
						
							| 
									
										
										
										
											2013-08-29 13:40:04 -04:00
										 |  |  | 				reIndexUser(usersData[i].uid, usersData[i].username); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			callback(null, 1); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-08-29 13:40:04 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-26 12:32:30 -04:00
										 |  |  | 	User.search = function(username, callback) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 		if (!username) { | 
					
						
							| 
									
										
										
										
											2013-06-26 15:19:18 -04:00
										 |  |  | 			callback([]); | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-12-05 20:24:25 -05:00
										 |  |  | 		db.search('user', username, function(err, uids) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2013-06-26 12:32:30 -04:00
										 |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-08-29 13:40:04 -04:00
										 |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (uids && uids.length) { | 
					
						
							| 
									
										
										
										
											2013-08-29 13:40:04 -04:00
										 |  |  | 				User.getDataForUsers(uids, function(userdata) { | 
					
						
							|  |  |  | 					callback(userdata); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				callback([]); | 
					
						
							| 
									
										
										
										
											2013-07-05 15:55:58 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-26 12:32:30 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02: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-08-12 14:32:56 -04:00
										 |  |  | 		User.incrementUserFieldBy(uid, 'postcount', 1, function(err, newpostcount) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 			db.sortedSetAdd('users:postcount', newpostcount, uid); | 
					
						
							| 
									
										
										
										
											2013-08-12 14:32:56 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 		User.setUserField(uid, 'lastposttime', timestamp); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		User.sendPostNotificationToFollowers(uid, tid, pid); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User.addPostIdToUser = function(uid, pid) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.listPrepend('uid:' + uid + ':posts', pid); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User.addTopicIdToUser = function(uid, tid) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.listPrepend('uid:' + uid + ':topics', tid); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 	User.getPostIds = function(uid, start, stop, callback) { | 
					
						
							|  |  |  | 		db.getListRange('uid:' + uid + ':posts', start, stop, function(err, pids) { | 
					
						
							| 
									
										
										
										
											2013-12-14 16:29:33 -05:00
										 |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			if (pids && pids.length) { | 
					
						
							|  |  |  | 				callback(null, pids); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2013-12-14 16:29:33 -05:00
										 |  |  | 				callback(null, []); | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-02 16:24:13 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 	User.follow = function(uid, followid, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.setAdd('following:' + uid, followid, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (!err) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 				db.setAdd('followers:' + followid, uid, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 					if (!err) { | 
					
						
							| 
									
										
										
										
											2013-07-10 11:20:31 -04:00
										 |  |  | 						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-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-14 16:44:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 	User.unfollow = function(uid, unfollowid, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.setRemove('following:' + uid, unfollowid, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (!err) { | 
					
						
							| 
									
										
										
										
											2013-12-02 21:20:55 -05:00
										 |  |  | 				db.setRemove('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
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User.getFollowing = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.getSetMembers('following:' + uid, function(err, userIds) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			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-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 	User.getFollowers = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.getSetMembers('followers:' + uid, function(err, userIds) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			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-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 15:46:13 -04:00
										 |  |  | 	User.getFollowingCount = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.getSetMembers('following:' + uid, function(err, userIds) { | 
					
						
							| 
									
										
										
										
											2013-10-11 14:29:34 -04:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-10-11 14:29:34 -04:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				userIds = userIds.filter(function(value) { | 
					
						
							| 
									
										
										
										
											2013-12-05 21:29:51 -05:00
										 |  |  | 					return parseInt(value, 10) !== 0; | 
					
						
							| 
									
										
										
										
											2013-10-11 14:29:34 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 				callback(userIds.length); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-17 15:46:13 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 15:46:13 -04:00
										 |  |  | 	User.getFollowerCount = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.getSetMembers('followers:' + uid, function(err, userIds) { | 
					
						
							| 
									
										
										
										
											2013-10-11 14:29:34 -04:00
										 |  |  | 			if(err) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				console.log(err); | 
					
						
							| 
									
										
										
										
											2013-10-11 14:29:34 -04:00
										 |  |  | 			} else { | 
					
						
							|  |  |  | 				userIds = userIds.filter(function(value) { | 
					
						
							| 
									
										
										
										
											2013-12-05 21:29:51 -05:00
										 |  |  | 					return parseInt(value, 10) !== 0; | 
					
						
							| 
									
										
										
										
											2013-10-11 14:29:34 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 				callback(userIds.length); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-06-17 15:46:13 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02: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-09-17 13:09:37 -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-12-05 13:11:27 -05:00
										 |  |  | 			if(parseInt(uid, 10) === 0) { | 
					
						
							| 
									
										
										
										
											2013-10-11 14:29:34 -04:00
										 |  |  | 				return callback(null); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-10-11 14:29:34 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-11 13:02:55 -04:00
										 |  |  | 			User.getUserData(uid, function(err, 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-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-19 15:26:29 -04:00
										 |  |  | 		async.eachSeries(uids, iterator, function(err) { | 
					
						
							| 
									
										
										
										
											2013-07-05 19:08:59 -04:00
										 |  |  | 			callback(returnData); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-18 10:01:42 -04:00
										 |  |  | 	User.sendPostNotificationToFollowers = function(uid, tid, pid) { | 
					
						
							| 
									
										
										
										
											2013-08-26 11:58:52 -04:00
										 |  |  | 		User.getUserField(uid, 'username', function(err, username) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 			db.getSetMembers('followers:' + uid, function(err, followers) { | 
					
						
							| 
									
										
										
										
											2013-08-11 16:12:20 -04:00
										 |  |  | 				topics.getTopicField(tid, 'slug', function(err, slug) { | 
					
						
							| 
									
										
										
										
											2013-08-27 13:04:18 -04:00
										 |  |  | 					var message = '<strong>' + username + '</strong> made a new post'; | 
					
						
							| 
									
										
										
										
											2013-06-18 10:47:43 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-03 20:35:16 -04:00
										 |  |  | 					notifications.create(message, nconf.get('relative_path') + '/topic/' + slug + '#' + pid, 'topic:' + tid, function(nid) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 						notifications.push(nid, followers); | 
					
						
							| 
									
										
										
										
											2013-06-18 10:47:43 -04:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-06-18 10:01:42 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-15 11:46:41 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-17 15:25:09 -04:00
										 |  |  | 	User.isFollowing = function(uid, theirid, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-04 15:11:17 -05:00
										 |  |  | 		db.isSetMember('following:' + uid, theirid, function(err, isMember) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			if (!err) { | 
					
						
							| 
									
										
										
										
											2013-12-04 15:11:17 -05:00
										 |  |  | 				callback(isMember); | 
					
						
							| 
									
										
										
										
											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-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-15 14:30:03 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-06-24 16:16:50 -04:00
										 |  |  | 	User.exists = function(userslug, callback) { | 
					
						
							| 
									
										
										
										
											2013-10-30 18:31:36 -04:00
										 |  |  | 		User.getUidByUserslug(userslug, function(err, exists) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			callback( !! exists); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	User.count = function(socket) { | 
					
						
							| 
									
										
										
										
											2013-12-02 21:33:35 -05:00
										 |  |  | 		db.getObjectField('global', 'userCount', function(err, count) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-07-05 23:46:32 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			socket.emit('user.count', { | 
					
						
							|  |  |  | 				count: count ? count : 0 | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 18:31:36 -04:00
										 |  |  | 	User.getUidByUsername = function(username, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.getObjectField('username:uid', username, callback); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 18:31:36 -04:00
										 |  |  | 	User.getUidByUserslug = function(userslug, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.getObjectField('userslug:uid', userslug, callback); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-06-24 14:33:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 18:31:36 -04:00
										 |  |  | 	User.getUsernamesByUids = function(uids, callback) { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		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) { | 
					
						
							| 
									
										
										
										
											2013-08-26 11:58:52 -04:00
										 |  |  | 			User.getUserField(uid, 'username', function(err, 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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-19 15:26:29 -04:00
										 |  |  | 		async.eachSeries(uids, iterator, function(err) { | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 			callback(usernames); | 
					
						
							| 
									
										
										
										
											2013-07-05 19:11:27 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-06-24 14:33:53 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 18:31:36 -04:00
										 |  |  | 	User.getUserSlugsByUids = function(uids, callback) { | 
					
						
							| 
									
										
										
										
											2013-06-24 14:33:53 -04:00
										 |  |  | 		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) { | 
					
						
							| 
									
										
										
										
											2013-08-26 11:58:52 -04:00
										 |  |  | 			User.getUserField(uid, 'userslug', function(err, userslug) { | 
					
						
							| 
									
										
										
										
											2013-07-05 18:19:55 -04:00
										 |  |  | 				userslugs.push(userslug); | 
					
						
							|  |  |  | 				callback(null); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-19 15:26:29 -04:00
										 |  |  | 		async.eachSeries(uids, iterator, function(err) { | 
					
						
							| 
									
										
										
										
											2013-08-23 13:14:36 -04:00
										 |  |  | 			callback(userslugs); | 
					
						
							| 
									
										
										
										
											2013-07-05 18:19:55 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 18:31:36 -04:00
										 |  |  | 	User.getUidByEmail = function(email, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.getObjectField('email:uid', email, function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 			callback(null, data); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 18:31:36 -04:00
										 |  |  | 	User.getUidByTwitterId = function(twid, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.getObjectField('twid:uid', twid, function(err, uid) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 			callback(null, uid); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 18:31:36 -04:00
										 |  |  | 	User.getUidByGoogleId = function(gplusid, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.getObjectField('gplusid:uid', gplusid, function(err, uid) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 			callback(null, uid); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 18:31:36 -04:00
										 |  |  | 	User.getUidByFbid = function(fbid, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.getObjectField('fbid:uid', fbid, function(err, uid) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (err) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 				return callback(err); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 			callback(null, uid); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:37:45 -05:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-16 12:49:39 -04:00
										 |  |  | 	User.isModerator = function(uid, cid, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		db.isSetMember('cid:' + cid + ':moderators', uid, function(err, exists) { | 
					
						
							|  |  |  | 			if(err) { | 
					
						
							|  |  |  | 				return calback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-12-04 15:11:17 -05:00
										 |  |  | 			callback(err, exists); | 
					
						
							| 
									
										
										
										
											2013-05-17 21:14:58 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-17 21:14:58 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	User.isAdministrator = function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 		groups.getGidFromName('Administrators', function(err, gid) { | 
					
						
							|  |  |  | 			groups.isMember(uid, gid, function(err, isAdmin) { | 
					
						
							| 
									
										
										
										
											2013-12-04 15:11:17 -05:00
										 |  |  | 				callback(err, isAdmin); | 
					
						
							| 
									
										
										
										
											2013-09-07 15:49:23 -04:00
										 |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-06-20 14:45:38 -04:00
										 |  |  | 		}); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02: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-08-23 13:14:36 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 			if (typeof callback !== 'function') { | 
					
						
							|  |  |  | 				callback = null; | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 			db.getObjectField('reset:uid', code, function(err, uid) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				if (err) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 					return callback(false); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				if (uid !== null) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 					db.getObjectField('reset:expiry', code, function(err, expiry) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 						if (err) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 							return callback(false); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 						if (expiry >= +Date.now() / 1000 | 0) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 							if (!callback) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 								socket.emit('user:reset.valid', { | 
					
						
							|  |  |  | 									valid: true | 
					
						
							|  |  |  | 								}); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 							} else { | 
					
						
							|  |  |  | 								callback(true); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 						} else { | 
					
						
							|  |  |  | 							// Expired, delete from db
 | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 							db.deleteObjectField('reset:uid', code); | 
					
						
							|  |  |  | 							db.deleteObjectField('reset:expiry', code); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 							if (!callback) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 								socket.emit('user:reset.valid', { | 
					
						
							|  |  |  | 									valid: false | 
					
						
							|  |  |  | 								}); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 							} else { | 
					
						
							|  |  |  | 								callback(false); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 					if (!callback) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 						socket.emit('user:reset.valid', { | 
					
						
							|  |  |  | 							valid: false | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 					} else { | 
					
						
							|  |  |  | 						callback(false); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		send: function(socket, email) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 			User.getUidByEmail(email, function(err, uid) { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				if (uid !== null) { | 
					
						
							|  |  |  | 					// Generate a new reset code
 | 
					
						
							|  |  |  | 					var reset_code = utils.generateUUID(); | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 					db.setObjectField('reset:uid', reset_code, uid); | 
					
						
							|  |  |  | 					db.setobjectField('reset:expiry', reset_code, (60 * 60) + new Date() / 1000 | 0); // Active for one hour
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-08-19 13:31:04 -04:00
										 |  |  | 					var reset_link = nconf.get('url') + 'reset/' + reset_code, | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 						reset_email = global.templates['emails/reset'].parse({ | 
					
						
							|  |  |  | 							'RESET_LINK': reset_link | 
					
						
							|  |  |  | 						}), | 
					
						
							|  |  |  | 						reset_email_plaintext = global.templates['emails/reset_plaintext'].parse({ | 
					
						
							|  |  |  | 							'RESET_LINK': reset_link | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 					var message = emailjs.message.create({ | 
					
						
							|  |  |  | 						text: reset_email_plaintext, | 
					
						
							| 
									
										
										
										
											2013-09-26 13:31:38 -04:00
										 |  |  | 						from: meta.config['email:from'] ? meta.config['email:from'] : 'localhost@example.org', | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 						to: email, | 
					
						
							|  |  |  | 						subject: 'Password Reset Requested', | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 						attachment: [{ | 
					
						
							|  |  |  | 							data: reset_email, | 
					
						
							|  |  |  | 							alternative: true | 
					
						
							|  |  |  | 						}] | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											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-09-26 12:29:35 -04:00
										 |  |  | 							winston.err(err); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 						} | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					socket.emit('user.send_reset', { | 
					
						
							|  |  |  | 						status: "error", | 
					
						
							|  |  |  | 						message: "invalid-email", | 
					
						
							|  |  |  | 						email: email | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		commit: function(socket, code, password) { | 
					
						
							| 
									
										
										
										
											2013-08-19 13:31:04 -04:00
										 |  |  | 			this.validate(socket, code, function(validated) { | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				if (validated) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 					db.getObjectField('reset:uid', code, function(err, uid) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 						if (err) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 							return; | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-06 22:22:42 -04:00
										 |  |  | 						User.hashPassword(password, function(err, hash) { | 
					
						
							| 
									
										
										
										
											2013-08-19 13:31:04 -04:00
										 |  |  | 							User.setUserField(uid, 'password', hash); | 
					
						
							| 
									
										
										
										
											2013-12-21 19:42:07 -05:00
										 |  |  | 							events.logPasswordReset(uid); | 
					
						
							| 
									
										
										
										
											2013-08-19 13:31:04 -04:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 						db.deleteObjectField('reset:uid', code); | 
					
						
							|  |  |  | 						db.deleteObjectField('reset:expiry', code); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 						socket.emit('user:reset.commit', { | 
					
						
							|  |  |  | 							status: 'ok' | 
					
						
							|  |  |  | 						}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 	User.sendConfirmationEmail = function(email) { | 
					
						
							|  |  |  | 		if (meta.config['email:smtp:host'] && meta.config['email:smtp:port'] && meta.config['email:from']) { | 
					
						
							|  |  |  | 			var confirm_code = utils.generateUUID(), | 
					
						
							|  |  |  | 				confirm_link = nconf.get('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 = Date.now() / 1000 + 60 * 60 * 2; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			db.setObjectField('email:confirm', email, confirm_code); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			db.setObjectField('confirm:email', confirm_code, email); | 
					
						
							|  |  |  | 			db.setObjectField('confirm:email', confirm_code + ':expire', expiry_time); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			// Send intro email w/ confirm code
 | 
					
						
							|  |  |  | 			var message = emailjs.message.create({ | 
					
						
							|  |  |  | 				text: confirm_email_plaintext, | 
					
						
							|  |  |  | 				from: meta.config['email:from'] || 'localhost@example.org', | 
					
						
							|  |  |  | 				to: email, | 
					
						
							|  |  |  | 				subject: '[NodeBB] Registration Email Verification', | 
					
						
							|  |  |  | 				attachment: [{ | 
					
						
							|  |  |  | 					data: confirm_email, | 
					
						
							|  |  |  | 					alternative: true | 
					
						
							|  |  |  | 				}] | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			emailjsServer.send(message, function(err, success) { | 
					
						
							|  |  |  | 				if (err) { | 
					
						
							|  |  |  | 					console.log(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-10 22:05:37 -05:00
										 |  |  | 	User.pushNotifCount = function(uid) { | 
					
						
							|  |  |  | 		User.notifications.getUnreadCount(uid, function(err, count) { | 
					
						
							|  |  |  | 			if (!err) { | 
					
						
							|  |  |  | 				websockets.in('uid_' + uid).emit('event:notifications.updateCount', count); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				winston.warn('[User.pushNotifCount] Count not retrieve unread notifications count to push to uid ' + uid + '\'s client(s)'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 	User.email = { | 
					
						
							|  |  |  | 		exists: function(socket, email, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 			User.getUidByEmail(email, function(err, exists) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 				exists = !! exists; | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				if (typeof callback !== 'function') { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 					socket.emit('user.email.exists', { | 
					
						
							|  |  |  | 						exists: exists | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					callback(exists); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		confirm: function(code, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 			db.getObjectFields('confirm:email', [code, code + ':expire'], function(err, data) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				if (err) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 					return callback({ | 
					
						
							|  |  |  | 						status:'error' | 
					
						
							|  |  |  | 					}); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 				var email = data.email; | 
					
						
							|  |  |  | 				var expiry = data[code + ':expire']; | 
					
						
							|  |  |  | 				if (parseInt(expiry, 10) >= Date.now() / 1000) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					db.deleteObjectField('confirm:email', code); | 
					
						
							|  |  |  | 					db.deleteObjectField('confirm:email', code + ':expire'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					return callback({ | 
					
						
							|  |  |  | 						status: 'expired' | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 				if (email !== null) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 					db.setObjectField('email:confirm', email, true); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					db.deleteObjectField('confirm:email', code); | 
					
						
							|  |  |  | 					db.deleteObjectField('confirm:email', code + ':expire'); | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 					callback({ | 
					
						
							|  |  |  | 						status: 'ok' | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 					callback({ | 
					
						
							|  |  |  | 						status: 'not_ok' | 
					
						
							|  |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-05-09 01:04:15 -04:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-05-21 17:02:04 -04:00
										 |  |  | 	}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 	User.notifications = { | 
					
						
							|  |  |  | 		get: function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-09-17 13:09:37 -04:00
										 |  |  | 			var maxNotifs = 15; | 
					
						
							| 
									
										
										
										
											2013-07-22 15:31:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 			async.parallel({ | 
					
						
							|  |  |  | 				unread: function(next) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 					db.getSortedSetRevRange('uid:' + uid + ':notifications:unread', 0, 10, function(err, nids) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 						// @todo handle err
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 						var unread = []; | 
					
						
							| 
									
										
										
										
											2013-07-22 15:31:28 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						// Cap the number of notifications returned
 | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 						if (nids.length > maxNotifs) { | 
					
						
							|  |  |  | 							nids.length = maxNotifs; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-07-22 15:31:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 						if (nids && nids.length > 0) { | 
					
						
							|  |  |  | 							async.eachSeries(nids, function(nid, next) { | 
					
						
							| 
									
										
										
										
											2013-10-22 15:06:07 -04:00
										 |  |  | 								notifications.get(nid, uid, function(notif_data) { | 
					
						
							| 
									
										
										
										
											2013-10-28 14:53:41 -04:00
										 |  |  | 									// If the notification could not be found, silently drop it
 | 
					
						
							|  |  |  | 									if (notif_data) { | 
					
						
							|  |  |  | 										unread.push(notif_data); | 
					
						
							|  |  |  | 									} else { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 										db.sortedSetRemove('uid:' + uid + ':notifications:unread', nid); | 
					
						
							| 
									
										
										
										
											2013-10-28 14:53:41 -04:00
										 |  |  | 									} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 									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-12-02 19:40:11 -05:00
										 |  |  | 					db.getSortedSetRevRange('uid:' + uid + ':notifications:read', 0, 10, function(err, nids) { | 
					
						
							| 
									
										
										
										
											2013-07-05 16:25:51 -05:00
										 |  |  | 						// @todo handle err
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 						var read = []; | 
					
						
							| 
									
										
										
										
											2013-07-22 15:31:28 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 						// Cap the number of notifications returned
 | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 						if (nids.length > maxNotifs) { | 
					
						
							|  |  |  | 							nids.length = maxNotifs; | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-07-22 15:31:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 						if (nids && nids.length > 0) { | 
					
						
							|  |  |  | 							async.eachSeries(nids, function(nid, next) { | 
					
						
							| 
									
										
										
										
											2013-10-22 15:06:07 -04:00
										 |  |  | 								notifications.get(nid, uid, function(notif_data) { | 
					
						
							| 
									
										
										
										
											2013-10-28 14:53:41 -04:00
										 |  |  | 									// If the notification could not be found, silently drop it
 | 
					
						
							|  |  |  | 									if (notif_data) { | 
					
						
							|  |  |  | 										read.push(notif_data); | 
					
						
							|  |  |  | 									} else { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 										db.sortedSetRemove('uid:' + uid + ':notifications:read', nid); | 
					
						
							| 
									
										
										
										
											2013-10-28 14:53:41 -04:00
										 |  |  | 									} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 									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-07-22 15:31:28 -04:00
										 |  |  | 				// Limit the number of notifications to `maxNotifs`, prioritising unread notifications
 | 
					
						
							|  |  |  | 				if (notifications.read.length + notifications.unread.length > maxNotifs) { | 
					
						
							|  |  |  | 					notifications.read.length = maxNotifs - notifications.unread.length; | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 				callback(notifications); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-23 12:54:57 -04:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-10-22 14:35:20 -04:00
										 |  |  | 		getAll: function(uid, limit, before, callback) { | 
					
						
							|  |  |  | 			var	now = new Date(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 			if (!limit || parseInt(limit,10) <= 0) { | 
					
						
							|  |  |  | 				limit = 25; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			if (before) { | 
					
						
							|  |  |  | 				before = new Date(parseInt(before, 10)); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-10-22 14:35:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 16:31:05 -05:00
										 |  |  | 			var args1 = ['uid:' + uid + ':notifications:read', before ? before.getTime(): now.getTime(), -Infinity, 'LIMIT', 0, limit]; | 
					
						
							|  |  |  | 			var args2 = ['uid:' + uid + ':notifications:unread', before ? before.getTime(): now.getTime(), -Infinity, 'LIMIT', 0, limit]; | 
					
						
							| 
									
										
										
										
											2013-10-22 14:35:20 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-04 16:31:05 -05:00
										 |  |  | 			db.getSortedSetRevRangeByScore(args1, function(err, results1) { | 
					
						
							|  |  |  | 				db.getSortedSetRevRangeByScore(args2, function(err, results2) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					var nids = results1.concat(results2); | 
					
						
							| 
									
										
										
										
											2013-10-22 14:35:20 -04:00
										 |  |  | 					async.map(nids, function(nid, next) { | 
					
						
							| 
									
										
										
										
											2013-10-22 15:06:07 -04:00
										 |  |  | 						notifications.get(nid, uid, function(notif_data) { | 
					
						
							| 
									
										
										
										
											2013-10-22 14:35:20 -04:00
										 |  |  | 							next(null, notif_data); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					}, function(err, notifs) { | 
					
						
							| 
									
										
										
										
											2013-11-18 15:09:34 -05:00
										 |  |  | 						notifs = notifs.filter(function(notif) { | 
					
						
							| 
									
										
										
										
											2013-11-18 15:56:12 -05:00
										 |  |  | 							return notif !== null; | 
					
						
							| 
									
										
										
										
											2013-11-18 15:09:34 -05:00
										 |  |  | 						}).sort(function(a, b) { | 
					
						
							| 
									
										
										
										
											2013-10-22 14:35:20 -04:00
										 |  |  | 							return parseInt(b.datetime, 10) - parseInt(a.datetime, 10); | 
					
						
							|  |  |  | 						}).map(function(notif) { | 
					
						
							|  |  |  | 							notif.datetimeISO = new Date(parseInt(notif.datetime, 10)).toISOString(); | 
					
						
							| 
									
										
										
										
											2013-10-22 15:06:07 -04:00
										 |  |  | 							notif.readClass = !notif.read ? 'unread' : ''; | 
					
						
							| 
									
										
										
										
											2013-10-22 14:35:20 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 							return notif; | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						callback(err, notifs); | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 					}); | 
					
						
							| 
									
										
										
										
											2013-10-22 14:35:20 -04:00
										 |  |  | 				}); | 
					
						
							| 
									
										
										
										
											2013-12-04 16:31:05 -05:00
										 |  |  | 			}); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-22 14:35:20 -04:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2013-07-24 15:19:26 -04:00
										 |  |  | 		getUnreadCount: function(uid, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-05 12:17:01 -05:00
										 |  |  | 			db.sortedSetCount('uid:' + uid + ':notifications:unread', -Infinity, Infinity, callback); | 
					
						
							| 
									
										
										
										
											2013-09-11 15:41:20 -04:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		getUnreadByUniqueId: function(uid, uniqueId, callback) { | 
					
						
							| 
									
										
										
										
											2013-12-02 19:40:11 -05:00
										 |  |  | 			db.getSortedSetRange('uid:' + uid + ':notifications:unread', 0, -1, function(err, nids) { | 
					
						
							| 
									
										
										
										
											2013-12-04 18:58:20 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-11 15:41:20 -04:00
										 |  |  | 				async.filter(nids, function(nid, next) { | 
					
						
							| 
									
										
										
										
											2013-10-22 15:06:07 -04:00
										 |  |  | 					notifications.get(nid, uid, function(notifObj) { | 
					
						
							| 
									
										
										
										
											2013-12-04 18:58:20 -05:00
										 |  |  | 						if(!notifObj) { | 
					
						
							|  |  |  | 							next(false); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 						if (notifObj.uniqueId === uniqueId) { | 
					
						
							|  |  |  | 							next(true); | 
					
						
							| 
									
										
										
										
											2013-11-04 23:30:30 +02:00
										 |  |  | 						} else { | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 							next(false); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2013-09-11 15:41:20 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				}, function(nids) { | 
					
						
							|  |  |  | 					callback(null, nids); | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							| 
									
										
										
										
											2013-05-22 21:43:01 -04:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-11-04 01:57:58 +02:00
										 |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2013-11-30 13:13:18 -05:00
										 |  |  | }(exports)); |