FEATURE: Unassign the review queue topic when a flag is handled

FEATURE: Unassign the review queue topic when a flag is handled

diff --git a/app/models/reviewable_flagged_post.rb b/app/models/reviewable_flagged_post.rb
index 340d63c..1dc8985 100644
--- a/app/models/reviewable_flagged_post.rb
+++ b/app/models/reviewable_flagged_post.rb
@@ -120,6 +120,7 @@ class ReviewableFlaggedPost < Reviewable
     end
 
     if actions.first.present?
+      unassign_topic performed_by, post
       DiscourseEvent.trigger(:flag_reviewed, post)
       DiscourseEvent.trigger(:flag_deferred, actions.first)
     end
@@ -190,6 +191,7 @@ class ReviewableFlaggedPost < Reviewable
     Post.with_deleted.where(id: target_id).update_all(cached)
 
     if actions.first.present?
+      unassign_topic performed_by, post
       DiscourseEvent.trigger(:flag_reviewed, post)
       DiscourseEvent.trigger(:flag_disagreed, actions.first)
     end
@@ -256,6 +258,7 @@ protected
     DiscourseEvent.trigger(:confirmed_spam_post, post) if trigger_spam
 
     if actions.first.present?
+      unassign_topic performed_by, post
       DiscourseEvent.trigger(:flag_reviewed, post)
       DiscourseEvent.trigger(:flag_agreed, actions.first)
       yield(actions.first) if block_given?
@@ -279,6 +282,26 @@ protected
     end
   end
 
+  def unassign_topic(performed_by, post)
+    topic = post.topic
+    return unless topic && performed_by && SiteSetting.reviewable_claiming != 'disabled'
+    ReviewableClaimedTopic.where(topic_id: topic.id).delete_all
+    topic.reviewables.find_each do |reviewable|
+      reviewable.log_history(:unclaimed, performed_by)
+    end
+
+    user_ids = User.staff.pluck(:id)
+
+    if SiteSetting.enable_category_group_review? && group_id = topic.category&.reviewable_by_group_id.presence
+      user_ids.concat(GroupUser.where(group_id: group_id).pluck(:user_id))
+      user_ids.uniq!
+    end
+
+    data = { topic_id: topic.id }
+
+    MessageBus.publish("/reviewable_claimed", data, user_ids: user_ids)
+  end
+
 private
 
   def destroyer(performed_by, post)
diff --git a/spec/models/reviewable_flagged_post_spec.rb b/spec/models/reviewable_flagged_post_spec.rb
index f656e57..0712468 100644
--- a/spec/models/reviewable_flagged_post_spec.rb
+++ b/spec/models/reviewable_flagged_post_spec.rb
@@ -93,6 +93,20 @@ RSpec.describe ReviewableFlaggedPost, type: :model do
       expect(post).not_to be_hidden
     end
 
+    describe "with reviewable claiming enabled" do
+      fab!(:claimed) { Fabricate(:reviewable_claimed_topic, topic: post.topic, user: moderator) }
+      it "clears the claimed topic on resolve" do
+        SiteSetting.reviewable_claiming = 'required'
+        reviewable.perform(moderator, :agree_and_keep)
+        expect(reviewable).to be_approved
+        expect(score.reload).to be_agreed
+        expect(post).not_to be_hidden
+        expect(ReviewableClaimedTopic.where(topic_id: post.topic.id).exists?).to eq(false)
+        expect(post.topic.reviewables.first.history.where(reviewable_history_type: ReviewableHistory.types[:unclaimed]).size).to eq(1)
+      end
+
+    end
+
     it "agree_and_suspend agrees with the flags and keeps the post" do
       reviewable.perform(moderator, :agree_and_suspend)
       expect(reviewable).to be_approved

GitHub sha: 7282435e