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

@@ -604,7 +604,7 @@ class Post < ApplicationRecord
# If someone else committed changes to this post before we did,
# then try to merge the tag changes together.
current_tags = tag_string_was.split
new_tags = PostQueryBuilder.new(tag_string, normalize_aliases: false).parse_tag_edit
new_tags = PostQueryBuilder.new(tag_string).parse_tag_edit
old_tags = old_tag_string.split
kept_tags = current_tags & new_tags
@@ -642,7 +642,7 @@ class Post < ApplicationRecord
end
def normalize_tags
normalized_tags = PostQueryBuilder.new(tag_string, normalize_aliases: false).parse_tag_edit
normalized_tags = PostQueryBuilder.new(tag_string).parse_tag_edit
normalized_tags = apply_casesensitive_metatags(normalized_tags)
normalized_tags = normalized_tags.map(&:downcase)
normalized_tags = filter_metatags(normalized_tags)
@@ -1475,7 +1475,8 @@ class Post < ApplicationRecord
end
def user_tag_match(query, user = CurrentUser.user, safe_mode: CurrentUser.safe_mode?, hide_deleted_posts: user.hide_deleted_posts?)
PostQueryBuilder.new(query, user, safe_mode: safe_mode, hide_deleted_posts: hide_deleted_posts).build
post_query = PostQueryBuilder.new(query, user, safe_mode: safe_mode, hide_deleted_posts: hide_deleted_posts)
post_query.normalized_query.build
end
def search(params)

View File

@@ -140,11 +140,11 @@ class SavedSearch < ApplicationRecord
end
def normalized_query
PostQueryBuilder.new(query).to_s
PostQueryBuilder.new(query).normalized_query.to_s
end
def normalize_query
self.query = PostQueryBuilder.new(query, normalize_order: false).to_s
self.query = PostQueryBuilder.new(query).normalized_query(sort: false).to_s
end
end

View File

@@ -237,7 +237,7 @@ class Upload < ApplicationRecord
include SourceMethods
def assign_rating_from_tags
if rating = PostQueryBuilder.new(tag_string, normalize_aliases: false).find_metatag(:rating)
if rating = PostQueryBuilder.new(tag_string).find_metatag(:rating)
self.rating = rating.downcase.first
end
end