Created
February 18, 2019 20:03
-
-
Save karasugawasu/8602251fee3439e3bcdaee433ad57c9b to your computer and use it in GitHub Desktop.
画像の説明を検索できるように・・・・
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SearchService < BaseService | |
attr_accessor :query, :account, :limit, :resolve | |
def call(query, limit, resolve = false, account = nil) | |
@query = query.strip | |
@account = account | |
@limit = limit | |
@resolve = resolve | |
default_results.tap do |results| | |
if url_query? | |
results.merge!(url_resource_results) unless url_resource.nil? | |
elsif query.present? | |
results[:accounts] = perform_accounts_search! if account_searchable? | |
results[:statuses] = perform_statuses_search! | |
results[:statuses].concat(perform_mediadescription_search!) #ここ | |
results[:statuses].uniq! #ここ | |
results[:hashtags] = perform_hashtags_search! if hashtag_searchable? | |
end | |
end | |
end | |
private | |
def perform_accounts_search! | |
AccountSearchService.new.call(query, limit, account, resolve: resolve) | |
end | |
#ここ | |
def perform_mediadescription_search! | |
mediades = Status.joins(:media_attachments) | |
.select('statuses.*') | |
.where('local = True') | |
.limit(limit) | |
query.split(/[\s ]+/).each do |keyword| | |
if (matches = keyword.match(/^-(.*)/)) | |
keyword = matches[1] | |
mediades = mediades.where('description !~ ?', keyword) | |
else | |
mediades = mediades.where('description ~ ?', keyword) | |
end | |
end | |
mediades.reject { |status| StatusFilter.new(status, account).filtered? } | |
rescue Faraday::ConnectionFailed | |
[] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment