FIX: Base topic details message on current category and tag tracking state (#12937)

The user may have changed their category or tag tracking settings since a topic was tracked/watched based on those settings in the past. In that case we need to alter the reason message we show them otherwise it is very confusing for the end user to be told they are tracking a topic because of a category, when they are no longer tracking that category.

For example: "You will see a count of new replies because you are tracking this category." becomes: "You will see a count of new replies because you were tracking this category in the past."

To do this, it was necessary to add tag and category tracking info to current user serializer. I improved the serializer code so it only does 3 SQL queries instead of 9 to get the tracking information for tags and categories for the current user.
This commit is contained in:
Martin Brennan
2021-05-06 09:14:07 +10:00
committed by GitHub
parent c792c2b5fe
commit 72648dd576
14 changed files with 530 additions and 64 deletions

View File

@ -1546,17 +1546,27 @@ class User < ActiveRecord::Base
values = []
%w{watching watching_first_post tracking regular muted}.each do |s|
category_ids = SiteSetting.get("default_categories_#{s}").split("|").map(&:to_i)
# The following site settings are used to pre-populate default category
# tracking settings for a user:
#
# * default_categories_watching
# * default_categories_tracking
# * default_categories_watching_first_post
# * default_categories_regular
# * default_categories_muted
%w{watching watching_first_post tracking regular muted}.each do |setting|
category_ids = SiteSetting.get("default_categories_#{setting}").split("|").map(&:to_i)
category_ids.each do |category_id|
next if category_id == 0
values << "(#{self.id}, #{category_id}, #{CategoryUser.notification_levels[s.to_sym]})"
values << {
user_id: self.id,
category_id: category_id,
notification_level: CategoryUser.notification_levels[setting.to_sym]
}
end
end
if values.present?
DB.exec("INSERT INTO category_users (user_id, category_id, notification_level) VALUES #{values.join(",")}")
end
CategoryUser.insert_all!(values) if values.present?
end
def set_default_tags_preferences
@ -1564,12 +1574,25 @@ class User < ActiveRecord::Base
values = []
%w{watching watching_first_post tracking muted}.each do |s|
tag_names = SiteSetting.get("default_tags_#{s}").split("|")
# The following site settings are used to pre-populate default tag
# tracking settings for a user:
#
# * default_tags_watching
# * default_tags_tracking
# * default_tags_watching_first_post
# * default_tags_muted
%w{watching watching_first_post tracking muted}.each do |setting|
tag_names = SiteSetting.get("default_tags_#{setting}").split("|")
now = Time.zone.now
Tag.where(name: tag_names).pluck(:id).each do |tag_id|
values << { user_id: self.id, tag_id: tag_id, notification_level: TagUser.notification_levels[s.to_sym], created_at: now, updated_at: now }
values << {
user_id: self.id,
tag_id: tag_id,
notification_level: TagUser.notification_levels[setting.to_sym],
created_at: now,
updated_at: now
}
end
end