mirror of
https://github.com/NodeBB/NodeBB.git
synced 2026-01-05 15:20:39 +01:00
User.exists change
This commit is contained in:
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user