DEV: Sidebar default tags and categories are determined at user creation (#18620)

The previous sidebar default tags and categories implementation did not
allow for a user to configure their sidebar to have no categories or
tags. This commit changes how the defaults are applied. When a user is being created,
we create the SidebarSectionLink records based on the `default_sidebar_categories` and
`default_sidebar_tags` site settings. SidebarSectionLink records are
only created for categories and tags which the user has visibility on at
the point of user creation.

With this change, we're also adding the ability for admins to apply
changes to the `default_sidebar_categories` and `default_sidebar_tags`
site settings historically when changing their site setting. When a new
category/tag has been added to the default, the new category/tag will be
added to the sidebar for all users if the admin elects to apply the changes historically.
Like wise when a tag/category is removed, the tag/category will be
removed from the sidebar for all users if the admin elects to apply the
changes historically.

Internal Ref: /t/73500
This commit is contained in:
Alan Guo Xiang Tan
2022-10-27 06:38:50 +08:00
committed by GitHub
parent a473e352de
commit 1b56a55f50
14 changed files with 637 additions and 107 deletions

View File

@ -116,6 +116,44 @@ RSpec.describe Admin::SiteSettingsController do
end
end
context 'when updating default sidebar categories and tags' do
it 'does not enqueue the backfilling job if update_existing_user param is not present' do
expect_not_enqueued_with(job: :backfill_sidebar_site_settings) do
put "/admin/site_settings/default_sidebar_categories.json", params: {
default_sidebar_categories: "1|2",
}
expect(response.status).to eq(200)
end
end
it 'enqueus the backfilling job if update_existing_user param is present when updating default sidebar tags' do
SiteSetting.default_sidebar_tags = "tag3"
expect_enqueued_with(job: :backfill_sidebar_site_settings, args: { setting_name: 'default_sidebar_tags', new_value: 'tag1|tag2', previous_value: 'tag3' }) do
put "/admin/site_settings/default_sidebar_tags.json", params: {
default_sidebar_tags: "tag1|tag2",
update_existing_user: true
}
expect(response.status).to eq(200)
end
end
it 'enqueus the backfilling job if update_existing_user param is present when updating default sidebar categories' do
SiteSetting.default_sidebar_categories = "3|4"
expect_enqueued_with(job: :backfill_sidebar_site_settings, args: { setting_name: 'default_sidebar_categories', new_value: '1|2', previous_value: '3|4' }) do
put "/admin/site_settings/default_sidebar_categories.json", params: {
default_sidebar_categories: "1|2",
update_existing_user: true
}
expect(response.status).to eq(200)
end
end
end
describe 'default categories' do
fab!(:user1) { Fabricate(:user) }
fab!(:user2) { Fabricate(:user) }
@ -265,6 +303,30 @@ RSpec.describe Admin::SiteSettingsController do
SiteSetting.setting(:default_tags_watching, "")
end
context "for sidebar defaults" do
it 'returns the right count for the default_sidebar_categories site setting' do
category = Fabricate(:category)
put "/admin/site_settings/default_sidebar_categories/user_count.json", params: {
default_sidebar_categories: "#{category.id}"
}
expect(response.status).to eq(200)
expect(response.parsed_body["user_count"]).to eq(User.real.not_staged.count)
end
it 'returns the right count for the default_sidebar_tags site setting' do
tag = Fabricate(:tag)
put "/admin/site_settings/default_sidebar_tags/user_count.json", params: {
default_sidebar_tags: "#{tag.name}"
}
expect(response.status).to eq(200)
expect(response.parsed_body["user_count"]).to eq(User.real.not_staged.count)
end
end
context "with user options" do
def expect_user_count(site_setting_name:, user_setting_name:, current_site_setting_value:, new_site_setting_value:,
current_user_setting_value: nil, new_user_setting_value: nil)