FEATURE: Assigning a user to a topic will make user watch the topic.

FEATURE: Assigning a user to a topic will make user watch the topic.

From 9bea97119627c3e39b58c5e4a73dd7c6113590d6 Mon Sep 17 00:00:00 2001
From: Guo Xiang Tan <tgx_world@hotmail.com>
Date: Fri, 3 Aug 2018 16:24:59 +0800
Subject: [PATCH] FEATURE: Assigning a user to a topic will make user watch the
 topic.


diff --git a/lib/topic_assigner.rb b/lib/topic_assigner.rb
index b172b89..cf4919a 100644
--- a/lib/topic_assigner.rb
+++ b/lib/topic_assigner.rb
@@ -144,6 +144,13 @@ 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 SiteSetting.assign_mailer_enabled
       if !@topic.muted?(assign_to)
         message = AssignMailer.send_assignment(assign_to.email, @topic, @assigned_by)
@@ -201,6 +208,13 @@ SQL
 
       post.publish_change_to_clients!(:revised, reload_topic: true)
 
+      TopicUser.change(
+        assigned_to_id,
+        @topic.id,
+        notification_level: TopicUser.notification_levels[:tracking],
+        notifications_reason_id: TopicUser.notification_reasons[:plugin_changed]
+      )
+
       assigned_user = User.find_by(id: assigned_to_id)
       MessageBus.publish(
         "/staff/topic-assignment",
diff --git a/spec/lib/topic_assigner_spec.rb b/spec/lib/topic_assigner_spec.rb
index bac7f53..d4a9791 100644
--- a/spec/lib/topic_assigner_spec.rb
+++ b/spec/lib/topic_assigner_spec.rb
@@ -31,9 +31,22 @@ RSpec.describe TopicAssigner do
 
     it "can assign and unassign correctly" do
       assigner.assign(moderator)
-      expect(TopicQuery.new(moderator, assigned: moderator.username).list_latest.topics).to be_present
+
+      expect(TopicQuery.new(
+        moderator, assigned: moderator.username
+      ).list_latest.topics).to eq([topic])
+
+      expect(TopicUser.find_by(user: moderator).notification_level)
+        .to eq(TopicUser.notification_levels[:watching])
+
       assigner.unassign
-      expect(TopicQuery.new(moderator, assigned: moderator.username).list_latest.topics).to be_blank
+
+      expect(TopicQuery.new(
+        moderator, assigned: moderator.username
+      ).list_latest.topics).to eq([])
+
+      expect(TopicUser.find_by(user: moderator).notification_level)
+        .to eq(TopicUser.notification_levels[:tracking])
     end
 
     it "can unassign all a user's topics at once" do

GitHub

2 Likes