2016-11-16 02:56:29 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-11-13 00:33:21 +11:00
|
|
|
class SearchService < BaseService
|
2023-09-01 17:27:03 +10:00
|
|
|
QUOTE_EQUIVALENT_CHARACTERS = /[“”„«»「」『』《》]/
|
|
|
|
|
Add type, limit, offset, min_id, max_id, account_id to search API (#10091)
* Add type, limit, offset, min_id, max_id, account_id to search API
Fix #8939
* Make the offset work on accounts and hashtags search as well
* Assure brakeman we are not doing mass assignment here
* Do not allow paginating unless a type is chosen
* Fix search query and index id field on statuses instead of created_at
2019-02-27 01:21:36 +11:00
|
|
|
def call(query, account, limit, options = {})
|
2023-09-01 17:27:03 +10:00
|
|
|
@query = query&.strip&.gsub(QUOTE_EQUIVALENT_CHARACTERS, '"')
|
2023-07-04 02:06:57 +10:00
|
|
|
@account = account
|
|
|
|
@options = options
|
|
|
|
@limit = limit.to_i
|
|
|
|
@offset = options[:type].blank? ? 0 : options[:offset].to_i
|
|
|
|
@resolve = options[:resolve] || false
|
|
|
|
@following = options[:following] || false
|
2016-11-13 00:33:21 +11:00
|
|
|
|
2017-05-06 01:26:04 +10:00
|
|
|
default_results.tap do |results|
|
2020-04-02 12:39:37 +11:00
|
|
|
next if @query.blank? || @limit.zero?
|
2019-12-04 14:34:08 +11:00
|
|
|
|
2017-05-06 01:26:04 +10:00
|
|
|
if url_query?
|
2020-04-02 12:39:37 +11:00
|
|
|
results.merge!(url_resource_results) unless url_resource.nil? || @offset.positive? || (@options[:type].present? && url_resource_symbol != @options[:type].to_sym)
|
Add type, limit, offset, min_id, max_id, account_id to search API (#10091)
* Add type, limit, offset, min_id, max_id, account_id to search API
Fix #8939
* Make the offset work on accounts and hashtags search as well
* Assure brakeman we are not doing mass assignment here
* Do not allow paginating unless a type is chosen
* Fix search query and index id field on statuses instead of created_at
2019-02-27 01:21:36 +11:00
|
|
|
elsif @query.present?
|
2018-02-10 09:04:47 +11:00
|
|
|
results[:accounts] = perform_accounts_search! if account_searchable?
|
2023-08-29 01:08:37 +10:00
|
|
|
results[:statuses] = perform_statuses_search! if status_searchable?
|
2018-02-10 09:04:47 +11:00
|
|
|
results[:hashtags] = perform_hashtags_search! if hashtag_searchable?
|
2017-05-06 01:26:04 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-04-01 07:44:12 +11:00
|
|
|
|
2018-02-10 09:04:47 +11:00
|
|
|
private
|
|
|
|
|
|
|
|
def perform_accounts_search!
|
Add type, limit, offset, min_id, max_id, account_id to search API (#10091)
* Add type, limit, offset, min_id, max_id, account_id to search API
Fix #8939
* Make the offset work on accounts and hashtags search as well
* Assure brakeman we are not doing mass assignment here
* Do not allow paginating unless a type is chosen
* Fix search query and index id field on statuses instead of created_at
2019-02-27 01:21:36 +11:00
|
|
|
AccountSearchService.new.call(
|
|
|
|
@query,
|
|
|
|
@account,
|
|
|
|
limit: @limit,
|
|
|
|
resolve: @resolve,
|
2023-06-29 21:05:21 +10:00
|
|
|
offset: @offset,
|
2023-07-04 02:06:57 +10:00
|
|
|
use_searchable_text: true,
|
2023-07-11 04:58:13 +10:00
|
|
|
following: @following,
|
|
|
|
start_with_hashtag: @query.start_with?('#')
|
Add type, limit, offset, min_id, max_id, account_id to search API (#10091)
* Add type, limit, offset, min_id, max_id, account_id to search API
Fix #8939
* Make the offset work on accounts and hashtags search as well
* Assure brakeman we are not doing mass assignment here
* Do not allow paginating unless a type is chosen
* Fix search query and index id field on statuses instead of created_at
2019-02-27 01:21:36 +11:00
|
|
|
)
|
2018-02-10 09:04:47 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform_statuses_search!
|
2023-08-25 00:40:04 +10:00
|
|
|
StatusesSearchService.new.call(
|
|
|
|
@query,
|
|
|
|
@account,
|
|
|
|
limit: @limit,
|
|
|
|
offset: @offset,
|
|
|
|
account_id: @options[:account_id],
|
|
|
|
min_id: @options[:min_id],
|
|
|
|
max_id: @options[:max_id]
|
|
|
|
)
|
2018-02-10 09:04:47 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform_hashtags_search!
|
2019-08-18 11:45:51 +10:00
|
|
|
TagSearchService.new.call(
|
|
|
|
@query,
|
|
|
|
limit: @limit,
|
2019-09-28 09:02:21 +10:00
|
|
|
offset: @offset,
|
|
|
|
exclude_unreviewed: @options[:exclude_unreviewed]
|
Add type, limit, offset, min_id, max_id, account_id to search API (#10091)
* Add type, limit, offset, min_id, max_id, account_id to search API
Fix #8939
* Make the offset work on accounts and hashtags search as well
* Assure brakeman we are not doing mass assignment here
* Do not allow paginating unless a type is chosen
* Fix search query and index id field on statuses instead of created_at
2019-02-27 01:21:36 +11:00
|
|
|
)
|
2018-02-10 09:04:47 +11:00
|
|
|
end
|
|
|
|
|
2017-05-06 01:26:04 +10:00
|
|
|
def default_results
|
|
|
|
{ accounts: [], hashtags: [], statuses: [] }
|
|
|
|
end
|
2016-11-13 00:33:21 +11:00
|
|
|
|
2017-05-06 01:26:04 +10:00
|
|
|
def url_query?
|
2023-06-06 22:50:51 +10:00
|
|
|
@resolve && %r{\Ahttps?://}.match?(@query)
|
2017-05-06 01:26:04 +10:00
|
|
|
end
|
|
|
|
|
2018-01-23 00:24:22 +11:00
|
|
|
def url_resource_results
|
|
|
|
{ url_resource_symbol => [url_resource] }
|
2017-05-06 01:26:04 +10:00
|
|
|
end
|
|
|
|
|
2018-01-23 00:24:22 +11:00
|
|
|
def url_resource
|
2023-07-12 18:08:51 +10:00
|
|
|
@url_resource ||= ResolveURLService.new.call(@query, on_behalf_of: @account)
|
2017-05-06 01:26:04 +10:00
|
|
|
end
|
2016-11-13 00:33:21 +11:00
|
|
|
|
2018-01-23 00:24:22 +11:00
|
|
|
def url_resource_symbol
|
|
|
|
url_resource.class.name.downcase.pluralize.to_sym
|
2016-11-13 00:33:21 +11:00
|
|
|
end
|
2018-02-10 09:04:47 +11:00
|
|
|
|
2023-08-29 01:08:37 +10:00
|
|
|
def status_searchable?
|
|
|
|
Chewy.enabled? && status_search? && @account.present?
|
2018-02-10 09:04:47 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def account_searchable?
|
2023-08-29 01:08:37 +10:00
|
|
|
account_search?
|
2018-02-10 09:04:47 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def hashtag_searchable?
|
2023-08-29 01:08:37 +10:00
|
|
|
hashtag_search?
|
Add type, limit, offset, min_id, max_id, account_id to search API (#10091)
* Add type, limit, offset, min_id, max_id, account_id to search API
Fix #8939
* Make the offset work on accounts and hashtags search as well
* Assure brakeman we are not doing mass assignment here
* Do not allow paginating unless a type is chosen
* Fix search query and index id field on statuses instead of created_at
2019-02-27 01:21:36 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
def account_search?
|
|
|
|
@options[:type].blank? || @options[:type] == 'accounts'
|
|
|
|
end
|
|
|
|
|
|
|
|
def hashtag_search?
|
|
|
|
@options[:type].blank? || @options[:type] == 'hashtags'
|
|
|
|
end
|
|
|
|
|
2023-08-29 01:08:37 +10:00
|
|
|
def status_search?
|
Add type, limit, offset, min_id, max_id, account_id to search API (#10091)
* Add type, limit, offset, min_id, max_id, account_id to search API
Fix #8939
* Make the offset work on accounts and hashtags search as well
* Assure brakeman we are not doing mass assignment here
* Do not allow paginating unless a type is chosen
* Fix search query and index id field on statuses instead of created_at
2019-02-27 01:21:36 +11:00
|
|
|
@options[:type].blank? || @options[:type] == 'statuses'
|
|
|
|
end
|
2016-11-13 00:33:21 +11:00
|
|
|
end
|