about summary refs log tree commit diff
path: root/app/services/vote_service.rb
diff options
context:
space:
mode:
authorThibG <thib@sitedethib.com>2019-09-30 23:29:55 +0200
committerGitHub <noreply@github.com>2019-09-30 23:29:55 +0200
commit65db9df011a3e13efa79f4e56343aa69bdad7716 (patch)
tree1941b85de1f0d1b6f56b73345cddf664a71faf20 /app/services/vote_service.rb
parentfebcdad2e2c98aee62b55ee21bdf0debf7c6fd6b (diff)
parent3b855b5c82362783969f748ad78bcaf85f938c9f (diff)
Merge pull request #1224 from ThibG/glitch-soc/merge-upstream
Merge upstream changes
Diffstat (limited to 'app/services/vote_service.rb')
-rw-r--r--app/services/vote_service.rb32
1 files changed, 29 insertions, 3 deletions
diff --git a/app/services/vote_service.rb b/app/services/vote_service.rb
index 0eeb8fd56..cb7dce6e8 100644
--- a/app/services/vote_service.rb
+++ b/app/services/vote_service.rb
@@ -12,12 +12,24 @@ class VoteService < BaseService
     @choices = choices
     @votes   = []
 
-    ApplicationRecord.transaction do
-      @choices.each do |choice|
-        @votes << @poll.votes.create!(account: @account, choice: choice)
+    already_voted = true
+
+    RedisLock.acquire(lock_options) do |lock|
+      if lock.acquired?
+        already_voted = @poll.votes.where(account: @account).exists?
+
+        ApplicationRecord.transaction do
+          @choices.each do |choice|
+            @votes << @poll.votes.create!(account: @account, choice: choice)
+          end
+        end
+      else
+        raise Mastodon::RaceConditionError
       end
     end
 
+    increment_voters_count! unless already_voted
+
     ActivityTracker.increment('activity:interactions')
 
     if @poll.account.local?
@@ -53,4 +65,18 @@ class VoteService < BaseService
   def build_json(vote)
     Oj.dump(serialize_payload(vote, ActivityPub::VoteSerializer))
   end
+
+  def increment_voters_count!
+    unless @poll.voters_count.nil?
+      @poll.voters_count = @poll.voters_count + 1
+      @poll.save
+    end
+  rescue ActiveRecord::StaleObjectError
+    @poll.reload
+    retry
+  end
+
+  def lock_options
+    { redis: Redis.current, key: "vote:#{@poll.id}:#{@account.id}" }
+  end
 end