mastodon-sakura/app/services/search_service.rb
neatchee 4691b0068c
Merge latest upstream from glitch-soc/mastodon/main (#70)
* Remove the search button from UI header when logged out (#25631)

* Change account search to match by text when opted-in (#25599)

Co-authored-by: Eugen Rochko <eugen@zeonfederated.com>

* Fix ResolveURLService not resolving local URLs for remote content (#25637)

* Remove `pkg-config` gem dependency (#25615)

* Update Crowdin configuration file

* Fix onboarding prompt being displayed because of disconnection gaps (#25617)

* Use an Immutable Record as the root state (#25584)

* Add index to backups on `user_id` column (#25647)

* Fix rails `rewhere` deprecation warning in directories api controller (#25625)

* Remove unused routes (#25578)

* Fixing an issue with a missing argument (#2261)

undefined

* Update uri to version 0.12.2 (CVE fix) (#25657)

* Change local and federated timelines to be in a single firehose column (#25641)

* Fix HTTP 500 in `/api/v1/emails/check_confirmation` (#25595)

* Rails 7 update (#24241)

* Change dropdown icon above compose form from ellipsis to bars in web UI (#25661)

* Prevent duplicate concurrent calls of `/api/*/instance` in web UI (#25663)

* Revert "Rails 7 update" (#25667)

* [Glitch] Remove the search button from UI header when logged out

Port 285a691936 to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>

* [Glitch] Fix onboarding prompt being displayed because of disconnection gaps

Port 9934949fc4 to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>

* [Glitch] Use an Immutable Record as the root state

Port 78ba12f0bf to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>

* [Glitch] Change local and federated timelines to be in a single firehose column

Port cea9db5a0b to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>

* [Glitch] Change dropdown icon above compose form from ellipsis to bars in web UI

Port 0512537eb6 to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>

* [Glitch] Prevent duplicate concurrent calls of `/api/*/instance` in web UI

Port 5b46345459 to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>

* Show local-only posts in “All” by default, and add back option to toggle it

* Fix showing local only toots in "All" (#2265)

* Fix warnings about missing dependency in hooks

Signed-off-by: Plastikmensch <plastikmensch@users.noreply.github.com>

* Add `allowLocalOnly` to timelineId

Without this local-only toots will never be loaded.

feedType is checked to be public to not show local-only toots in the "Remote" tab.

Signed-off-by: Plastikmensch <plastikmensch@users.noreply.github.com>

---------

Signed-off-by: Plastikmensch <plastikmensch@users.noreply.github.com>

* Add regex filter back to firehose (#2266)

* Add regex filter back to firehose

The regex filter will apply to all tabs and not be automatically applied when pinned.

Signed-off-by: Plastikmensch <plastikmensch@users.noreply.github.com>

* Keep regex when pinned

Signed-off-by: Plastikmensch <plastikmensch@users.noreply.github.com>

---------

Signed-off-by: Plastikmensch <plastikmensch@users.noreply.github.com>

---------

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
Signed-off-by: Plastikmensch <plastikmensch@users.noreply.github.com>
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
Co-authored-by: jsgoldstein <jakegoldstein95@gmail.com>
Co-authored-by: Eugen Rochko <eugen@zeonfederated.com>
Co-authored-by: Renaud Chaput <renchap@gmail.com>
Co-authored-by: Matt Jankowski <matt@jankowski.online>
Co-authored-by: Vivianne <puttabutta@gmail.com>
Co-authored-by: Daniel M Brasil <danielmbrasil@protonmail.com>
Co-authored-by: mogaminsk <mgmnjp@icloud.com>
Co-authored-by: Plastikmensch <Plastikmensch@users.noreply.github.com>
2023-07-03 09:12:00 -07:00

119 lines
3.4 KiB
Ruby

# frozen_string_literal: true
class SearchService < BaseService
def call(query, account, limit, options = {})
@query = query&.strip
@account = account
@options = options
@limit = limit.to_i
@offset = options[:type].blank? ? 0 : options[:offset].to_i
@resolve = options[:resolve] || false
default_results.tap do |results|
next if @query.blank? || @limit.zero?
if url_query?
results.merge!(url_resource_results) unless url_resource.nil? || @offset.positive? || (@options[:type].present? && url_resource_symbol != @options[:type].to_sym)
elsif @query.present?
results[:accounts] = perform_accounts_search! if account_searchable?
results[:statuses] = perform_statuses_search! if full_text_searchable?
results[:hashtags] = perform_hashtags_search! if hashtag_searchable?
end
end
end
private
def perform_accounts_search!
AccountSearchService.new.call(
@query,
@account,
limit: @limit,
resolve: @resolve,
offset: @offset,
use_searchable_text: true
)
end
def perform_statuses_search!
definition = parsed_query.apply(StatusesIndex.filter(term: { searchable_by: @account.id }))
definition = definition.filter(term: { account_id: @options[:account_id] }) if @options[:account_id].present?
if @options[:min_id].present? || @options[:max_id].present?
range = {}
range[:gt] = @options[:min_id].to_i if @options[:min_id].present?
range[:lt] = @options[:max_id].to_i if @options[:max_id].present?
definition = definition.filter(range: { id: range })
end
results = definition.limit(@limit).offset(@offset).objects.compact
account_ids = results.map(&:account_id)
account_domains = results.map(&:account_domain)
preloaded_relations = @account.relations_map(account_ids, account_domains)
results.reject { |status| StatusFilter.new(status, @account, preloaded_relations).filtered? }
rescue Faraday::ConnectionFailed, Parslet::ParseFailed
[]
end
def perform_hashtags_search!
TagSearchService.new.call(
@query,
limit: @limit,
offset: @offset,
exclude_unreviewed: @options[:exclude_unreviewed]
)
end
def default_results
{ accounts: [], hashtags: [], statuses: [] }
end
def url_query?
@resolve && %r{\Ahttps?://}.match?(@query)
end
def url_resource_results
{ url_resource_symbol => [url_resource] }
end
def url_resource
@_url_resource ||= ResolveURLService.new.call(@query, on_behalf_of: @account)
end
def url_resource_symbol
url_resource.class.name.downcase.pluralize.to_sym
end
def full_text_searchable?
return false unless Chewy.enabled?
statuses_search? && !@account.nil? && !((@query.start_with?('#') || @query.include?('@')) && !@query.include?(' '))
end
def account_searchable?
account_search? && !(@query.start_with?('#') || (@query.include?('@') && @query.include?(' ')))
end
def hashtag_searchable?
hashtag_search? && !@query.include?('@')
end
def account_search?
@options[:type].blank? || @options[:type] == 'accounts'
end
def hashtag_search?
@options[:type].blank? || @options[:type] == 'hashtags'
end
def statuses_search?
@options[:type].blank? || @options[:type] == 'statuses'
end
def parsed_query
SearchQueryTransformer.new.apply(SearchQueryParser.new.parse(@query))
end
end