From 2a96bca7a14c1b79d79e83baa55a18d891432ee5 Mon Sep 17 00:00:00 2001 From: Roman Rizzi Date: Mon, 25 Apr 2022 16:04:13 -0300 Subject: [PATCH] FIX: Correctly handle the print param on topics#show. (#16555) The controller incorrectly sets print to true when passing `print=false`, which causes the rate limit to perform. --- app/controllers/topics_controller.rb | 2 +- config/routes.rb | 2 +- spec/requests/topics_controller_spec.rb | 7 +++++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index afb2d313a52..5e4c9639560 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -66,7 +66,7 @@ class TopicsController < ApplicationController opts = params.slice(:username_filters, :filter, :page, :post_number, :show_deleted, :replies_to_post_number, :filter_upwards_post_id, :filter_top_level_replies) username_filters = opts[:username_filters] - opts[:print] = true if params[:print].present? + opts[:print] = true if params[:print] == 'true' opts[:username_filters] = username_filters.split(',') if username_filters.is_a?(String) # Special case: a slug with a number in front should look by slug first before looking diff --git a/config/routes.rb b/config/routes.rb index 3cf5d7ef97f..e7c73c0990f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -823,7 +823,7 @@ Discourse::Application.routes.draw do # Topic routes get "t/id_for/:slug" => "topics#id_for_slug" get "t/external_id/:external_id" => "topics#show_by_external_id", format: :json, constrains: { external_id: /\A[\w-]+\z/ } - get "t/:slug/:topic_id/print" => "topics#show", format: :html, print: true, constraints: { topic_id: /\d+/ } + get "t/:slug/:topic_id/print" => "topics#show", format: :html, print: 'true', constraints: { topic_id: /\d+/ } get "t/:slug/:topic_id/wordpress" => "topics#wordpress", constraints: { topic_id: /\d+/ } get "t/:topic_id/wordpress" => "topics#wordpress", constraints: { topic_id: /\d+/ } get "t/:slug/:topic_id/moderator-liked" => "topics#moderator_liked", constraints: { topic_id: /\d+/ } diff --git a/spec/requests/topics_controller_spec.rb b/spec/requests/topics_controller_spec.rb index 26fdddb9f5f..80da19936d1 100644 --- a/spec/requests/topics_controller_spec.rb +++ b/spec/requests/topics_controller_spec.rb @@ -2002,6 +2002,13 @@ RSpec.describe TopicsController do expect(response.status).to eq(404) end + it "doesn't use print mode when print equals false" do + SiteSetting.max_prints_per_hour_per_user = 0 + + get "/t/#{topic.slug}/#{topic.id}.json?print=false" + expect(response.status).to eq(200) + end + context 'a topic with nil slug exists' do before do nil_slug_topic = Fabricate(:topic)