From 1d160702adfc0bc7d34e051dfaaed1c948c4ccf5 Mon Sep 17 00:00:00 2001 From: Bianca Nenciu Date: Fri, 2 Feb 2024 10:35:15 +0200 Subject: [PATCH] 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. --- app/models/site.rb | 19 +++++++++++---- app/serializers/site_serializer.rb | 2 +- spec/serializers/site_serializer_spec.rb | 30 ++++++++++++++++++++---- 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/app/models/site.rb b/app/models/site.rb index 2c467001703..c46c666e0b4 100644 --- a/app/models/site.rb +++ b/app/models/site.rb @@ -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 diff --git a/app/serializers/site_serializer.rb b/app/serializers/site_serializer.rb index 0d429b3e007..9108ad3bbb9 100644 --- a/app/serializers/site_serializer.rb +++ b/app/serializers/site_serializer.rb @@ -240,7 +240,7 @@ class SiteSerializer < ApplicationSerializer end def include_categories? - !scope.can_lazy_load_categories? + object.categories.present? end def markdown_additional_options diff --git a/spec/serializers/site_serializer_spec.rb b/spec/serializers/site_serializer_spec.rb index 451072f4b93..38019a3a6e8 100644 --- a/spec/serializers/site_serializer_spec.rb +++ b/spec/serializers/site_serializer_spec.rb @@ -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