More cleanup of user.js

See added comments for more information on problems discovered within the codebase.
v1.18.x
Damian Bushong 12 years ago
parent 7a5013325c
commit f61e71729a

@ -240,6 +240,9 @@ var utils = require('./../public/src/utils.js'),
return; return;
} }
// round count should be variable somewhere instead of hardcoded here
// if an admin has the resources to up the round count, then making it easy for them to do so
// can't hurt
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);
@ -254,7 +257,7 @@ var utils = require('./../public/src/utils.js'),
} }
RDB.keys('username:*'+ username + '*:uid', function(err, keys) { RDB.keys('username:*'+ username + '*:uid', function(err, keys) {
if(err === null) { if(!err) {
if(keys && keys.length) { if(keys && keys.length) {
RDB.mget(keys, function(err, uids) { RDB.mget(keys, function(err, uids) {
User.getDataForUsers(uids, function(userdata) { User.getDataForUsers(uids, function(userdata) {
@ -272,7 +275,7 @@ var utils = require('./../public/src/utils.js'),
User.onNewPostMade = function(uid, tid, pid, timestamp) { User.onNewPostMade = function(uid, tid, pid, timestamp) {
User.addPostIdToUser(uid, pid) User.addPostIdToUser(uid, pid)
User.incrementUserFieldBy(uid, 'postcount', 1); User.incrementUserFieldBy(uid, 'postcount', 1);
User.setUserField(uid, 'lastposttime', timestamp); User.setUserField(uid, 'lastposttime', timestamp);
@ -289,13 +292,12 @@ var utils = require('./../public/src/utils.js'),
User.getPostIds = function(uid, start, end, callback) { User.getPostIds = function(uid, start, end, callback) {
RDB.lrange('uid:' + uid + ':posts', start, end, function(err, pids) { RDB.lrange('uid:' + uid + ':posts', start, end, function(err, pids) {
if(err === null) { if(!err) {
if(pids && pids.length) if(pids && pids.length)
callback(pids); callback(pids);
else else
callback([]); callback([]);
} } else {
else {
console.log(err); console.log(err);
callback([]); callback([]);
} }
@ -334,69 +336,78 @@ var utils = require('./../public/src/utils.js'),
}); });
emailjsServer.send(message, function(err, success) { emailjsServer.send(message, function(err, success) {
if (err) if (err) {
console.log(err); console.log(err);
}
}); });
} }
} }
User.follow = function(uid, followid, callback) { User.follow = function(uid, followid, callback) {
RDB.sadd('following:'+uid, followid, function(err, data) { RDB.sadd('following:' + uid, followid, function(err, data) {
if(err === null) { if(!err) {
RDB.sadd('followers:'+followid, uid, function(err, data) { RDB.sadd('followers:' + followid, uid, function(err, data) {
callback(data); callback(data);
}); });
} } else {
else
console.log(err); console.log(err);
}
}); });
} }
User.unfollow = function(uid, unfollowid, callback) { User.unfollow = function(uid, unfollowid, callback) {
RDB.srem('following:'+uid, unfollowid, function(err, data){ RDB.srem('following:' + uid, unfollowid, function(err, data){
if(err === null) { if(!err) {
RDB.srem('followers:'+unfollowid, uid, function(err, data){ RDB.srem('followers:' + unfollowid, uid, function(err, data){
callback(data); callback(data);
}); });
} } else {
else
console.log(err); console.log(err);
}
}); });
} }
User.getFollowing = function(uid, callback) { User.getFollowing = function(uid, callback) {
RDB.smembers('following:'+uid, function(err, userIds) { RDB.smembers('following:' + uid, function(err, userIds) {
if(err === null) if(!err) {
User.getDataForUsers(userIds, callback); User.getDataForUsers(userIds, callback);
else } else {
console.log(err); console.log(err);
}
}); });
} }
User.getFollowers = function(uid, callback) { User.getFollowers = function(uid, callback) {
RDB.smembers('followers:'+uid, function(err, userIds) { RDB.smembers('followers:' + uid, function(err, userIds) {
if(err === null) if(!err) {
User.getDataForUsers(userIds, callback); User.getDataForUsers(userIds, callback);
else } else {
console.log(err); console.log(err);
}
}); });
} }
User.getFollowingCount = function(uid, callback) { User.getFollowingCount = function(uid, callback) {
RDB.smembers('following:'+uid, function(err, userIds) { RDB.smembers('following:' + uid, function(err, userIds) {
if(err === null) if(!err) {
callback(userIds.length); callback(userIds.length);
else } else {
console.log(err); console.log(err);
}
}); });
} }
User.getFollowerCount = function(uid, callback) { User.getFollowerCount = function(uid, callback) {
RDB.smembers('followers:'+uid, function(err, userIds) { RDB.smembers('followers:' + uid, function(err, userIds) {
if(err === null) // @note why are error-handling styles being mixed?
// either go with not-error-dosomething-else-dosomethingelse, or
// go with if-error-dosomething-return
// also why is console.log(err) being used when below we're using RDB.handle()?
if(!err) {
callback(userIds.length); callback(userIds.length);
else } else {
console.log(err); console.log(err);
}
}); });
} }
@ -408,23 +419,21 @@ var utils = require('./../public/src/utils.js'),
return; return;
} }
for(var i=0, ii=userIds.length; i<ii; ++i) { for(var i = 0, ii = userIds.length; i < ii; ++i) {
User.getUserData(userIds[i], function(userData) { User.getUserData(userIds[i], function(userData) {
returnData.push(userData); returnData.push(userData);
if(returnData.length == userIds.length) if(returnData.length == userIds.length) {
callback(returnData); callback(returnData);
}
}); });
} }
} }
User.sendPostNotificationToFollowers = function(uid, tid, pid) { User.sendPostNotificationToFollowers = function(uid, tid, pid) {
User.getUserField(uid, 'username', function(username) { User.getUserField(uid, 'username', function(username) {
RDB.smembers('followers:'+uid, function(err, followers) { RDB.smembers('followers:' + uid, function(err, followers) {
topics.getTopicField(tid, 'slug', function(slug) { topics.getTopicField(tid, 'slug', function(slug) {
var message = username + ' made a new post'; var message = username + ' made a new post';
notifications.create(message, 5, global.config.url + 'topic/' + slug + '#' + pid, 'notification_'+ Date.now(), function(nid) { notifications.create(message, 5, global.config.url + 'topic/' + slug + '#' + pid, 'notification_'+ Date.now(), function(nid) {
@ -437,36 +446,41 @@ var utils = require('./../public/src/utils.js'),
} }
User.isFollowing = function(uid, theirid, callback) { User.isFollowing = function(uid, theirid, callback) {
RDB.sismember('following:'+uid, theirid, function(err, data) { RDB.sismember('following:' + uid, theirid, function(err, data) {
if(err === null) if(!err) {
callback(data === 1); callback(data === 1);
else } else {
console.log(err); console.log(err);
}
}); });
} }
// @todo check why this function checks for a callback - if there's no callback,
// it effectively does nothing
User.exists = function(userslug, callback) { User.exists = function(userslug, callback) {
User.get_uid_by_userslug(userslug, function(exists) { User.get_uid_by_userslug(userslug, function(exists) {
exists = !!exists; if (callback) {
callback(!!exists);
if (callback) }
callback(exists);
}); });
}; };
User.count = function(socket) { User.count = function(socket) {
RDB.get('usercount', function(err, count) { RDB.get('usercount', function(err, count) {
RDB.handle(err); if (err) {
socket.emit('user.count', {count: (count === null) ? 0 : count}); RDB.handle(err);
}
socket.emit('user.count', { count: count ? count : 0 });
}); });
}; };
User.latest = function(socket) { User.latest = function(socket) {
RDB.lrange('userlist', 0, 0, function(err, username) { RDB.lrange('userlist', 0, 0, function(err, username) {
RDB.handle(err); if (err) {
RDB.handle(err);
}
User.get_uid_by_username(username, function(uid) { User.get_uid_by_username(username, function(uid) {
User.getUserField(uid, 'userslug', function(userslug) { User.getUserField(uid, 'userslug', function(userslug) {
socket.emit('user.latest', {userslug: userslug, username: username}); socket.emit('user.latest', {userslug: userslug, username: username});
}); });
@ -476,14 +490,18 @@ var utils = require('./../public/src/utils.js'),
User.get_uid_by_username = function(username, callback) { User.get_uid_by_username = function(username, callback) {
RDB.get('username:' + username + ':uid', function(err, data) { RDB.get('username:' + username + ':uid', function(err, data) {
RDB.handle(err); if (err) {
RDB.handle(err);
}
callback(data); callback(data);
}); });
}; };
User.get_uid_by_userslug = function(userslug, callback) { User.get_uid_by_userslug = function(userslug, callback) {
RDB.get('userslug:' + userslug + ':uid', function(err, data) { RDB.get('userslug:' + userslug + ':uid', function(err, data) {
RDB.handle(err); if (err) {
RDB.handle(err);
}
callback(data); callback(data);
}); });
}; };
@ -491,16 +509,17 @@ var utils = require('./../public/src/utils.js'),
User.get_usernames_by_uids = function(uids, callback) { User.get_usernames_by_uids = function(uids, callback) {
var usernames = []; var usernames = [];
if (!Array.isArray(uids)) return callback([]); if (!Array.isArray(uids)) {
return callback([]);
}
for(var i=0, ii=uids.length; i<ii; ++i) { for(var i=0, ii=uids.length; i<ii; ++i) {
User.getUserField(uids[i],'username', function(username) { User.getUserField(uids[i],'username', function(username) {
usernames.push(username); usernames.push(username);
if(usernames.length >= uids.length) if(usernames.length >= uids.length) {
callback(usernames); callback(usernames);
}
}); });
} }
} }
@ -508,51 +527,65 @@ var utils = require('./../public/src/utils.js'),
User.get_userslugs_by_uids = function(uids, callback) { User.get_userslugs_by_uids = function(uids, callback) {
var userslugs = []; var userslugs = [];
if (!Array.isArray(uids)) return callback([]); if (!Array.isArray(uids)) {
return callback([]);
}
// @todo - rework this logic. it doesn't make much sense when you're going through
// each and then placing the check logic into the innermost callback.
// this is probably a situation where an async.method is ideal
for(var i=0, ii=uids.length; i<ii; ++i) { for(var i=0, ii=uids.length; i<ii; ++i) {
User.getUserField(uids[i],'userslug', function(userslug) { User.getUserField(uids[i],'userslug', function(userslug) {
userslugs.push(userslug); userslugs.push(userslug);
if(userslugs.length >= uids.length) if(userslugs.length >= uids.length) {
callback(userslugs); callback(userslugs);
}
}); });
} }
} }
User.get_uid_by_email = function(email, callback) { User.get_uid_by_email = function(email, callback) {
RDB.get('email:' + email + ':uid', function(err, data) { RDB.get('email:' + email + ':uid', function(err, data) {
RDB.handle(err); if (err) {
RDB.handle(err);
}
callback(data); callback(data);
}); });
}; };
User.get_uid_by_session = function(session, callback) { User.get_uid_by_session = function(session, callback) {
RDB.get('sess:' + session + ':uid', function(err, data) { RDB.get('sess:' + session + ':uid', function(err, data) {
RDB.handle(err); if (err) {
RDB.handle(err);
}
callback(data); callback(data);
}); });
}; };
User.get_uid_by_twitter_id = function(twid, callback) { User.get_uid_by_twitter_id = function(twid, callback) {
RDB.hget('twid:uid', twid, function(err, uid) { RDB.hget('twid:uid', twid, function(err, uid) {
RDB.handle(err); if (err) {
RDB.handle(err);
}
callback(uid); callback(uid);
}); });
} }
User.get_uid_by_google_id = function(gplusid, callback) { User.get_uid_by_google_id = function(gplusid, callback) {
RDB.hget('gplusid:uid', gplusid, function(err, uid) { RDB.hget('gplusid:uid', gplusid, function(err, uid) {
RDB.handle(err); if (err) {
RDB.handle(err);
}
callback(uid); callback(uid);
}); });
} }
User.get_uid_by_fbid = function(fbid, callback) { User.get_uid_by_fbid = function(fbid, callback) {
RDB.hget('fbid:uid', fbid, function(err, uid) { RDB.hget('fbid:uid', fbid, function(err, uid) {
RDB.handle(err); if (err) {
RDB.handle(err);
}
callback(uid); callback(uid);
}); });
} }
@ -560,7 +593,9 @@ var utils = require('./../public/src/utils.js'),
User.session_ping = function(sessionID, uid) { User.session_ping = function(sessionID, uid) {
// Start, replace, or extend a session // Start, replace, or extend a session
RDB.get('sess:' + sessionID, function(err, session) { RDB.get('sess:' + sessionID, function(err, session) {
RDB.handle(err); if (err) {
RDB.handle(err);
}
var expiry = 60*60*24*14, // Login valid for two weeks var expiry = 60*60*24*14, // Login valid for two weeks
sess_key = 'sess:' + sessionID + ':uid', sess_key = 'sess:' + sessionID + ':uid',
@ -575,61 +610,84 @@ var utils = require('./../public/src/utils.js'),
User.isModerator = function(uid, cid, callback) { User.isModerator = function(uid, cid, callback) {
RDB.sismember('cid:' + cid + ':moderators', uid, function(err, exists) { RDB.sismember('cid:' + cid + ':moderators', uid, function(err, exists) {
// @todo handle error
callback(!!exists); callback(!!exists);
}); });
} }
User.isAdministrator = function(uid, callback) { User.isAdministrator = function(uid, callback) {
RDB.sismember('administrators', uid, function(err, exists) { RDB.sismember('administrators', uid, function(err, exists) {
// @todo handle error
callback(!!exists); callback(!!exists);
}); });
} }
User.makeAdministrator = function(uid, callback) { User.makeAdministrator = function(uid, callback) {
RDB.sadd('administrators', uid, function(err, data){ RDB.sadd('administrators', uid, function(err, data){
if(err === null) { if(!err) {
User.setUserField(uid, 'administrator', 1); User.setUserField(uid, 'administrator', 1);
} }
if(callback)
if(callback) {
// @todo address why we're only sending back a boolean in the callback and not an error if it occurred
callback(err === null); callback(err === null);
}
}); });
} }
User.removeAdministrator = function(uid, callback) { User.removeAdministrator = function(uid, callback) {
RDB.srem('administrators', uid, function(err, data){ RDB.srem('administrators', uid, function(err, data){
if(err === null) { if(!err) {
User.setUserField(uid, 'administrator', 0); User.setUserField(uid, 'administrator', 0);
} }
if(callback)
if(callback) {
// @todo address why we're only sending back a boolean in the callback and not an error if it occurred
callback(err === null); callback(err === null);
}
}); });
} }
User.reset = { User.reset = {
validate: function(socket, code, callback) { validate: function(socket, code, callback) {
if (typeof callback !== 'function') callback = undefined; if (typeof callback !== 'function') {
callback = null;
}
RDB.get('reset:' + code + ':uid', function(err, uid) { RDB.get('reset:' + code + ':uid', function(err, uid) {
RDB.handle(err); if (err) {
RDB.handle(err);
}
if (uid !== null) { if (uid !== null) {
RDB.get('reset:' + code + ':expiry', function(err, expiry) { RDB.get('reset:' + code + ':expiry', function(err, expiry) {
RDB.handle(err); if (err) {
RDB.handle(err);
}
if (expiry >= +new Date()/1000|0) { if (expiry >= +Date.now()/1000|0) {
if (!callback) socket.emit('user:reset.valid', { valid: true }); if (!callback) {
else callback(true); socket.emit('user:reset.valid', { valid: true });
} else {
callback(true);
}
} else { } else {
// Expired, delete from db // Expired, delete from db
RDB.del('reset:' + code + ':uid'); RDB.del('reset:' + code + ':uid');
RDB.del('reset:' + code + ':expiry'); RDB.del('reset:' + code + ':expiry');
if (!callback) socket.emit('user:reset.valid', { valid: false }); if (!callback) {
else callback(false); socket.emit('user:reset.valid', { valid: false });
} else {
callback(false);
}
} }
}); });
} else { } else {
if (!callback) socket.emit('user:reset.valid', { valid: false }); if (!callback) {
else callback(false); socket.emit('user:reset.valid', { valid: false });
} else {
callback(false);
}
} }
}); });
}, },
@ -670,6 +728,7 @@ var utils = require('./../public/src/utils.js'),
status: "error", status: "error",
message: "send-failed" message: "send-failed"
}); });
// @todo handle error properly
throw new Error(err); throw new Error(err);
} }
}); });
@ -686,7 +745,9 @@ var utils = require('./../public/src/utils.js'),
this.validate(code, function(validated) { this.validate(code, function(validated) {
if (validated) { if (validated) {
RDB.get('reset:' + code + ':uid', function(err, uid) { RDB.get('reset:' + code + ':uid', function(err, uid) {
RDB.handle(err); if (err) {
RDB.handle(err);
}
User.setUserField(uid, 'password', password); User.setUserField(uid, 'password', password);
RDB.del('reset:' + code + ':uid'); RDB.del('reset:' + code + ':uid');
@ -703,24 +764,25 @@ var utils = require('./../public/src/utils.js'),
exists: function(socket, email, callback) { exists: function(socket, email, callback) {
User.get_uid_by_email(email, function(exists) { User.get_uid_by_email(email, function(exists) {
exists = !!exists; exists = !!exists;
if (typeof callback !== 'function') socket.emit('user.email.exists', { exists: exists }); if (typeof callback !== 'function') {
else callback(exists); socket.emit('user.email.exists', { exists: exists });
} else {
callback(exists);
}
}); });
}, },
confirm: function(code, callback) { confirm: function(code, callback) {
RDB.get('confirm:' + code + ':email', function(err, email) { RDB.get('confirm:' + code + ':email', function(err, email) {
RDB.handle(err); if (err) {
RDB.handle(err);
}
if (email !== null) { if (email !== null) {
RDB.set('email:' + email + ':confirm', true); RDB.set('email:' + email + ':confirm', true);
RDB.del('confirm:' + code + ':email'); RDB.del('confirm:' + code + ':email');
callback({ callback({ status: 'ok' });
status: 'ok'
});
} else { } else {
callback({ callback({ status: 'not_ok' });
status: 'not_ok'
});
} }
}); });
} }
@ -728,19 +790,24 @@ var utils = require('./../public/src/utils.js'),
User.get_online_users = function(socket, uids) { User.get_online_users = function(socket, uids) {
RDB.sismembers('users:online', uids, function(err, data) { RDB.sismembers('users:online', uids, function(err, data) {
// @todo handle err
socket.emit('api:user.get_online_users', data); socket.emit('api:user.get_online_users', data);
}); });
}; };
User.go_online = function(uid) { User.go_online = function(uid) {
RDB.sadd('users:online', uid, function(err) { RDB.sadd('users:online', uid, function(err) {
if (err) RDB.handle(err); if (err) {
RDB.handle(err);
}
}); });
}; };
User.go_offline = function(uid) { User.go_offline = function(uid) {
RDB.srem('users:online', uid, function(err) { RDB.srem('users:online', uid, function(err) {
if (err) RDB.handle(err); if (err) {
RDB.handle(err);
}
}); });
}; };
@ -749,7 +816,7 @@ var utils = require('./../public/src/utils.js'),
get_record : function(socket) { get_record : function(socket) {
RDB.mget(['global:active_user_record', 'global:active_user_record_date'], function(err, data) { RDB.mget(['global:active_user_record', 'global:active_user_record_date'], function(err, data) {
RDB.handle(err); RDB.handle(err);
socket.emit('api:user.active.get_record', {record: data[0], timestamp: data[1]}); socket.emit('api:user.active.get_record', { record: data[0], timestamp: data[1] });
}); });
}, },
@ -821,6 +888,7 @@ var utils = require('./../public/src/utils.js'),
async.parallel({ async.parallel({
unread: function(next) { unread: function(next) {
RDB.zrevrangebyscore('uid:' + uid + ':notifications:unread', 10, 0, function(err, nids) { RDB.zrevrangebyscore('uid:' + uid + ':notifications:unread', 10, 0, function(err, nids) {
// @todo handle err
var unread = []; var unread = [];
if (nids && nids.length > 0) { if (nids && nids.length > 0) {
async.eachSeries(nids, function(nid, next) { async.eachSeries(nids, function(nid, next) {
@ -831,11 +899,14 @@ var utils = require('./../public/src/utils.js'),
}, function(err) { }, function(err) {
next(null, unread); next(null, unread);
}); });
} else next(null, unread); } else {
next(null, unread);
}
}); });
}, },
read: function(next) { read: function(next) {
RDB.zrevrangebyscore('uid:' + uid + ':notifications:read', 10, 0, function(err, nids) { RDB.zrevrangebyscore('uid:' + uid + ':notifications:read', 10, 0, function(err, nids) {
// @todo handle err
var read = []; var read = [];
if (nids && nids.length > 0) { if (nids && nids.length > 0) {
async.eachSeries(nids, function(nid, next) { async.eachSeries(nids, function(nid, next) {
@ -846,31 +917,40 @@ var utils = require('./../public/src/utils.js'),
}, function(err) { }, function(err) {
next(null, read); next(null, read);
}); });
} else next(null, read); } else {
next(null, read);
}
}); });
} }
}, function(err, notifications) { }, function(err, notifications) {
// While maintaining score sorting, sort by time // While maintaining score sorting, sort by time
notifications.read.sort(function(a, b) { notifications.read.sort(function(a, b) {
if (a.score === b.score) return (a.datetime - b.datetime) > 0 ? -1 : 1; if (a.score === b.score) {
return (a.datetime - b.datetime) > 0 ? -1 : 1;
}
}); });
notifications.unread.sort(function(a, b) { notifications.unread.sort(function(a, b) {
if (a.score === b.score) return (a.datetime - b.datetime) > 0 ? -1 : 1; if (a.score === b.score) {
return (a.datetime - b.datetime) > 0 ? -1 : 1;
}
}); });
callback(notifications); callback(notifications);
}); });
}, },
hasFlag: function(uid, callback) { hasFlag: function(uid, callback) {
RDB.get('uid:1:notifications:flag', function(err, flag) { RDB.get('uid:1:notifications:flag', function(err, flag) {
if (err) RDB.handle(err); if (err) {
RDB.handle(err);
}
if (flag === '1') callback(true); callback(flag === 1);
else callback(false);
}); });
}, },
removeFlag: function(uid) { removeFlag: function(uid) {
RDB.del('uid:' + uid + ':notifications:flag', function(err) { RDB.del('uid:' + uid + ':notifications:flag', function(err) {
if (err) RDB.handle(err); if (err) {
RDB.handle(err);
}
}); });
} }
} }

Loading…
Cancel
Save