New API method: /api/v1/search
Returns accounts, statuses, hashtags arrays
This commit is contained in:
26
app/services/account_search_service.rb
Normal file
26
app/services/account_search_service.rb
Normal file
@ -0,0 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class AccountSearchService < BaseService
|
||||
def call(query, limit, resolve = false, account = nil)
|
||||
return [] if query.blank? || query.start_with?('#')
|
||||
|
||||
username, domain = query.gsub(/\A@/, '').split('@')
|
||||
domain = nil if TagManager.instance.local_domain?(domain)
|
||||
|
||||
if domain.nil?
|
||||
exact_match = Account.find_local(username)
|
||||
results = account.nil? ? Account.search_for(username, limit) : Account.advanced_search_for(username, account, limit)
|
||||
else
|
||||
exact_match = Account.find_remote(username, domain)
|
||||
results = account.nil? ? Account.search_for("#{username} #{domain}", limit) : Account.advanced_search_for("#{username} #{domain}", account, limit)
|
||||
end
|
||||
|
||||
results = [exact_match] + results.reject { |a| a.id == exact_match.id } if exact_match
|
||||
|
||||
if resolve && !exact_match && !domain.nil?
|
||||
results = [FollowRemoteAccountService.new.call("#{username}@#{domain}")]
|
||||
end
|
||||
|
||||
results
|
||||
end
|
||||
end
|
18
app/services/fetch_remote_resource_service.rb
Normal file
18
app/services/fetch_remote_resource_service.rb
Normal file
@ -0,0 +1,18 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class FetchRemoteResourceService < BaseService
|
||||
def call(url)
|
||||
atom_url, body = FetchAtomService.new.call(url)
|
||||
|
||||
return nil if atom_url.nil?
|
||||
|
||||
xml = Nokogiri::XML(body)
|
||||
xml.encoding = 'utf-8'
|
||||
|
||||
if xml.root.name == 'feed'
|
||||
FetchRemoteAccountService.new.call(atom_url)
|
||||
elsif xml.root.name == 'entry'
|
||||
FetchRemoteStatusService.new.call(atom_url)
|
||||
end
|
||||
end
|
||||
end
|
@ -2,23 +2,18 @@
|
||||
|
||||
class SearchService < BaseService
|
||||
def call(query, limit, resolve = false, account = nil)
|
||||
return if query.blank? || query.start_with?('#')
|
||||
return if query.blank?
|
||||
|
||||
username, domain = query.gsub(/\A@/, '').split('@')
|
||||
domain = nil if TagManager.instance.local_domain?(domain)
|
||||
results = { accounts: [], hashtags: [], statuses: [] }
|
||||
|
||||
if domain.nil?
|
||||
exact_match = Account.find_local(username)
|
||||
results = account.nil? ? Account.search_for(username, limit) : Account.advanced_search_for(username, account, limit)
|
||||
if query =~ /\Ahttps?:\/\//
|
||||
resource = FetchRemoteResourceService.new.call(query)
|
||||
|
||||
results[:accounts] << resource if resource.is_a?(Account)
|
||||
results[:statuses] << resource if resource.is_a?(Status)
|
||||
else
|
||||
exact_match = Account.find_remote(username, domain)
|
||||
results = account.nil? ? Account.search_for("#{username} #{domain}", limit) : Account.advanced_search_for("#{username} #{domain}", account, limit)
|
||||
end
|
||||
|
||||
results = [exact_match] + results.reject { |a| a.id == exact_match.id } if exact_match
|
||||
|
||||
if resolve && !exact_match && !domain.nil?
|
||||
results = [FollowRemoteAccountService.new.call("#{username}@#{domain}")]
|
||||
results[:accounts] = AccountSearchService.new.call(query, limit, resolve, account)
|
||||
results[:hashtags] = Tag.search_for(query.gsub(/\A#/, ''), limit) unless query.start_with?('@')
|
||||
end
|
||||
|
||||
results
|
||||
|
Reference in New Issue
Block a user