FIX: Preload sidebar categories when lazy loading categories (#25332)

This fixes a bug where the sidebar categories would not be loaded when
the categories were lazy loaded because the sidebar uses the preloaded
category list, which was empty.
This commit is contained in:
Bianca Nenciu 2024-02-02 10:35:15 +02:00 committed by GitHub
parent e071b74a79
commit 1d160702ad
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 9 deletions

View File

@ -106,15 +106,26 @@ class Site
end
def categories
if @guardian.can_lazy_load_categories?
preloaded_category_ids = []
if @guardian.authenticated?
preloaded_category_ids.concat(@guardian.user.secured_sidebar_category_ids(@guardian))
end
end
@categories ||=
begin
categories = []
self.class.all_categories_cache.each do |category|
if @guardian.can_see_serialized_category?(
category_id: category[:id],
read_restricted: category[:read_restricted],
)
if (
!@guardian.can_lazy_load_categories? ||
preloaded_category_ids.include?(category[:id])
) &&
@guardian.can_see_serialized_category?(
category_id: category[:id],
read_restricted: category[:read_restricted],
)
categories << category
end
end

View File

@ -240,7 +240,7 @@ class SiteSerializer < ApplicationSerializer
end
def include_categories?
!scope.can_lazy_load_categories?
object.categories.present?
end
def markdown_additional_options

View File

@ -131,12 +131,34 @@ RSpec.describe SiteSerializer do
expect(serialized[:shared_drafts_category_id]).to eq(nil)
end
it "does not include categories if lazy_load_categories" do
SiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:everyone]}"
context "with lazy loaded categories enabled" do
fab!(:user)
let(:guardian) { Guardian.new(user) }
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
before { SiteSetting.lazy_load_categories_groups = "#{Group::AUTO_GROUPS[:everyone]}" }
expect(serialized[:categories]).to eq(nil)
it "categories does not include any categories for anonymous users" do
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
expect(serialized[:categories]).to eq(nil)
end
it "categories include only sidebar categories" do
Fabricate(:category_sidebar_section_link, linkable: category, user: user)
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
expect(serialized[:categories].map { |c| c[:id] }).to contain_exactly(category.id)
end
it "categories include only visible sidebar categories" do
Fabricate(:category_sidebar_section_link, linkable: category, user: user)
category.update!(read_restricted: true)
serialized = described_class.new(Site.new(guardian), scope: guardian, root: false).as_json
expect(serialized[:categories]).to eq(nil)
end
end
describe "#anonymous_default_navigation_menu_tags" do