Skip to content

A platform for community discussion. Free, open, simple.

FIX: Respect user's manual topic notification level changes (#39037)

Régis Hanold69173c8b807
diff --git a/app/models/category_user.rb b/app/models/category_user.rb
index e4588f13218..3e166255cf6 100644
--- a/app/models/category_user.rb
+++ b/app/models/category_user.rb
@@ -40,7 +40,7 @@ class CategoryUser < ActiveRecord::Base
end
if category_ids.present?
- params = { user_id: user.id, level_num: level_num }
+ params = { user_id: user.id, level_num: }
sql = <<~SQL
INSERT INTO category_users (user_id, category_id, notification_level)
@@ -65,7 +65,7 @@ class CategoryUser < ActiveRecord::Base
end
def self.set_notification_level_for_category(user, level, category_id)
- record = CategoryUser.where(user: user, category_id: category_id).first
+ record = CategoryUser.where(user:, category_id:).first
return if record && record.notification_level == level
@@ -74,7 +74,7 @@ class CategoryUser < ActiveRecord::Base
record.save!
else
begin
- CategoryUser.create!(user: user, category_id: category_id, notification_level: level)
+ CategoryUser.create!(user:, category_id:, notification_level: level)
rescue ActiveRecord::RecordNotUnique
# does not matter
end
@@ -93,20 +93,21 @@ class CategoryUser < ActiveRecord::Base
/*where*/
SQL
- builder.where(
- "tu.topic_id = t.id AND
- cu.category_id = t.category_id AND
- cu.user_id = tu.user_id AND
- cu.notification_level = :tracking AND
- tu.notification_level = :regular",
- )
+ builder.where <<~SQL
+ tu.topic_id = t.id
+ AND cu.category_id = t.category_id
+ AND cu.user_id = tu.user_id
+ AND cu.notification_level = :tracking
+ AND tu.notification_level = :regular
+ AND COALESCE(tu.notifications_reason_id, 0) != :user_changed
+ SQL
if category_id = opts[:category_id]
- builder.where("t.category_id = :category_id", category_id: category_id)
+ builder.where("t.category_id = :category_id", category_id:)
end
if topic_id = opts[:topic_id]
- builder.where("tu.topic_id = :topic_id", topic_id: topic_id)
+ builder.where("tu.topic_id = :topic_id", topic_id:)
end
user_ids = opts[:user_ids] || opts[:user_id]
@@ -116,6 +117,7 @@ class CategoryUser < ActiveRecord::Base
tracking: notification_levels[:tracking],
regular: notification_levels[:regular],
auto_track_category: TopicUser.notification_reasons[:auto_track_category],
+ user_changed: TopicUser.notification_reasons[:user_changed],
)
end
@@ -135,15 +137,8 @@ class CategoryUser < ActiveRecord::Base
FROM (
SELECT tu1.topic_id,
tu1.user_id,
- CASE WHEN
- cu.user_id IS NULL AND tu1.notification_level = :watching AND tu1.notifications_reason_id = :auto_watch_category THEN true
- ELSE false
- END should_track,
- CASE WHEN
- cu.user_id IS NOT NULL AND tu1.notification_level in (:regular, :tracking) THEN true
- ELSE false
- END should_watch
-
+ CASE WHEN cu.user_id IS NULL AND tu1.notification_level = :watching AND tu1.notifications_reason_id = :auto_watch_category THEN true ELSE false END should_track,
+ CASE WHEN cu.user_id IS NOT NULL AND tu1.notification_level in (:regular, :tracking) THEN true ELSE false END should_watch
FROM topic_users tu1
JOIN topics t ON t.id = tu1.topic_id
LEFT JOIN category_users cu ON cu.category_id = t.category_id AND cu.user_id = tu1.user_id AND cu.notification_level = :watching
@@ -155,6 +150,7 @@ class CategoryUser < ActiveRecord::Base
builder.where("X.topic_id = tu.topic_id AND X.user_id = tu.user_id")
builder.where("should_watch OR should_track")
+ builder.where2("COALESCE(tu1.notifications_reason_id, 0) != :user_changed")
if category_id = opts[:category_id]
builder.where2("t.category_id = :category_id", category_id: category_id)
@@ -176,6 +172,7 @@ class CategoryUser < ActiveRecord::Base
tracking: notification_levels[:tracking],
regular: notification_levels[:regular],
auto_watch_category: TopicUser.notification_reasons[:auto_watch_category],
+ user_changed: TopicUser.notification_reasons[:user_changed],
)
end
diff --git a/app/models/tag_user.rb b/app/models/tag_user.rb
index 7b76fb7a73b..ac0843d1316 100644
--- a/app/models/tag_user.rb
+++ b/app/models/tag_user.rb
@@ -112,18 +112,18 @@ class TagUser < ActiveRecord::Base
tag_id = tag_id.to_i
user_id = user_id.to_i
- tag_user = TagUser.where(user_id: user_id, tag_id: tag_id).first
+ tag_user = TagUser.where(user_id:, tag_id:).first
if tag_user
return tag_user if tag_user.notification_level == level
tag_user.notification_level = level
tag_user.save
else
- tag_user = TagUser.create(user_id: user_id, tag_id: tag_id, notification_level: level)
+ tag_user = TagUser.create(user_id:, tag_id:, notification_level: level)
end
- auto_watch(user_id: user_id)
- auto_track(user_id: user_id)
+ auto_watch(user_id:)
+ auto_track(user_id:)
tag_user
rescue ActiveRecord::RecordNotUnique
@@ -133,42 +133,29 @@ class TagUser < ActiveRecord::Base
def self.auto_watch(opts)
builder = DB.build <<~SQL
UPDATE topic_users
- SET notification_level = CASE WHEN should_watch THEN :watching ELSE :tracking END,
- notifications_reason_id = CASE WHEN should_watch THEN :auto_watch_tag ELSE NULL END
- FROM
- (
- SELECT tu.topic_id, tu.user_id, CASE
- WHEN MAX(tag_users.notification_level) = :watching THEN true
- ELSE false
- END
- should_watch,
-
- CASE WHEN MAX(tag_users.notification_level) IS NULL AND
- tu.notification_level = :watching AND
- tu.notifications_reason_id = :auto_watch_tag
- THEN true
- ELSE false
- END
- should_track
-
- FROM topic_users tu
- LEFT JOIN topic_tags ON tu.topic_id = topic_tags.topic_id
- LEFT JOIN tag_users ON tag_users.user_id = tu.user_id
- AND topic_tags.tag_id = tag_users.tag_id
- AND tag_users.notification_level = :watching
- /*where*/
- GROUP BY tu.topic_id, tu.user_id, tu.notification_level, tu.notifications_reason_id
- ) AS X
- WHERE X.topic_id = topic_users.topic_id AND
- X.user_id = topic_users.user_id AND
- (should_track OR should_watch)
-
+ SET notification_level = CASE WHEN should_watch THEN :watching ELSE :tracking END
+ , notifications_reason_id = CASE WHEN should_watch THEN :auto_watch_tag ELSE NULL END
+ FROM (
+ SELECT tu.topic_id
+ , tu.user_id
+ , CASE WHEN MAX(tag_users.notification_level) = :watching THEN true ELSE false END should_watch
+ , CASE WHEN MAX(tag_users.notification_level) IS NULL AND tu.notification_level = :watching AND tu.notifications_reason_id = :auto_watch_tag THEN true ELSE false END should_track
+ FROM topic_users tu
+ LEFT JOIN topic_tags ON tu.topic_id = topic_tags.topic_id
+ LEFT JOIN tag_users ON tag_users.user_id = tu.user_id AND topic_tags.tag_id = tag_users.tag_id AND tag_users.notification_level = :watching
+ /*where*/
+ GROUP BY tu.topic_id, tu.user_id, tu.notification_level, tu.notifications_reason_id
+ ) AS X
+ WHERE X.topic_id = topic_users.topic_id
+ AND X.user_id = topic_users.user_id
+ AND (should_track OR should_watch)
SQL
- builder.where("tu.notification_level in (:tracking, :regular, :watching)")
+ builder.where("tu.notification_level IN (:tracking, :regular, :watching)")
+ builder.where("COALESCE(tu.notifications_reason_id, 0) != :user_changed")
if topic_id = opts[:topic_id]
- builder.where("tu.topic_id = :topic_id", topic_id: topic_id)
+ builder.where("tu.topic_id = :topic_id", topic_id:)
end
user_ids = opts[:user_ids] || opts[:user_id]
@@ -179,30 +166,31 @@ class TagUser < ActiveRecord::Base
tracking: notification_levels[:tracking],
regular: notification_levels[:regular],
auto_watch_tag: TopicUser.notification_reasons[:auto_watch_tag],
+ user_changed: TopicUser.notification_reasons[:user_changed],
)
end
def self.auto_track(opts)
builder = DB.build <<~SQL
UPDATE topic_users
- SET notification_level = :tracking, notifications_reason_id = :auto_track_tag
- FROM (
- SELECT DISTINCT tu.topic_id, tu.user_id
- FROM topic_users tu
- JOIN topic_tags ON tu.topic_id = topic_tags.topic_id
- JOIN tag_users ON tag_users.user_id = tu.user_id
- AND topic_tags.tag_id = tag_users.tag_id
- AND tag_users.notification_level = :tracking
- /*where*/
- ) as X
- WHERE
- topic_users.notification_level = :regular AND
- topic_users.topic_id = X.topic_id AND
- topic_users.user_id = X.user_id
+ SET notification_level = :tracking
+ , notifications_reason_id = :auto_track_tag
+ FROM (
+ SELECT DISTINCT tu.topic_id
+ , tu.user_id
+ FROM topic_users tu
+ JOIN topic_tags ON tu.topic_id = topic_tags.topic_id
+ JOIN tag_users ON tag_users.user_id = tu.user_id AND topic_tags.tag_id = tag_users.tag_id AND tag_users.notification_level = :tracking
+ /*where*/
+ ) AS X
+ WHERE topic_users.notification_level = :regular
+ AND COALESCE(topic_users.notifications_reason_id, 0) != :user_changed
+ AND topic_users.topic_id = X.topic_id
+ AND topic_users.user_id = X.user_id
SQL
if topic_id = opts[:topic_id]
- builder.where("tu.topic_id = :topic_id", topic_id: topic_id)
+ builder.where("tu.topic_id = :topic_id", topic_id:)
end
user_ids = opts[:user_ids] || opts[:user_id]
@@ -212,6 +200,7 @@ class TagUser < ActiveRecord::Base
tracking: notification_levels[:tracking],
regular: notification_levels[:regular],
auto_track_tag: TopicUser.notification_reasons[:auto_track_tag],
+ user_changed: TopicUser.notification_reasons[:user_changed],
)
end
diff --git a/app/models/topic_user.rb b/app/models/topic_user.rb
index b12ac36dc5e..7fb815aa13b 100644
--- a/app/models/topic_user.rb
+++ b/app/models/topic_user.rb
@@ -84,32 +84,6 @@ class TopicUser < ActiveRecord::Base
)
end
- def unwatch_categories!(user, category_ids)
- track_threshold = user.user_option.auto_track_topics_after_msecs
-
- sql = <<~SQL
- UPDATE topic_users tu
- SET notification_level = CASE
- WHEN t.user_id = :user_id THEN :watching
- WHEN total_msecs_viewed > :track_threshold AND :track_threshold >= 0 THEN :tracking
- ELSE :regular
- end
- FROM topics t
- WHERE t.id = tu.topic_id AND tu.notification_level <> :muted AND category_id IN (:category_ids) AND tu.user_id = :user_id
- SQL
-
- DB.exec(
- sql,
- watching: notification_levels[:watching],
- tracking: notification_levels[:tracking],
- regular: notification_levels[:regular],
- muted: notification_levels[:muted],
- category_ids: category_ids,
- user_id: user.id,
- track_threshold: track_threshold,
- )
- end
-
# Find the information specific to a user in a forum topic
def lookup_for(user, topics)
# If the user isn't logged in, there's no last read posts
diff --git a/spec/models/category_user_spec.rb b/spec/models/category_user_spec.rb
index 5484bd60b21..6428eb8df95 100644
--- a/spec/models/category_user_spec.rb
+++ b/spec/models/category_user_spec.rb
@@ -4,6 +4,10 @@
RSpec.describe CategoryUser do
fab!(:user)
+ def watching
+ CategoryUser.notification_levels[:watching]
+ end
+
def tracking
CategoryUser.notification_levels[:tracking]
end
@@ -12,6 +16,10 @@ RSpec.describe CategoryUser do
CategoryUser.notification_levels[:regular]
end
+ def muted
+ CategoryUser.notification_levels[:muted]
+ end
+
describe "#batch_set" do
fab!(:category)
@@ -272,6 +280,48 @@ RSpec.describe CategoryUser do
).to eq TopicUser.notification_levels[:tracking]
end
+ it "does not override user's manual topic notification level changes when auto_track runs" do
+ tracked_category = Fabricate(:category)
+ other_category = Fabricate(:category)
+
+ CategoryUser.set_notification_level_for_category(user, tracking, tracked_category.id)
+
+ post = create_post(category: tracked_category)
+ TopicUser.change(user.id, post.topic_id, total_msecs_viewed: 10)
+ expect(TopicUser.get(post.topic, user).notification_level).to eq(tracking)
+
+ TopicUser.change(user.id, post.topic_id, notification_level: regular)
+ tu = TopicUser.get(post.topic, user)
+ expect(tu.notification_level).to eq(regular)
+ expect(tu.notifications_reason_id).to eq(TopicUser.notification_reasons[:user_changed])
+
+ # muting another category triggers auto_track/auto_watch for the user
+ CategoryUser.set_notification_level_for_category(user, muted, other_category.id)
+
+ expect(TopicUser.get(post.topic, user).notification_level).to eq(regular)
+ end
+
+ it "does not override user's manual topic notification level changes when auto_watch runs" do
+ watched_category = Fabricate(:category)
+ other_category = Fabricate(:category)
+
+ CategoryUser.set_notification_level_for_category(user, watching, watched_category.id)
+
+ post = create_post(category: watched_category)
+ TopicUser.change(user.id, post.topic_id, total_msecs_viewed: 10)
+ expect(TopicUser.get(post.topic, user).notification_level).to eq(watching)
+
+ TopicUser.change(user.id, post.topic_id, notification_level: tracking)
+ tu = TopicUser.get(post.topic, user)
+ expect(tu.notification_level).to eq(tracking)
+ expect(tu.notifications_reason_id).to eq(TopicUser.notification_reasons[:user_changed])
+
+ # muting another category triggers auto_track/auto_watch for the user
+ CategoryUser.set_notification_level_for_category(user, muted, other_category.id)
+
+ expect(TopicUser.get(post.topic, user).notification_level).to eq(tracking)
+ end
+
it "is destroyed when a user is deleted" do
category = Fabricate(:category)
diff --git a/spec/models/tag_user_spec.rb b/spec/models/tag_user_spec.rb
index d413811af33..951d658a83d 100644
--- a/spec/models/tag_user_spec.rb
+++ b/spec/models/tag_user_spec.rb
@@ -338,6 +338,66 @@ RSpec.describe TagUser do
expect(topic.tags.length).to eq(2)
end
+ it "does not override user's manual topic notification level changes when auto_track runs" do
+ tracked_tag = Fabricate(:tag)
+ other_tag = Fabricate(:tag)
+ post = create_post(tags: [tracked_tag.name])
+
+ TopicUser.change(user.id, post.topic_id, total_msecs_viewed: 1)
+ TagUser.change(user.id, tracked_tag.id, tracking)
+
+ tu = TopicUser.get(post.topic, user)
+ expect(tu.notification_level).to eq tracking
+
+ # user manually sets topic to regular
+ TopicUser.change(
+ user.id,
+ post.topic_id,
+ notification_level: TopicUser.notification_levels[:regular],
+ )
+ tu = TopicUser.get(post.topic, user)
+ expect(tu.notification_level).to eq regular
+ expect(tu.notifications_reason_id).to eq TopicUser.notification_reasons[:user_changed]
+
+ # changing another tag triggers auto_track/auto_watch for the user
+ TagUser.change(user.id, other_tag.id, watching)
+
+ tu = TopicUser.get(post.topic, user)
+ expect(tu.notification_level).to eq regular
+ end
+
+ it "does not override user's manual topic notification level changes when auto_watch runs" do
+ post = create_post(tags: [watched_tag.name])
+ other_tag = Fabricate(:tag)
+
+ TopicUser.change(user.id, post.topic_id, total_msecs_viewed: 1)
+ TagUser.create!(
+ user: user,
+ tag: watched_tag,
+ notification_level: TagUser.notification_levels[:watching],
+ )
+ TagUser.auto_watch(user_id: user.id)
+
+ tu = TopicUser.get(post.topic, user)
+ expect(tu.notification_level).to eq watching
+
+ # user manually sets topic to tracking
+ TopicUser.change(
+ user.id,
+ post.topic_id,
+ notification_level: TopicUser.notification_levels[:tracking],
+ )
+ tu = TopicUser.get(post.topic, user)
+ expect(tu.notification_level).to eq tracking
+ expect(tu.notifications_reason_id).to eq TopicUser.notification_reasons[:user_changed]
+
+ # changing another tag triggers auto_track/auto_watch for the user
+ TagUser.change(user.id, other_tag.id, watching)
+
+ tu = TopicUser.get(post.topic, user)
+ expect(tu.notification_level).to eq tracking
+ end
+
it "is destroyed when a user is deleted" do
TagUser.create!(
user: user,
diff --git a/spec/models/topic_user_spec.rb b/spec/models/topic_user_spec.rb
index 19c44967603..c69901be854 100644
--- a/spec/models/topic_user_spec.rb
+++ b/spec/models/topic_user_spec.rb
@@ -13,34 +13,6 @@ RSpec.describe TopicUser do
TopicUser.notification_levels[:tracking]
end
- describe "#unwatch_categories!" do
- it "correctly unwatches categories" do
- op_topic = Fabricate(:topic)
- another_topic = Fabricate(:topic)
- tracked_topic = Fabricate(:topic)
-
- user = op_topic.user
-
- TopicUser.change(user.id, op_topic, notification_level: watching)
- TopicUser.change(user.id, another_topic, notification_level: watching)
- TopicUser.change(
- user.id,
- tracked_topic,
- notification_level: watching,
- total_msecs_viewed: SiteSetting.default_other_auto_track_topics_after_msecs + 1,
- )
-
- TopicUser.unwatch_categories!(user, [Fabricate(:category).id, Fabricate(:category).id])
- expect(TopicUser.get(another_topic, user).notification_level).to eq(watching)
-
- TopicUser.unwatch_categories!(user, [op_topic.category_id])
-
- expect(TopicUser.get(op_topic, user).notification_level).to eq(watching)
- expect(TopicUser.get(another_topic, user).notification_level).to eq(regular)
- expect(TopicUser.get(tracked_topic, user).notification_level).to eq(tracking)
- end
- end
-
describe "#notification_levels" do
context "when verifying enum sequence" do
let(:notification_levels) { TopicUser.notification_levels }