refactored `User.create` in preparation for twitter compatibility (issue

v1.18.x
Julian Lam 12 years ago
parent 5e7da6391c
commit 667c331a67

@ -10,29 +10,34 @@ var utils = require('./../public/src/utils.js'),
async = require('async');
(function(User) {
User.create = function(username, password, email, callback) {
username = username.trim(), email = email.trim();
// @todo use node-validator?
if(!utils.isEmailValid(email) || !utils.isUserNameValid(username) || !utils.isPasswordValid(password)) {
callback('Invalid email/username/password!', 0);
return;
}
var userslug = utils.slugify(username);
User.exists(userslug, function(exists) {
if(exists) {
callback('Username taken!', 0);
return;
}
username = username.trim();
email = email.trim();
async.parallel([
function(next) {
next(!utils.isEmailValid(email) ? new Error('Invalid Email!') : null);
},
function(next) {
next(!utils.isUserNameValid(username) ? new Error('Invalid Username!') : null);
},
function(next) {
next(!utils.isPasswordValid(password) ? new Error('Invalid Password!') : null);
},
function(next) {
User.exists(userslug, function(exists) {
next(exists ? new Error('Username taken!') : null);
});
},
function(next) {
User.isEmailAvailable(email, function(available) {
if(!available) {
callback('Email taken!', 0);
return;
next(!available ? new Error('Email taken!') : null);
});
}
], function(err, results) {
if (err) callback(err, 0); // FIXME: Maintaining the 0 for backwards compatibility. Do we need this?
RDB.incr('global:next_user_id', function(err, uid) {
RDB.handle(err);
@ -63,9 +68,7 @@ var utils = require('./../public/src/utils.js'),
RDB.set('email:' + email +':uid', uid);
RDB.set('userslug:'+ userslug +':uid', uid);
if(email) {
User.sendConfirmationEmail(email);
}
RDB.incr('usercount', function(err, count) {
RDB.handle(err);
@ -86,7 +89,6 @@ var utils = require('./../public/src/utils.js'),
callback(null, uid);
});
});
});
};
User.delete = function(uid, callback) {

Loading…
Cancel
Save