Merge remote-tracking branch 'parent/main' into kb_migration

This commit is contained in:
KMY 2023-08-29 13:25:48 +09:00
commit 05a022448b
22 changed files with 306 additions and 209 deletions

View file

@ -9,7 +9,7 @@ class SearchQueryParser < Parslet::Parser
rule(:prefix) { (term >> colon).as(:prefix) }
rule(:shortcode) { (colon >> term >> colon.maybe).as(:shortcode) }
rule(:phrase) { (quote >> (term >> space.maybe).repeat >> quote).as(:phrase) }
rule(:clause) { (prefix.maybe >> operator.maybe >> (phrase | term | shortcode)).as(:clause) }
rule(:clause) { (operator.maybe >> prefix.maybe >> (phrase | term | shortcode)).as(:clause) }
rule(:query) { (clause >> space.maybe).repeat.as(:query) }
root(:query)
end

View file

@ -36,7 +36,11 @@ class SearchQueryTransformer < Parslet::Transform
def clause_to_filter(clause)
case clause
when PrefixClause
{ clause.type => { clause.filter => clause.term } }
if clause.negated?
{ bool: { must_not: { clause.type => { clause.filter => clause.term } } } }
else
{ clause.type => { clause.filter => clause.term } }
end
else
raise "Unexpected clause type: #{clause}"
end
@ -81,7 +85,9 @@ class SearchQueryTransformer < Parslet::Transform
class PrefixClause
attr_reader :type, :filter, :operator, :term
def initialize(prefix, term)
def initialize(prefix, operator, term, options = {})
@negated = operator == '-'
@options = options
@operator = :filter
case prefix
@ -100,23 +106,29 @@ class SearchQueryTransformer < Parslet::Transform
when 'before'
@filter = :created_at
@type = :range
@term = { lt: term }
@term = { lt: term, time_zone: @options[:current_account]&.user_time_zone || 'UTC' }
when 'after'
@filter = :created_at
@type = :range
@term = { gt: term }
@term = { gt: term, time_zone: @options[:current_account]&.user_time_zone || 'UTC' }
when 'during'
@filter = :created_at
@type = :range
@term = { gte: term, lte: term }
@term = { gte: term, lte: term, time_zone: @options[:current_account]&.user_time_zone || 'UTC' }
else
raise Mastodon::SyntaxError
end
end
def negated?
@negated
end
private
def account_id_from_term(term)
return @options[:current_account]&.id || -1 if term == 'me'
username, domain = term.gsub(/\A@/, '').split('@')
domain = nil if TagManager.instance.local_domain?(domain)
account = Account.find_remote(username, domain)
@ -132,7 +144,7 @@ class SearchQueryTransformer < Parslet::Transform
operator = clause[:operator]&.to_s
if clause[:prefix]
PrefixClause.new(prefix, clause[:term].to_s)
PrefixClause.new(prefix, operator, clause[:term].to_s, current_account: current_account)
elsif clause[:term]
TermClause.new(prefix, operator, clause[:term].to_s)
elsif clause[:shortcode]