Node.js based forum software built for the modern web
prevent double follow/unfollow
barisusaklibd17bff7d47b
diff --git a/src/socket.io/user.js b/src/socket.io/user.jsindex 15aa0e2ba6..7170cf27d0 100644--- a/src/socket.io/user.js+++ b/src/socket.io/user.js@@ -282,28 +282,24 @@ SocketUser.follow = function(socket, data, callback) {return;}- toggleFollow('follow', socket.uid, data.uid, function(err) {- if (err) {- return callback(err);- }-- user.getUserFields(socket.uid, ['username', 'userslug'], function(err, userData) {- if (err) {- return callback(err);- }-+ async.waterfall([+ function(next) {+ toggleFollow('follow', socket.uid, data.uid, next);+ },+ function(next) {+ user.getUserFields(socket.uid, ['username', 'userslug'], next);+ },+ function(userData, next) {notifications.create({bodyShort: '[[notifications:user_started_following_you, ' + userData.username + ']]',nid: 'follow:' + data.uid + ':uid:' + socket.uid,from: socket.uid- }, function(err, notification) {- if (!err && notification) {- notifications.push(notification, [data.uid]);- }- callback(err);- });- });- });+ }, next);+ },+ function(notification, next) {+ notifications.push(notification, [data.uid], next);+ }+ ], callback);};SocketUser.unfollow = function(socket, data, callback) {diff --git a/src/user/follow.js b/src/user/follow.jsindex 6b0c0a3973..b7bda96903 100644--- a/src/user/follow.js+++ b/src/user/follow.js@@ -23,22 +23,34 @@ module.exports = function(User) {return callback(new Error('[[error:you-cant-follow-yourself]]'));}- if (type === 'follow') {- 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 {- 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);- }+ User.isFollowing(uid, theiruid, function(err, isFollowing) {+ if (err) {+ return callback(err);+ }++ if (type === 'follow') {+ if (isFollowing) {+ return callback(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)+ ], callback);+ } else {+ if (!isFollowing) {+ return callback(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)+ ], callback);+ }+ });}User.getFollowing = function(uid, start, end, callback) {