Cleanup user.js for style guide compliance

v1.18.x
Damian Bushong 12 years ago
parent 0a09156a5e
commit 78ef54baf2

@ -12,26 +12,25 @@ var utils = require('./../public/src/utils.js'),
(function(User) { (function(User) {
User.getUserField = function(uid, field, callback) { User.getUserField = function(uid, field, callback) {
RDB.hget('user:' + uid, field, function(err, data) { RDB.hget('user:' + uid, field, function(err, data) {
if(err === null) if(err === null) {
callback(data); callback(data);
else } else {
console.log(err); console.log(err);
}
}); });
} }
User.getUserFields = function(uid, fields, callback) { User.getUserFields = function(uid, fields, callback) {
RDB.hmget('user:' + uid, fields, function(err, data) { RDB.hmget('user:' + uid, fields, function(err, data) {
if(err === null) { if(err === null) {
var returnData = {}; for(var i = 0, returnData = {}, ii=fields.length; i<ii; ++i) {
for(var i=0, ii=fields.length; i<ii; ++i) {
returnData[fields[i]] = data[i]; returnData[fields[i]] = data[i];
} }
callback(returnData); callback(returnData);
} } else {
else
console.log(err); console.log(err);
}
}); });
} }
@ -41,12 +40,11 @@ var utils = require('./../public/src/utils.js'),
return; return;
} }
var uuids = uids.filter(function(value, index, self) {
return self.indexOf(value) === index;
});
var data = {}, var data = {},
loaded = 0; loaded = 0;
uuids = uids.filter(function(value, index, self) {
return self.indexOf(value) === index;
});
for (var i=0, ii=uuids.length; i<ii; i++) { for (var i=0, ii=uuids.length; i<ii; i++) {
(function(user_id) { (function(user_id) {
@ -60,7 +58,6 @@ var utils = require('./../public/src/utils.js'),
} }
User.getUserData = function(uid, callback) { User.getUserData = function(uid, callback) {
RDB.hgetall('user:' + uid, function(err, data) { RDB.hgetall('user:' + uid, function(err, data) {
if(err === null) { if(err === null) {
if(data) { if(data) {
@ -68,33 +65,29 @@ var utils = require('./../public/src/utils.js'),
delete data['password']; delete data['password'];
} }
callback(data); callback(data);
} } else {
else
console.log(err); console.log(err);
}
}); });
} }
User.updateProfile = function(uid, data, callback) { User.updateProfile = function(uid, data, callback) {
var fields = ['email', 'fullname', 'website', 'location', 'birthday', 'signature']; var fields = ['email', 'fullname', 'website', 'location', 'birthday', 'signature'];
var key = '';
if(data['signature'] !== undefined && data['signature'].length > 150) { if(data['signature'] !== undefined && data['signature'].length > 150) {
callback({error:'Signature can\'t be longer than 150 characters!'}); callback({error:'Signature can\'t be longer than 150 characters!'});
return; return;
} }
for(var i = 0, key, ii = fields.length; i < ii; ++i) {
for(var i=0,ii=fields.length; i<ii; ++i) {
key = fields[i]; key = fields[i];
if(data[key] !== undefined) { if(data[key] !== undefined) {
if(key === 'email') { if(key === 'email') {
User.setUserField(uid, 'gravatarpicture', User.createGravatarURLFromEmail(data[key])); User.setUserField(uid, 'gravatarpicture', User.createGravatarURLFromEmail(data[key]));
RDB.set('email:' + data['email'] +':uid', uid); RDB.set('email:' + data['email'] +':uid', uid);
} } else if(key === 'signature') {
else if(key === 'signature') {
data[key] = utils.strip_tags(data[key]); data[key] = utils.strip_tags(data[key]);
} }
@ -141,7 +134,6 @@ var utils = require('./../public/src/utils.js'),
console.log('deleting uid ' + uid); console.log('deleting uid ' + uid);
User.getUserData(uid, function(data) { User.getUserData(uid, function(data) {
RDB.del('username:' + data['username'] + ':uid'); RDB.del('username:' + data['username'] + ':uid');
RDB.del('email:' + data['email'] +':uid'); RDB.del('email:' + data['email'] +':uid');
RDB.del('userslug:'+ data['userslug'] +':uid'); RDB.del('userslug:'+ data['userslug'] +':uid');
@ -149,22 +141,21 @@ var utils = require('./../public/src/utils.js'),
RDB.del('user:' + uid); RDB.del('user:' + uid);
RDB.del('followers:' + uid); RDB.del('followers:' + uid);
RDB.del('following:' + uid); RDB.del('following:' + uid);
RDB.lrem('userlist', 1, data['username']); RDB.lrem('userlist', 1, data['username']);
callback(true); callback(true);
}); });
} } else {
else
callback(false); callback(false);
}
}); });
} }
User.create = function(username, password, email, callback) { User.create = function(username, password, email, callback) {
username = username.trim(), email = email.trim();
username = username.trim(); // @todo return a proper error? use node-validator?
email = email.trim();
if(!utils.isEmailValid(email) || !utils.isUserNameValid(username) || !utils.isPasswordValid(password)) { if(!utils.isEmailValid(email) || !utils.isUserNameValid(username) || !utils.isPasswordValid(password)) {
console.log('Invalid email/username/password!'); console.log('Invalid email/username/password!');
callback(null, 0); callback(null, 0);
@ -174,7 +165,6 @@ var utils = require('./../public/src/utils.js'),
var userslug = utils.slugify(username); var userslug = utils.slugify(username);
User.exists(userslug, function(exists) { User.exists(userslug, function(exists) {
if(exists) { if(exists) {
callback(null, 0); callback(null, 0);
return; return;
@ -202,15 +192,16 @@ var utils = require('./../public/src/utils.js'),
'reputation': 0, 'reputation': 0,
'postcount': 0, 'postcount': 0,
'lastposttime': 0, 'lastposttime': 0,
'administrator': (uid==1)?1:0 'administrator': (uid == 1) ? 1 : 0
}); });
RDB.set('username:' + username + ':uid', uid); RDB.set('username:' + username + ':uid', uid);
RDB.set('email:' + email +':uid', uid); RDB.set('email:' + email +':uid', uid);
RDB.set('userslug:'+ userslug +':uid', uid); RDB.set('userslug:'+ userslug +':uid', uid);
if(email) if(email) {
User.sendConfirmationEmail(email); User.sendConfirmationEmail(email);
}
RDB.incr('usercount', function(err, count) { RDB.incr('usercount', function(err, count) {
RDB.handle(err); RDB.handle(err);
@ -233,8 +224,9 @@ var utils = require('./../public/src/utils.js'),
}; };
User.createGravatarURLFromEmail = function(email) { User.createGravatarURLFromEmail = function(email) {
if (!email) if (!email) {
email = utils.generateUUID(); email = utils.generateUUID();
}
var md5sum = crypto.createHash('md5'); var md5sum = crypto.createHash('md5');
md5sum.update(email.toLowerCase().trim()); md5sum.update(email.toLowerCase().trim());
var gravatarURL = 'http://www.gravatar.com/avatar/' + md5sum.digest('hex') + '?default=identicon&s=128'; var gravatarURL = 'http://www.gravatar.com/avatar/' + md5sum.digest('hex') + '?default=identicon&s=128';
@ -246,7 +238,7 @@ var utils = require('./../public/src/utils.js'),
callback(password); callback(password);
return; return;
} }
bcrypt.genSalt(10, function(err, salt) { bcrypt.genSalt(10, function(err, salt) {
bcrypt.hash(password, salt, function(err, hash) { bcrypt.hash(password, salt, function(err, hash) {
callback(hash); callback(hash);
@ -268,12 +260,12 @@ var utils = require('./../public/src/utils.js'),
callback(userdata); callback(userdata);
}); });
}); });
} } else {
else
callback([]); callback([]);
} }
else } else {
console.log(err); console.log(err);
}
}); });
} }
@ -881,4 +873,4 @@ var utils = require('./../public/src/utils.js'),
}); });
} }
} }
}(exports)); }(exports));

Loading…
Cancel
Save