discourse/app/serializers/basic_user_serializer.rb
Martin Brennan 72648dd576
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.
2021-05-06 09:14:07 +10:00

46 lines
1.1 KiB
Ruby

# frozen_string_literal: true
class BasicUserSerializer < ApplicationSerializer
attributes :id, :username, :name, :avatar_template
def name
Hash === user ? user[:name] : user.try(:name)
end
def include_name?
SiteSetting.enable_names?
end
def avatar_template
if Hash === object
User.avatar_template(user[:username], user[:uploaded_avatar_id])
else
user&.avatar_template
end
end
def user
object[:user] || object.try(:user) || object
end
def categories_with_notification_level(lookup_level)
category_user_notification_levels.select do |id, level|
level == CategoryUser.notification_levels[lookup_level]
end.keys
end
def category_user_notification_levels
@category_user_notification_levels ||= CategoryUser.notification_levels_for(scope)
end
def tags_with_notification_level(lookup_level)
tag_user_notification_levels.select do |id, level|
level == TagUser.notification_levels[lookup_level]
end.keys
end
def tag_user_notification_levels
@tag_user_notification_levels ||= TagUser.notification_levels_for(scope)
end
end