FEATURE: default sidebar categories and tags (#17939)

Ability to set up default sidebar categories and tags for authenticated users
This commit is contained in:
Krzysztof Kotlarek 2022-08-22 08:46:20 +10:00 committed by GitHub
parent 49fa2e93c2
commit caab916569
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 12 deletions

View File

@ -109,7 +109,7 @@ class User < ActiveRecord::Base
has_many :sidebar_section_links, dependent: :delete_all
has_many :category_sidebar_section_links, -> { where(linkable_type: "Category") }, class_name: 'SidebarSectionLink'
has_many :sidebar_tags, through: :sidebar_section_links, source: :linkable, source_type: "Tag"
has_many :custom_sidebar_tags, through: :sidebar_section_links, source: :linkable, source_type: "Tag"
delegate :last_sent_email_address, to: :email_logs
@ -1660,6 +1660,14 @@ class User < ActiveRecord::Base
Discourse.redis.del("#{REDESIGN_USER_MENU_REDIS_KEY_PREFIX}#{self.id}")
end
def sidebar_categories_ids
categories_ids = category_sidebar_section_links.pluck(:linkable_id)
if categories_ids.blank? && SiteSetting.default_sidebar_categories.present?
return guardian.allowed_category_ids & SiteSetting.default_sidebar_categories.split("|").map(&:to_i)
end
categories_ids
end
protected
def badge_grant
@ -1952,6 +1960,15 @@ class User < ActiveRecord::Base
end
end
def sidebar_tags
return custom_sidebar_tags if custom_sidebar_tags.present?
if SiteSetting.default_sidebar_tags.present?
tag_names = SiteSetting.default_sidebar_tags.split("|") - DiscourseTagging.hidden_tag_names(guardian)
return Tag.where(name: tag_names)
end
[]
end
def self.ensure_consistency!
DB.exec <<~SQL
UPDATE users

View File

@ -76,7 +76,6 @@ class CurrentUserSerializer < BasicUserSerializer
:pending_posts_count,
:status,
:sidebar_category_ids,
:sidebar_tags,
:likes_notifications_disabled,
:grouped_unread_high_priority_notifications,
:redesigned_user_menu_enabled
@ -309,7 +308,7 @@ class CurrentUserSerializer < BasicUserSerializer
end
def sidebar_category_ids
object.category_sidebar_section_links.pluck(:linkable_id)
object.sidebar_categories_ids
end
def include_sidebar_category_ids?

View File

@ -2380,6 +2380,8 @@ en:
suggest_weekends_in_date_pickers: "Include weekends (Saturday and Sunday) in date picker suggestions (disable this if you use Discourse only on weekdays, Monday through Friday)."
splash_screen: "Displays a temporary loading screen while site assets load"
default_sidebar_categories: "Selected categories will be displayed under Sidebar's Categories section by default."
default_sidebar_tags: "Selected tags will be displayed under Sidebar's Tags section by default."
errors:
invalid_css_color: "Invalid color. Enter a color name or hex value."

View File

@ -1994,6 +1994,14 @@ developer:
enable_sidebar:
default: true
client: true
default_sidebar_categories:
type: category_list
default: ""
client: true
default_sidebar_tags:
type: tag_list
default: ""
client: true
embedding:
embed_by_username:

View File

@ -221,10 +221,15 @@ RSpec.describe CurrentUserSerializer do
end
describe '#sidebar_tags' do
fab!(:tag_sidebar_section_link) { Fabricate(:tag_sidebar_section_link, user: user) }
fab!(:tag_sidebar_section_link_2) { Fabricate(:tag_sidebar_section_link, user: user) }
fab!(:tag_1) { Fabricate(:tag, name: "foo") }
fab!(:tag_2) { Fabricate(:tag, name: "bar") }
fab!(:hidden_tag) { Fabricate(:tag, name: "secret") }
fab!(:staff_tag_group) { Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: ["secret"]) }
let(:tag_sidebar_section_link) { Fabricate(:tag_sidebar_section_link, user: user) }
let(:tag_sidebar_section_link_2) { Fabricate(:tag_sidebar_section_link, user: user) }
it "is not included when experimental sidebar has not been enabled" do
tag_sidebar_section_link
SiteSetting.enable_experimental_sidebar_hamburger = false
SiteSetting.tagging_enabled = true
@ -234,6 +239,7 @@ RSpec.describe CurrentUserSerializer do
end
it "is not included when tagging has not been enabled" do
tag_sidebar_section_link
SiteSetting.enable_experimental_sidebar_hamburger = true
SiteSetting.tagging_enabled = false
@ -243,6 +249,7 @@ RSpec.describe CurrentUserSerializer do
end
it "is present when experimental sidebar and tagging has been enabled" do
tag_sidebar_section_link
SiteSetting.enable_experimental_sidebar_hamburger = true
SiteSetting.tagging_enabled = true
@ -255,13 +262,43 @@ RSpec.describe CurrentUserSerializer do
{ name: tag_sidebar_section_link_2.linkable.name, pm_only: true }
)
end
it 'includes visible default sidebar tags' do
SiteSetting.enable_experimental_sidebar_hamburger = true
SiteSetting.tagging_enabled = true
SiteSetting.default_sidebar_tags = "foo|bar|secret"
json = serializer.as_json
expect(json[:sidebar_tags]).to eq([
{ name: "foo", pm_only: false },
{ name: "bar", pm_only: false }
])
end
it 'includes tags choosen by user' do
SiteSetting.enable_experimental_sidebar_hamburger = true
SiteSetting.tagging_enabled = true
SiteSetting.default_sidebar_tags = "foo|bar|secret"
tag_sidebar_section_link = Fabricate(:tag_sidebar_section_link, user: user)
json = serializer.as_json
expect(json[:sidebar_tags]).to eq([
{ name: tag_sidebar_section_link.linkable.name, pm_only: false }
])
end
end
describe '#sidebar_category_ids' do
fab!(:category_sidebar_section_link) { Fabricate(:category_sidebar_section_link, user: user) }
fab!(:category_sidebar_section_link_2) { Fabricate(:category_sidebar_section_link, user: user) }
fab!(:category) { Fabricate(:category) }
fab!(:category_2) { Fabricate(:category) }
fab!(:private_category) { Fabricate(:private_category, group: Fabricate(:group)) }
let(:category_sidebar_section_link) { Fabricate(:category_sidebar_section_link, user: user) }
let(:category_sidebar_section_link_2) { Fabricate(:category_sidebar_section_link, user: user) }
it "is not included when SiteSeting.enable_experimental_sidebar_hamburger is false" do
category_sidebar_section_link
SiteSetting.enable_experimental_sidebar_hamburger = false
json = serializer.as_json
@ -270,6 +307,7 @@ RSpec.describe CurrentUserSerializer do
end
it "is not included when experimental sidebar has not been enabled" do
category_sidebar_section_link
SiteSetting.enable_experimental_sidebar_hamburger = false
json = serializer.as_json
@ -277,15 +315,23 @@ RSpec.describe CurrentUserSerializer do
expect(json[:sidebar_category_ids]).to eq(nil)
end
it "is present when experimental sidebar has been enabled" do
it 'includes visible default sidebar categories' do
SiteSetting.enable_experimental_sidebar_hamburger = true
SiteSetting.default_sidebar_categories = "#{category.id}|#{category_2.id}|#{private_category.id}"
json = serializer.as_json
expect(json[:sidebar_category_ids]).to eq([category.id, category_2.id])
end
expect(json[:sidebar_category_ids]).to contain_exactly(
category_sidebar_section_link.linkable_id,
category_sidebar_section_link_2.linkable_id
)
it 'includes categories choosen by user' do
SiteSetting.enable_experimental_sidebar_hamburger = true
SiteSetting.default_sidebar_categories = "#{category.id}|#{category_2.id}|#{private_category.id}"
category_sidebar_section_link
category_sidebar_section_link_2
json = serializer.as_json
expect(json[:sidebar_category_ids]).to eq([category_sidebar_section_link.linkable.id, category_sidebar_section_link_2.linkable.id])
end
end