FIX: Prevent duplicate approvals (#54)

FIX: Prevent duplicate approvals (#54)

diff --git a/lib/discourse_code_review/state/commit_approval.rb b/lib/discourse_code_review/state/commit_approval.rb
index a5d7dc1..efb82b2 100644
--- a/lib/discourse_code_review/state/commit_approval.rb
+++ b/lib/discourse_code_review/state/commit_approval.rb
@@ -94,32 +94,36 @@ module DiscourseCodeReview::State::CommitApproval
     private
 
     def ensure_approved_post(topic, approver)
-      post =
-        Post.where(
-          topic_id: topic.id,
-          user_id: approver.id,
-          action_code: "approved"
-        ).first
-
-      unless post
-        old_highest_post_number = topic.highest_post_number
-        post =
-          topic.add_moderator_post(
-            approver,
-            nil,
-            bump: false,
-            post_type: Post.types[:small_action],
-            action_code: "approved"
-          )
+      DistributedMutex.synchronize("code-review:ensure-approved-post:#{topic.id}") do
+        ActiveRecord::Base.transaction(requires_new: true) do
+          post =
+            Post.where(
+              topic_id: topic.id,
+              user_id: approver.id,
+              action_code: "approved"
+            ).first
+
+          unless post
+            old_highest_post_number = topic.highest_post_number
+            post =
+              topic.add_moderator_post(
+                approver,
+                nil,
+                bump: false,
+                post_type: Post.types[:small_action],
+                action_code: "approved"
+              )
+
+            PostTiming.pretend_read(
+              topic.id,
+              old_highest_post_number,
+              post.post_number
+            )
+          end
 
-        PostTiming.pretend_read(
-          topic.id,
-          old_highest_post_number,
-          post.post_number
-        )
+          post
+        end
       end
-
-      post
     end
 
     def transition_to_approved(topic)

GitHub sha: 2c58a363

This commit appears in #54 which was approved by eviltrout. It was merged by danielwaterworth.