diff --git a/src/meta.js b/src/meta.js index 316d6349ce..3209bb13d6 100644 --- a/src/meta.js +++ b/src/meta.js @@ -30,7 +30,7 @@ var async = require('async'), /* Assorted */ Meta.userOrGroupExists = function(slug, callback) { async.parallel([ - async.apply(user.exists, slug), + async.apply(user.existsBySlug, slug), async.apply(groups.existsBySlug, slug) ], function(err, results) { callback(err, results ? results.some(function(result) { return result; }) : false); diff --git a/src/user.js b/src/user.js index e9555b6d5e..81b9bbafab 100644 --- a/src/user.js +++ b/src/user.js @@ -168,11 +168,15 @@ var async = require('async'), Password.hash(nconf.get('bcrypt_rounds') || 12, password, callback); }; - User.exists = function(userslug, callback) { + User.exists = function(uid, callback) { + db.isSortedSetMember('users:joindate', uid, callback); + }; + + User.existsBySlug = function(userslug, callback) { User.getUidByUserslug(userslug, function(err, exists) { callback(err, !! exists); }); - }; + } User.getUidByUsername = function(username, callback) { if (!username) { diff --git a/src/user/create.js b/src/user/create.js index ed04c2cd75..3238f628ee 100644 --- a/src/user/create.js +++ b/src/user/create.js @@ -199,7 +199,7 @@ module.exports = function(User) { var newUsername = ''; async.forever(function(next) { newUsername = userData.username + (Math.floor(Math.random() * 255) + 1); - User.exists(newUsername, function(err, exists) { + User.existsBySlug(newUsername, function(err, exists) { if (err) { return callback(err); } diff --git a/src/user/follow.js b/src/user/follow.js index 34efbf8149..094d33c460 100644 --- a/src/user/follow.js +++ b/src/user/follow.js @@ -3,7 +3,7 @@ var async = require('async'), plugins = require('../plugins'), - db = require('./../database'); + db = require('../database'); module.exports = function(User) { @@ -24,34 +24,41 @@ module.exports = function(User) { return callback(new Error('[[error:you-cant-follow-yourself]]')); } - User.isFollowing(uid, theiruid, function(err, isFollowing) { - if (err) { - return callback(err); - } - - if (type === 'follow') { - if (isFollowing) { - return callback(new Error('[[error:already-following]]')); + async.waterfall([ + function (next) { + User.exists(theiruid, next); + }, + function (exists, next) { + if (!exists) { + return next(new Error('[[error:no-user]]')); } - var now = Date.now(); - async.parallel([ - async.apply(db.sortedSetAdd, 'following:' + uid, now, theiruid), - async.apply(db.sortedSetAdd, 'followers:' + theiruid, now, uid), - async.apply(User.incrementUserFieldBy, uid, 'followingCount', 1), - async.apply(User.incrementUserFieldBy, theiruid, 'followerCount', 1) - ], callback); - } else { - if (!isFollowing) { - return callback(new Error('[[error:not-following]]')); + User.isFollowing(uid, theiruid, next); + }, + function (isFollowing, next) { + if (type === 'follow') { + if (isFollowing) { + return next(new Error('[[error:already-following]]')); + } + var now = Date.now(); + async.parallel([ + async.apply(db.sortedSetAdd, 'following:' + uid, now, theiruid), + async.apply(db.sortedSetAdd, 'followers:' + theiruid, now, uid), + async.apply(User.incrementUserFieldBy, uid, 'followingCount', 1), + async.apply(User.incrementUserFieldBy, theiruid, 'followerCount', 1) + ], next); + } else { + if (!isFollowing) { + return next(new Error('[[error:not-following]]')); + } + async.parallel([ + async.apply(db.sortedSetRemove, 'following:' + uid, theiruid), + async.apply(db.sortedSetRemove, 'followers:' + theiruid, uid), + async.apply(User.decrementUserFieldBy, uid, 'followingCount', 1), + async.apply(User.decrementUserFieldBy, theiruid, 'followerCount', 1) + ], next); } - async.parallel([ - async.apply(db.sortedSetRemove, 'following:' + uid, theiruid), - async.apply(db.sortedSetRemove, 'followers:' + theiruid, uid), - async.apply(User.decrementUserFieldBy, uid, 'followingCount', 1), - async.apply(User.decrementUserFieldBy, theiruid, 'followerCount', 1) - ], callback); } - }); + ], callback); } User.getFollowing = function(uid, start, stop, callback) { diff --git a/src/user/profile.js b/src/user/profile.js index 7a3f761edd..95272d55fe 100644 --- a/src/user/profile.js +++ b/src/user/profile.js @@ -71,10 +71,13 @@ module.exports = function(User) { return next(); } User.getUserFields(uid, ['username', 'userslug'], function(err, userData) { + if (err) { + return next(err); + } var userslug = utils.slugify(data.username); - if(userslug === userData.userslug) { + if (userslug === userData.userslug) { return next(); } @@ -86,12 +89,12 @@ module.exports = function(User) { return next(new Error('[[error:username-too-long]]')); } - if(!utils.isUserNameValid(data.username) || !userslug) { + if (!utils.isUserNameValid(data.username) || !userslug) { return next(new Error('[[error:invalid-username]]')); } - User.exists(userslug, function(err, exists) { - if(err) { + User.existsBySlug(userslug, function(err, exists) { + if (err) { return next(err); } diff --git a/tests/user.js b/tests/user.js index 82659ada90..1a4a402185 100644 --- a/tests/user.js +++ b/tests/user.js @@ -186,7 +186,7 @@ describe('User', function() { it('should delete a user account', function(done) { User.delete(uid, function(err) { assert.ifError(err); - User.exists('usertodelete', function(err, exists) { + User.existsBySlug('usertodelete', function(err, exists) { assert.ifError(err); assert.equal(exists, false); done();