posts/index: fix rating:s being included in page title in safe mode.

Fixes bug described in d3e4ac7c17 (commitcomment-39049351)

When dealing with searches, there are several variables we have to keep
in mind:

* Whether tag aliases should be applied.
* Whether search terms should be sorted.
* Whether the rating:s and -status:deleted metatags should be added by
  safe mode and the hide deleted posts setting.

Which of these things we need to do depends on the context:

* We want to apply aliases when actually doing the search, calculating
  the count, looking up the wiki excerpt, recording missed/popular
  searches in Reportbooru, and calculating related tags for the sidebar,
  but not when displaying the raw search as typed by the user (for
  example, in the page title or in the tag search box).
* We want to sort the search when calculating cache keys for fast_count
  or related tags, and when recording missed/popular searches, but not
  in the page title or when displaying the raw search.
* We want to add rating:s and -status:deleted when performing the
  search, calculating the count, or recording missed/popular searches,
  but not when calculating related tags for the sidebar, or when
  displaying the page title or raw search.

Here we introduce normalized_query and try to use it in contexts where
query normalization is necessary. When to use the normalized query
versus the raw unnormalized query is still subtle and prone to error.
This commit is contained in:
evazion
2020-05-12 21:08:52 -05:00
parent ea400296d4
commit ad02e0f62c
11 changed files with 91 additions and 69 deletions

View File

@@ -305,6 +305,13 @@ class PostsControllerTest < ActionDispatch::IntegrationTest
assert_select "#post_#{@post.id}", 1
end
end
context "in safe mode" do
should "not include the rating:s tag in the page title" do
get posts_path(tags: "1girl", safe_mode: true)
assert_select "title", text: "1girl Art | Safebooru"
end
end
end
context "show_seq action" do

View File

@@ -6,7 +6,7 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
end
def assert_fast_count(count, query, query_options = {}, fast_count_options = {})
assert_equal(count, PostQueryBuilder.new(query, **query_options).fast_count(**fast_count_options))
assert_equal(count, PostQueryBuilder.new(query, **query_options).normalized_query.fast_count(**fast_count_options))
end
setup do
@@ -1018,7 +1018,6 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
context "Parsing:" do
should "split a query" do
assert_equal(%w(aaa bbb), PostQueryBuilder.new("aaa bbb").split_query)
assert_equal(%w(~aaa -bbb*), PostQueryBuilder.new("~AAa -BBB* -bbb*").split_query)
end
should "not strip out valid characters when scanning" do
@@ -1050,22 +1049,22 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
end
end
context "The normalize_query method" do
context "The normalized_query method" do
should "work" do
create(:tag_alias, antecedent_name: "gray", consequent_name: "grey")
assert_equal("foo", PostQueryBuilder.new("foo").to_s)
assert_equal("foo", PostQueryBuilder.new(" foo ").to_s)
assert_equal("foo", PostQueryBuilder.new("FOO").to_s)
assert_equal("foo", PostQueryBuilder.new("foo foo").to_s)
assert_equal("grey", PostQueryBuilder.new("gray").to_s)
assert_equal("aaa bbb", PostQueryBuilder.new("bbb aaa").to_s)
assert_equal("-aaa bbb", PostQueryBuilder.new("bbb -aaa").to_s)
assert_equal("~aaa ~bbb", PostQueryBuilder.new("~bbb ~aaa").to_s)
assert_equal("commentary:true bbb", PostQueryBuilder.new("bbb commentary:true").to_s)
assert_equal('commentary:"true" bbb', PostQueryBuilder.new("bbb commentary:'true'").to_s)
assert_equal('-commentary:true bbb', PostQueryBuilder.new("bbb -commentary:true").to_s)
assert_equal('-commentary:"true" bbb', PostQueryBuilder.new("bbb -commentary:'true'").to_s)
assert_equal("foo", PostQueryBuilder.new("foo").normalized_query.to_s)
assert_equal("foo", PostQueryBuilder.new(" foo ").normalized_query.to_s)
assert_equal("foo", PostQueryBuilder.new("FOO").normalized_query.to_s)
assert_equal("foo", PostQueryBuilder.new("foo foo").normalized_query.to_s)
assert_equal("grey", PostQueryBuilder.new("gray").normalized_query.to_s)
assert_equal("aaa bbb", PostQueryBuilder.new("bbb aaa").normalized_query.to_s)
assert_equal("-aaa bbb", PostQueryBuilder.new("bbb -aaa").normalized_query.to_s)
assert_equal("~aaa ~bbb", PostQueryBuilder.new("~bbb ~aaa").normalized_query.to_s)
assert_equal("commentary:true bbb", PostQueryBuilder.new("bbb commentary:true").normalized_query.to_s)
assert_equal('commentary:"true" bbb', PostQueryBuilder.new("bbb commentary:'true'").normalized_query.to_s)
assert_equal('-commentary:true bbb', PostQueryBuilder.new("bbb -commentary:true").normalized_query.to_s)
assert_equal('-commentary:"true" bbb', PostQueryBuilder.new("bbb -commentary:'true'").normalized_query.to_s)
end
end
@@ -1114,7 +1113,7 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
should "set the expiration time" do
Cache.expects(:put).with(PostQueryBuilder.new("score:42 aaa").count_cache_key, 1, 180)
PostQueryBuilder.new("aaa score:42").fast_count
assert_fast_count(1, "aaa score:42")
end
should "work with the hide_deleted_posts option turned on" do
@@ -1126,8 +1125,8 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
context "a blank search" do
should "should execute a search" do
assert_equal(1, PostQueryBuilder.new("").fast_count(estimate_count: false))
assert_nothing_raised { PostQueryBuilder.new("").fast_count(estimate_count: true) }
assert_fast_count(1, "", {}, { estimate_count: false })
assert_nothing_raised { PostQueryBuilder.new("").normalized_query.fast_count(estimate_count: true) }
end
should "return 0 for a nonexisting tag" do
@@ -1136,13 +1135,13 @@ class PostQueryBuilderTest < ActiveSupport::TestCase
context "in safe mode" do
should "work for a blank search" do
assert_equal(2, PostQueryBuilder.new("").fast_count(estimate_count: false))
assert_nothing_raised { PostQueryBuilder.new("").fast_count(estimate_count: true) }
assert_fast_count(0, "", { safe_mode: true }, { estimate_count: false })
assert_nothing_raised { PostQueryBuilder.new("", safe_mode: true).normalized_query.fast_count(estimate_count: true) }
end
should "work for a nil search" do
assert_equal(2, PostQueryBuilder.new(nil).fast_count(estimate_count: false))
assert_nothing_raised { PostQueryBuilder.new(nil).fast_count(estimate_count: true) }
assert_fast_count(0, nil, { safe_mode: true }, { estimate_count: false })
assert_nothing_raised { PostQueryBuilder.new("", safe_mode: true).normalized_query.fast_count(estimate_count: true) }
end
should "not fail for a two tag search by a member" do