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.
This commit is contained in:
evazion
2022-05-02 22:03:33 -05:00
parent 8d9e53da2b
commit 1a89071f2d
3 changed files with 11 additions and 1 deletions

View File

@@ -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

View File

@@ -35,7 +35,8 @@ module PostSets
# The description of the page for the <meta name="description"> 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

View File

@@ -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