FIX: Do not assign the same user more than once

FIX: Do not assign the same user more than once

This prevents resending assignment notification emails.

diff --git a/lib/topic_assigner.rb b/lib/topic_assigner.rb
index 1513fd3..7a80de6 100644
--- a/lib/topic_assigner.rb
+++ b/lib/topic_assigner.rb
@@ -131,6 +131,8 @@ SQL
   end
 
   def assign(assign_to, silent: false)
+    return false if @topic.custom_fields[ASSIGNED_TO_ID] == assign_to.id.to_s
+
     @topic.custom_fields[ASSIGNED_TO_ID] = assign_to.id
     @topic.custom_fields[ASSIGNED_BY_ID] = @assigned_by.id
     @topic.save_custom_fields
diff --git a/spec/lib/topic_assigner_spec.rb b/spec/lib/topic_assigner_spec.rb
index c32341e..9863bcf 100644
--- a/spec/lib/topic_assigner_spec.rb
+++ b/spec/lib/topic_assigner_spec.rb
@@ -114,6 +114,19 @@ RSpec.describe TopicAssigner do
           .to eq(moderator.id)
       end
     end
+
+    it "doesn't assign the same user more than once" do
+      SiteSetting.assign_mailer_enabled = true
+
+      Email::Sender.any_instance.expects(:send).once
+      expect(assigner.assign(moderator)).to eq(true)
+
+      Email::Sender.any_instance.expects(:send).never
+      expect(assigner.assign(moderator)).to eq(false)
+
+      Email::Sender.any_instance.expects(:send).once
+      expect(assigner.assign(Fabricate(:moderator))).to eq(true)
+    end
   end
 
   context "unassign_on_close" do

GitHub sha: a308e2a4

1 Like