From 30104b898cea7c1772e8f522d73226b1e3b90937 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Thu, 26 Mar 2015 12:22:27 -0400 Subject: [PATCH] dont allow vote spam --- src/favourites.js | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/src/favourites.js b/src/favourites.js index 0c9c81dc46..cbcb080a49 100644 --- a/src/favourites.js +++ b/src/favourites.js @@ -10,6 +10,8 @@ var async = require('async'), (function (Favourites) { + var votesInProgress = {}; + function vote(type, unvote, pid, uid, callback) { uid = parseInt(uid, 10); @@ -127,13 +129,41 @@ var async = require('async'), }); }; + function voteInProgress(pid, uid) { + return Array.isArray(votesInProgress[uid]) && votesInProgress[uid].indexOf(parseInt(pid, 10)) !== -1; + } + + function putVoteInProgress(pid, uid) { + votesInProgress[uid] = votesInProgress[uid] || []; + votesInProgress[uid].push(parseInt(pid, 10)); + } + + function clearVoteProgress(pid, uid) { + if (Array.isArray(votesInProgress[uid])) { + var index = votesInProgress[uid].indexOf(parseInt(pid, 10)); + if (index !== -1) { + votesInProgress[uid].splice(index, 1); + } + } + } + function toggleVote(type, pid, uid, callback) { + function done(err, data) { + clearVoteProgress(pid, uid); + callback(err, data); + } + + if (voteInProgress(pid, uid)) { + return callback(new Error('[[error:already-voting-for-this-post]]')); + } + putVoteInProgress(pid, uid); + unvote(pid, uid, type, function(err) { if (err) { - return callback(err); + return done(err); } - vote(type, false, pid, uid, callback); + vote(type, false, pid, uid, done); }); }