prevent double follow/unfollow

v1.18.x
barisusakli 10 years ago
parent 5b67cd841e
commit bd17bff7d4

@ -282,28 +282,24 @@ SocketUser.follow = function(socket, data, callback) {
return; return;
} }
toggleFollow('follow', socket.uid, data.uid, function(err) { async.waterfall([
if (err) { function(next) {
return callback(err); toggleFollow('follow', socket.uid, data.uid, next);
} },
function(next) {
user.getUserFields(socket.uid, ['username', 'userslug'], function(err, userData) { user.getUserFields(socket.uid, ['username', 'userslug'], next);
if (err) { },
return callback(err); function(userData, next) {
}
notifications.create({ notifications.create({
bodyShort: '[[notifications:user_started_following_you, ' + userData.username + ']]', bodyShort: '[[notifications:user_started_following_you, ' + userData.username + ']]',
nid: 'follow:' + data.uid + ':uid:' + socket.uid, nid: 'follow:' + data.uid + ':uid:' + socket.uid,
from: socket.uid from: socket.uid
}, function(err, notification) { }, next);
if (!err && notification) { },
notifications.push(notification, [data.uid]); function(notification, next) {
} notifications.push(notification, [data.uid], next);
callback(err); }
}); ], callback);
});
});
}; };
SocketUser.unfollow = function(socket, data, callback) { SocketUser.unfollow = function(socket, data, callback) {

@ -23,22 +23,34 @@ module.exports = function(User) {
return callback(new Error('[[error:you-cant-follow-yourself]]')); return callback(new Error('[[error:you-cant-follow-yourself]]'));
} }
if (type === 'follow') { User.isFollowing(uid, theiruid, function(err, isFollowing) {
var now = Date.now(); if (err) {
async.parallel([ return callback(err);
async.apply(db.sortedSetAdd, 'following:' + uid, now, theiruid), }
async.apply(db.sortedSetAdd, 'followers:' + theiruid, now, uid),
async.apply(User.incrementUserFieldBy, uid, 'followingCount', 1), if (type === 'follow') {
async.apply(User.incrementUserFieldBy, theiruid, 'followerCount', 1) if (isFollowing) {
], callback); return callback(new Error('[[error:already-following]]'));
} else { }
async.parallel([ var now = Date.now();
async.apply(db.sortedSetRemove, 'following:' + uid, theiruid), async.parallel([
async.apply(db.sortedSetRemove, 'followers:' + theiruid, uid), async.apply(db.sortedSetAdd, 'following:' + uid, now, theiruid),
async.apply(User.decrementUserFieldBy, uid, 'followingCount', 1), async.apply(db.sortedSetAdd, 'followers:' + theiruid, now, uid),
async.apply(User.decrementUserFieldBy, theiruid, 'followerCount', 1) async.apply(User.incrementUserFieldBy, uid, 'followingCount', 1),
], callback); 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) { User.getFollowing = function(uid, start, end, callback) {

Loading…
Cancel
Save