FIX: Don't set notification level to `watching` when already watching.

FIX: Don’t set notification level to watching when already watching.

From ed1f942c525374959308e1e3b7f662d00aefead1 Mon Sep 17 00:00:00 2001
From: Guo Xiang Tan <tgx_world@hotmail.com>
Date: Fri, 7 Sep 2018 15:00:53 +0800
Subject: [PATCH] FIX: Don't set notification level to `watching` when already
 watching.


diff --git a/lib/topic_assigner.rb b/lib/topic_assigner.rb
index 9b262ff..39d99ee 100644
--- a/lib/topic_assigner.rb
+++ b/lib/topic_assigner.rb
@@ -149,13 +149,20 @@ SQL
 
     publish_topic_tracking_state(@topic, assign_to.id)
 
-    TopicUser.change(
-      assign_to.id,
-      @topic.id,
-      notification_level: TopicUser.notification_levels[:watching],
-      notifications_reason_id: TopicUser.notification_reasons[:plugin_changed]
+    if !TopicUser.exists?(
+      user_id: assign_to.id,
+      topic_id: @topic.id,
+      notification_level: TopicUser.notification_levels[:watching]
     )
 
+      TopicUser.change(
+        assign_to.id,
+        @topic.id,
+        notification_level: TopicUser.notification_levels[:watching],
+        notifications_reason_id: TopicUser.notification_reasons[:plugin_changed]
+      )
+    end
+
     if SiteSetting.assign_mailer_enabled
       if !@topic.muted?(assign_to)
         message = AssignMailer.send_assignment(assign_to.email, @topic, @assigned_by)
diff --git a/spec/lib/topic_assigner_spec.rb b/spec/lib/topic_assigner_spec.rb
index 30cde5b..9091d5e 100644
--- a/spec/lib/topic_assigner_spec.rb
+++ b/spec/lib/topic_assigner_spec.rb
@@ -49,6 +49,16 @@ RSpec.describe TopicAssigner do
         .to eq(TopicUser.notification_levels[:tracking])
     end
 
+    it 'does not update notification level if already watching' do
+      TopicUser.change(moderator.id, topic.id,
+        notification_level: TopicUser.notification_levels[:watching]
+      )
+
+      expect do
+        assigner.assign(moderator)
+      end.to_not change { TopicUser.last.notifications_reason_id }
+    end
+
     it 'does not update notification level if it is not set by the plugin' do
       assigner.assign(moderator)

GitHub

1 Like