From e82696a4b9fbdb496fd46389f672a0d972689782 Mon Sep 17 00:00:00 2001 From: Claire Date: Wed, 26 Apr 2023 11:42:47 +0200 Subject: [PATCH] Fix /api/v1/instance/domain_blocks being unconditionally cached (#24662) --- .../api/v1/instances/domain_blocks_controller.rb | 9 +++++++-- app/controllers/concerns/cache_concern.rb | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/v1/instances/domain_blocks_controller.rb b/app/controllers/api/v1/instances/domain_blocks_controller.rb index 49fd8fa987..e954c45897 100644 --- a/app/controllers/api/v1/instances/domain_blocks_controller.rb +++ b/app/controllers/api/v1/instances/domain_blocks_controller.rb @@ -6,10 +6,15 @@ class Api::V1::Instances::DomainBlocksController < Api::BaseController before_action :require_enabled_api! before_action :set_domain_blocks - vary_by '' + vary_by '', if: -> { Setting.show_domain_blocks == 'all' } def index - cache_even_if_authenticated! + if Setting.show_domain_blocks == 'all' + cache_even_if_authenticated! + else + cache_if_unauthenticated! + end + render json: @domain_blocks, each_serializer: REST::DomainBlockSerializer, with_comment: (Setting.show_domain_blocks_rationale == 'all' || (Setting.show_domain_blocks_rationale == 'users' && user_signed_in?)) end diff --git a/app/controllers/concerns/cache_concern.rb b/app/controllers/concerns/cache_concern.rb index dae1dad1b0..55ebe1bd64 100644 --- a/app/controllers/concerns/cache_concern.rb +++ b/app/controllers/concerns/cache_concern.rb @@ -156,8 +156,8 @@ module CacheConcern end class_methods do - def vary_by(value) - before_action do |controller| + def vary_by(value, **kwargs) + before_action(**kwargs) do |controller| response.headers['Vary'] = value.respond_to?(:call) ? controller.instance_exec(&value) : value end end