autocomplete: replace calls to PostQueryBuilder with PostQuery.

This commit is contained in:
evazion
2022-03-30 01:50:58 -05:00
parent 6edff247f2
commit 04551b8154
3 changed files with 24 additions and 16 deletions

View File

@@ -45,7 +45,7 @@ class AutocompleteService
def autocomplete_results def autocomplete_results
case type case type
when :tag_query when :tag_query
autocomplete_tag_query(query) autocomplete_tag_query
when :tag when :tag
autocomplete_tag(query) autocomplete_tag(query)
when :artist when :artist
@@ -70,17 +70,20 @@ class AutocompleteService
end end
# Complete a tag search (a regular tag or a metatag) # Complete a tag search (a regular tag or a metatag)
# @param string [String] the string to complete #
# @return [Array<Hash>] the autocomplete results # @return [Array<Hash>] the autocomplete results
def autocomplete_tag_query(string) def autocomplete_tag_query
term = PostQueryBuilder.new(string).terms.first if parsed_query.tag?
return [] if term.nil? tag = parsed_query.tag_names.first
autocomplete_tag(tag)
case term.type elsif parsed_query.wildcard?
when :tag wildcard = parsed_query.wildcards.first
autocomplete_tag(term.name) autocomplete_tag(wildcard.name)
when :metatag elsif parsed_query.metatag?
autocomplete_metatag(term.name, term.value) metatag = parsed_query.metatags.first
autocomplete_metatag(metatag.name, metatag.value)
else
[]
end end
end end
@@ -330,7 +333,7 @@ class AutocompleteService
# Whether the results can be safely cached with `Cache-Control: public`. # Whether the results can be safely cached with `Cache-Control: public`.
# Queries that don't depend on the current user are safe to cache publicly. # Queries that don't depend on the current user are safe to cache publicly.
def cache_publicly? def cache_publicly?
if type == :tag_query && parsed_search&.type == :tag if type == :tag_query && parsed_query.tag?
true true
elsif type.in?(%i[tag artist wiki_page pool opensearch]) elsif type.in?(%i[tag artist wiki_page pool opensearch])
true true
@@ -339,9 +342,9 @@ class AutocompleteService
end end
end end
def parsed_search def parsed_query
PostQueryBuilder.new(query).terms.first PostQuery.new(query.delete_prefix("-").delete_prefix("~"))
end end
memoize :autocomplete_results memoize :autocomplete_results, :parsed_query
end end

View File

@@ -4,7 +4,7 @@ class PostQuery
extend Memoist extend Memoist
attr_reader :search, :parser, :builder, :ast attr_reader :search, :parser, :builder, :ast
delegate :tag_names, :metatags, to: :ast delegate :tag?, :metatag?, :wildcard?, :metatags, :wildcards, :tag_names, :metatags, to: :ast
def initialize(search, current_user: User.anonymous, tag_limit: nil, safe_mode: false, hide_deleted_posts: false) def initialize(search, current_user: User.anonymous, tag_limit: nil, safe_mode: false, hide_deleted_posts: false)
@search = search @search = search

View File

@@ -242,6 +242,11 @@ class PostQuery
nodes.select(&:metatag?).uniq.sort nodes.select(&:metatag?).uniq.sort
end end
# @return [Array<AST>] A list of all unique wildcard nodes in the AST.
def wildcards
nodes.select(&:wildcard?).uniq.sort
end
# @return [Array<String>] The names of all unique tags in the AST. # @return [Array<String>] The names of all unique tags in the AST.
def tag_names def tag_names
tags.map(&:name) tags.map(&:name)