Skip to content

A platform for community discussion. Free, open, simple.

FIX: Invalidate anonymous /site.json cache when public sidebar sections are made p... (#43954)

Penar Musaraj67c4c3c5315a
diff --git a/app/controllers/sidebar_sections_controller.rb b/app/controllers/sidebar_sections_controller.rb
index 0d90e6ee5da..44fda45f0c5 100644
--- a/app/controllers/sidebar_sections_controller.rb
+++ b/app/controllers/sidebar_sections_controller.rb
@@ -88,6 +88,7 @@ class SidebarSectionsController < ApplicationController
case sidebar_section.section_type
when "community"
sidebar_section.reset_community!
+ Site.clear_anon_cache!
end
render_serialized(sidebar_section, SidebarSectionSerializer)
@@ -157,6 +158,7 @@ class SidebarSectionsController < ApplicationController
if sidebar_section.public?
StaffActionLogger.new(current_user).log_destroy_public_sidebar_section(sidebar_section)
MessageBus.publish("/refresh-sidebar-sections", nil)
+ Site.clear_anon_cache!
end
render json: success_json
diff --git a/app/services/sidebar_section_updater.rb b/app/services/sidebar_section_updater.rb
index 7f6293bff22..5133062d08d 100644
--- a/app/services/sidebar_section_updater.rb
+++ b/app/services/sidebar_section_updater.rb
@@ -13,7 +13,10 @@ class SidebarSectionUpdater
end
def update!
+ was_public = nil
+
@sidebar_section.with_lock do
+ was_public = @sidebar_section.public?
@sidebar_section.assign_attributes(
@section_params.merge(sidebar_urls_attributes: @links_params),
)
@@ -22,7 +25,7 @@ class SidebarSectionUpdater
update_link_order
end
- publish_public_update if @sidebar_section.public?
+ publish_public_update if was_public || @sidebar_section.public?
@sidebar_section
end
diff --git a/spec/requests/sidebar_sections_controller_spec.rb b/spec/requests/sidebar_sections_controller_spec.rb
index 011212b76a4..4d3d3eb2863 100644
--- a/spec/requests/sidebar_sections_controller_spec.rb
+++ b/spec/requests/sidebar_sections_controller_spec.rb
@@ -1146,6 +1146,74 @@ RSpec.describe SidebarSectionsController do
end
end
+ describe "anonymous site JSON cache" do
+ fab!(:admin_api_key, refind: false) { Fabricate(:api_key, user: admin) }
+ fab!(:public_section_to_make_private) do
+ Fabricate(:sidebar_section, title: "Private after update", public: true)
+ end
+ fab!(:public_section_to_delete) do
+ Fabricate(:sidebar_section, title: "Deleted after update", public: true)
+ end
+
+ it "removes sections that an admin makes private or deletes from anonymous site JSON" do
+ get "/site.json"
+
+ expect(response.status).to eq(200)
+ expect(response.body).to include(public_section_to_make_private.title)
+ expect(response.body).to include(public_section_to_delete.title)
+
+ put "/sidebar_sections/#{public_section_to_make_private.id}.json",
+ params: {
+ public: false,
+ },
+ headers: {
+ HTTP_API_KEY: admin_api_key.key,
+ }
+
+ expect(response.status).to eq(200)
+ get "/site.json"
+
+ expect(response.status).to eq(200)
+ expect(response.body).not_to include(public_section_to_make_private.title)
+ expect(response.body).to include(public_section_to_delete.title)
+
+ delete "/sidebar_sections/#{public_section_to_delete.id}.json",
+ headers: {
+ HTTP_API_KEY: admin_api_key.key,
+ }
+
+ expect(response.status).to eq(200)
+ get "/site.json"
+
+ expect(response.status).to eq(200)
+ expect(response.body).not_to include(public_section_to_delete.title)
+ end
+
+ it "removes reset Community section links from anonymous site JSON" do
+ community_section =
+ SidebarSection.find_by(section_type: SidebarSection.section_types[:community])
+ sidebar_url = Fabricate(:sidebar_url, name: "Removed Community link", value: "/removed")
+ Fabricate(:sidebar_section_link, sidebar_section: community_section, linkable: sidebar_url)
+ Site.clear_anon_cache!
+
+ get "/site.json"
+
+ expect(response.status).to eq(200)
+ expect(response.body).to include(sidebar_url.name)
+
+ put "/sidebar_sections/reset/#{community_section.id}.json",
+ headers: {
+ HTTP_API_KEY: admin_api_key.key,
+ }
+
+ expect(response.status).to eq(200)
+ get "/site.json"
+
+ expect(response.status).to eq(200)
+ expect(response.body).not_to include(sidebar_url.name)
+ end
+ end
+
describe "#destroy" do
fab!(:sidebar_section) { Fabricate(:sidebar_section, user: user) }