DEV: Modifier to add params to TopicsController redirect url (#26470)
This commit is contained in:
parent
51006b5591
commit
797ab30d95
|
@ -1255,7 +1255,19 @@ class TopicsController < ApplicationController
|
|||
raise(SiteSetting.detailed_404 ? ex : Discourse::NotFound)
|
||||
end
|
||||
|
||||
opts = params.slice(:page, :print, :filter_top_level_replies, :preview_theme_id)
|
||||
# Allow plugins to append allowed query parameters, so they aren't scrubbed on redirect to proper topic URL
|
||||
additional_allowed_query_parameters =
|
||||
DiscoursePluginRegistry.apply_modifier(
|
||||
:redirect_to_correct_topic_additional_query_parameters,
|
||||
[],
|
||||
)
|
||||
|
||||
opts =
|
||||
params.slice(
|
||||
*%i[page print filter_top_level_replies preview_theme_id].concat(
|
||||
additional_allowed_query_parameters,
|
||||
),
|
||||
)
|
||||
opts.delete(:page) if params[:page] == 0
|
||||
|
||||
url = topic.relative_url
|
||||
|
|
|
@ -2317,6 +2317,12 @@ RSpec.describe TopicsController do
|
|||
expect(response).to redirect_to("#{topic.relative_url}/42?page=1")
|
||||
end
|
||||
|
||||
it "scrubs invalid query parameters when redirecting" do
|
||||
get "/t/#{topic.slug}", params: { silly_param: "hehe" }
|
||||
|
||||
expect(response).to redirect_to(topic.relative_url)
|
||||
end
|
||||
|
||||
it "returns 404 when an invalid slug is given and no id" do
|
||||
get "/t/nope-nope.json"
|
||||
|
||||
|
@ -2386,6 +2392,28 @@ RSpec.describe TopicsController do
|
|||
expect(second_request_queries.count).to eq(first_request_queries.count)
|
||||
end
|
||||
|
||||
context "with registered redirect_to_correct_topic_additional_query_parameters" do
|
||||
let(:modifier_block) { Proc.new { |allowed_params| allowed_params << :silly_param } }
|
||||
|
||||
it "retains the permitted query param when redirecting" do
|
||||
plugin_instance = Plugin::Instance.new
|
||||
plugin_instance.register_modifier(
|
||||
:redirect_to_correct_topic_additional_query_parameters,
|
||||
&modifier_block
|
||||
)
|
||||
|
||||
get "/t/#{topic.slug}", params: { silly_param: "hehe" }
|
||||
|
||||
expect(response).to redirect_to("#{topic.relative_url}?silly_param=hehe")
|
||||
ensure
|
||||
DiscoursePluginRegistry.unregister_modifier(
|
||||
plugin_instance,
|
||||
:redirect_to_correct_topic_additional_query_parameters,
|
||||
&modifier_block
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context "when a topic with nil slug exists" do
|
||||
before do
|
||||
nil_slug_topic = Fabricate(:topic)
|
||||
|
|
Loading…
Reference in New Issue