User.exists change

v1.18.x
barisusakli 10 years ago
parent 45ebeb276e
commit b616e69070

@ -30,7 +30,7 @@ var async = require('async'),
/* Assorted */ /* Assorted */
Meta.userOrGroupExists = function(slug, callback) { Meta.userOrGroupExists = function(slug, callback) {
async.parallel([ async.parallel([
async.apply(user.exists, slug), async.apply(user.existsBySlug, slug),
async.apply(groups.existsBySlug, slug) async.apply(groups.existsBySlug, slug)
], function(err, results) { ], function(err, results) {
callback(err, results ? results.some(function(result) { return result; }) : false); callback(err, results ? results.some(function(result) { return result; }) : false);

@ -168,11 +168,15 @@ var async = require('async'),
Password.hash(nconf.get('bcrypt_rounds') || 12, password, callback); 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) { User.getUidByUserslug(userslug, function(err, exists) {
callback(err, !! exists); callback(err, !! exists);
}); });
}; }
User.getUidByUsername = function(username, callback) { User.getUidByUsername = function(username, callback) {
if (!username) { if (!username) {

@ -199,7 +199,7 @@ module.exports = function(User) {
var newUsername = ''; var newUsername = '';
async.forever(function(next) { async.forever(function(next) {
newUsername = userData.username + (Math.floor(Math.random() * 255) + 1); newUsername = userData.username + (Math.floor(Math.random() * 255) + 1);
User.exists(newUsername, function(err, exists) { User.existsBySlug(newUsername, function(err, exists) {
if (err) { if (err) {
return callback(err); return callback(err);
} }

@ -3,7 +3,7 @@
var async = require('async'), var async = require('async'),
plugins = require('../plugins'), plugins = require('../plugins'),
db = require('./../database'); db = require('../database');
module.exports = function(User) { module.exports = function(User) {
@ -24,34 +24,41 @@ module.exports = function(User) {
return callback(new Error('[[error:you-cant-follow-yourself]]')); return callback(new Error('[[error:you-cant-follow-yourself]]'));
} }
User.isFollowing(uid, theiruid, function(err, isFollowing) { async.waterfall([
if (err) { function (next) {
return callback(err); User.exists(theiruid, next);
} },
function (exists, next) {
if (type === 'follow') { if (!exists) {
if (isFollowing) { return next(new Error('[[error:no-user]]'));
return callback(new Error('[[error:already-following]]'));
} }
var now = Date.now(); User.isFollowing(uid, theiruid, next);
async.parallel([ },
async.apply(db.sortedSetAdd, 'following:' + uid, now, theiruid), function (isFollowing, next) {
async.apply(db.sortedSetAdd, 'followers:' + theiruid, now, uid), if (type === 'follow') {
async.apply(User.incrementUserFieldBy, uid, 'followingCount', 1), if (isFollowing) {
async.apply(User.incrementUserFieldBy, theiruid, 'followerCount', 1) return next(new Error('[[error:already-following]]'));
], callback); }
} else { var now = Date.now();
if (!isFollowing) { async.parallel([
return callback(new Error('[[error:not-following]]')); 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) { User.getFollowing = function(uid, start, stop, callback) {

@ -71,10 +71,13 @@ module.exports = function(User) {
return next(); return next();
} }
User.getUserFields(uid, ['username', 'userslug'], function(err, userData) { User.getUserFields(uid, ['username', 'userslug'], function(err, userData) {
if (err) {
return next(err);
}
var userslug = utils.slugify(data.username); var userslug = utils.slugify(data.username);
if(userslug === userData.userslug) { if (userslug === userData.userslug) {
return next(); return next();
} }
@ -86,12 +89,12 @@ module.exports = function(User) {
return next(new Error('[[error:username-too-long]]')); 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]]')); return next(new Error('[[error:invalid-username]]'));
} }
User.exists(userslug, function(err, exists) { User.existsBySlug(userslug, function(err, exists) {
if(err) { if (err) {
return next(err); return next(err);
} }

@ -186,7 +186,7 @@ describe('User', function() {
it('should delete a user account', function(done) { it('should delete a user account', function(done) {
User.delete(uid, function(err) { User.delete(uid, function(err) {
assert.ifError(err); assert.ifError(err);
User.exists('usertodelete', function(err, exists) { User.existsBySlug('usertodelete', function(err, exists) {
assert.ifError(err); assert.ifError(err);
assert.equal(exists, false); assert.equal(exists, false);
done(); done();

Loading…
Cancel
Save