FIX: Check if topic is assigned before assigning again. (#25)

approved
#1

FIX: Check if topic is assigned before assigning again. (#25)

diff --git a/plugin.rb b/plugin.rb
index fcd75ba..935a306 100644
--- a/plugin.rb
+++ b/plugin.rb
@@ -50,8 +50,10 @@ after_initialize do
     end
   end
 
-  DiscourseEvent.on(:assign_topic) do |topic, user, assigning_user|
-    TopicAssigner.new(topic, assigning_user).assign(user)
+  DiscourseEvent.on(:assign_topic) do |topic, user, assigning_user, force|
+    if force || !topic.custom_fields[TopicAssigner::ASSIGNED_TO_ID]
+      TopicAssigner.new(topic, assigning_user).assign(user)
+    end
   end
 
   DiscourseEvent.on(:unassign_topic) do |topic, unassigning_user|
diff --git a/spec/integration/assign_spec.rb b/spec/integration/assign_spec.rb
index 373248b..d6e46bf 100644
--- a/spec/integration/assign_spec.rb
+++ b/spec/integration/assign_spec.rb
@@ -83,4 +83,23 @@ describe 'integration tests' do
       DiscourseEvent.trigger(:before_staff_flag_action, args)
     end
   end
+
+  describe "on assign_topic event" do
+    let(:post) { Fabricate(:post) }
+    let(:topic) { post.topic }
+    let(:admin) { Fabricate(:admin) }
+    let(:user1) { Fabricate(:user) }
+    let(:user2) { Fabricate(:user) }
+
+    it "assigns topic" do
+      DiscourseEvent.trigger(:assign_topic, topic, user1, admin)
+      expect(topic.reload.custom_fields[TopicAssigner::ASSIGNED_TO_ID].to_i).to eq(user1.id)
+
+      DiscourseEvent.trigger(:assign_topic, topic, user2, admin)
+      expect(topic.reload.custom_fields[TopicAssigner::ASSIGNED_TO_ID].to_i).to eq(user1.id)
+
+      DiscourseEvent.trigger(:assign_topic, topic, user2, admin, true)
+      expect(topic.reload.custom_fields[TopicAssigner::ASSIGNED_TO_ID].to_i).to eq(user2.id)
+    end
+  end
 end

GitHub sha: 801b1cd4

1 Like
Approved #2