From 1a89071f2dfb8cba966dd438a86de9388ec0b2d9 Mon Sep 17 00:00:00 2001 From: evazion Date: Mon, 2 May 2022 22:03:33 -0500 Subject: [PATCH] posts: fix error on post index page when search count times out in safe mode Fix a nil deference error on the post index page. This happened when performing a single tag search in safe mode and calculating the number of search results timed out. --- app/logical/post_query.rb | 1 + app/logical/post_sets/post.rb | 4 +++- test/functional/posts_controller_test.rb | 7 +++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/app/logical/post_query.rb b/app/logical/post_query.rb index ad821a336..706492dd0 100644 --- a/app/logical/post_query.rb +++ b/app/logical/post_query.rb @@ -201,6 +201,7 @@ class PostQuery end concerning :CountMethods do + # @return [Integer, nil] The number of posts returned by the search, or nil on timeout. def post_count @post_count ||= fast_count end diff --git a/app/logical/post_sets/post.rb b/app/logical/post_sets/post.rb index 5f9bcdf5c..54025675f 100644 --- a/app/logical/post_sets/post.rb +++ b/app/logical/post_sets/post.rb @@ -35,7 +35,8 @@ module PostSets # The description of the page for the tag. def meta_description - if post_query.is_simple_tag? + # XXX post_count may be nil if the search times out because of safe mode + if normalized_query.is_simple_tag? && post_count.present? humanized_count = ApplicationController.helpers.humanized_number(post_count, million: " million", thousand: " thousand") humanized_count = "over #{humanized_count}" if post_count >= 1_000 @@ -108,6 +109,7 @@ module PostSets @posts ||= normalized_query.paginated_posts(page, includes: includes, count: post_count, search_count: !post_count.nil?, limit: per_page, max_limit: max_per_page).load end + # @return [Integer, nil] The number of posts returned by the search, or nil if unknown. def post_count normalized_query.post_count end diff --git a/test/functional/posts_controller_test.rb b/test/functional/posts_controller_test.rb index e0c088ab6..fad91acba 100644 --- a/test/functional/posts_controller_test.rb +++ b/test/functional/posts_controller_test.rb @@ -234,6 +234,13 @@ class PostsControllerTest < ActionDispatch::IntegrationTest assert_response 410 assert_select "h1", "Search Error" end + + should "render if the search count times out" do + PostQuery.any_instance.stubs(:exact_count).returns(nil) + get posts_path, params: { tags: "1girl", safe_mode: "true" } + + assert_response :success + end end context "with a pool: search" do