FIX: Seed all categories and tags configured as defaults for nav menu (#22793)

Context of this change:

There are two site settings which an admin can configured to set the
default categories and tags that are shown for a new user. `default_navigation_menu_categories`
is used to determine the default categories while
`default_navigation_menu_tags` is used to determine the default tags.

Prior to this change when seeding the defaults, we will filter out the
categories/tags that the user do not have permission to see. However,
this means that when the user does eventually gain permission down the
line, the default categories and tags do not appear.

What does this change do?

With this commit, we have changed it such that all the categories and tags
configured in the `default_navigation_menu_categories` and
`default_navigation_menu_tags` site settings are seeded regardless of
whether the user's visibility of the categories or tags. During
serialization, we will then filter out the categories and tags which the
user does not have visibility of.
This commit is contained in:
Alan Guo Xiang Tan
2023-07-27 10:52:33 +08:00
committed by GitHub
parent f3db8579d6
commit 0a56274596
8 changed files with 117 additions and 160 deletions

View File

@@ -137,13 +137,6 @@ class User < ActiveRecord::Base
belongs_to :uploaded_avatar, class_name: "Upload"
has_many :sidebar_section_links, dependent: :delete_all
has_many :category_sidebar_section_links,
-> { where(linkable_type: "Category") },
class_name: "SidebarSectionLink"
has_many :custom_sidebar_tags,
through: :sidebar_section_links,
source: :linkable,
source_type: "Tag"
delegate :last_sent_email_address, to: :email_logs
@@ -170,11 +163,8 @@ class User < ActiveRecord::Base
after_create :ensure_in_trust_level_group
after_create :set_default_categories_preferences
after_create :set_default_tags_preferences
after_create :add_default_sidebar_section_links
after_update :update_default_sidebar_section_links, if: Proc.new { self.saved_change_to_admin? }
after_update :add_default_sidebar_section_links, if: Proc.new { self.saved_change_to_staged? }
after_create :set_default_sidebar_section_links
after_update :set_default_sidebar_section_links, if: Proc.new { self.saved_change_to_staged? }
after_update :trigger_user_updated_event,
if: Proc.new { self.human? && self.saved_change_to_uploaded_avatar_id? }
@@ -360,9 +350,22 @@ class User < ActiveRecord::Base
)
end
def secured_sidebar_category_ids(user_guardian = nil)
user_guardian ||= guardian
SidebarSectionLink.where(user_id: self.id, linkable_type: "Category").pluck(:linkable_id) &
user_guardian.allowed_category_ids
end
def visible_sidebar_tags(user_guardian = nil)
user_guardian ||= guardian
DiscourseTagging.filter_visible(custom_sidebar_tags, user_guardian)
DiscourseTagging.filter_visible(
Tag.where(
id: SidebarSectionLink.where(user_id: self.id, linkable_type: "Tag").select(:linkable_id),
),
user_guardian,
)
end
def self.max_password_length
@@ -2057,16 +2060,6 @@ class User < ActiveRecord::Base
if SiteSetting.default_navigation_menu_categories.present?
categories_to_update = SiteSetting.default_navigation_menu_categories.split("|")
if update
filtered_default_category_ids =
Category.secured(self.guardian).where(id: categories_to_update).pluck(:id)
existing_category_ids =
SidebarSectionLink.where(user: self, linkable_type: "Category").pluck(:linkable_id)
categories_to_update =
existing_category_ids + (filtered_default_category_ids & self.secure_category_ids)
end
SidebarSectionLinksUpdater.update_category_section_links(
self,
category_ids: categories_to_update,
@@ -2074,39 +2067,13 @@ class User < ActiveRecord::Base
end
if SiteSetting.tagging_enabled && SiteSetting.default_navigation_menu_tags.present?
tags_to_update = SiteSetting.default_navigation_menu_tags.split("|")
if update
default_tag_ids = Tag.where(name: tags_to_update).pluck(:id)
filtered_default_tags =
DiscourseTagging
.filter_visible(Tag, self.guardian)
.where(id: default_tag_ids)
.pluck(:name)
existing_tag_ids =
SidebarSectionLink.where(user: self, linkable_type: "Tag").pluck(:linkable_id)
existing_tags =
DiscourseTagging
.filter_visible(Tag, self.guardian)
.where(id: existing_tag_ids)
.pluck(:name)
tags_to_update = existing_tags + (filtered_default_tags & DiscourseTagging.hidden_tag_names)
end
SidebarSectionLinksUpdater.update_tag_section_links(self, tag_names: tags_to_update)
SidebarSectionLinksUpdater.update_tag_section_links(
self,
tag_ids: Tag.where(name: SiteSetting.default_navigation_menu_tags.split("|")).pluck(:id),
)
end
end
def add_default_sidebar_section_links
set_default_sidebar_section_links
end
def update_default_sidebar_section_links
set_default_sidebar_section_links(update: true)
end
def stat
user_stat || create_user_stat
end