2017-03-21 18:32:27 -07:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-07 11:09:25 -07:00
|
|
|
class Api::V1::SearchController < Api::BaseController
|
2017-11-14 16:53:33 -08:00
|
|
|
include Authorization
|
|
|
|
|
2017-07-15 06:42:24 -07:00
|
|
|
RESULTS_LIMIT = 10
|
2017-05-30 18:11:54 -07:00
|
|
|
|
2017-07-11 08:08:26 -07:00
|
|
|
before_action -> { doorkeeper_authorize! :read }
|
|
|
|
before_action :require_user!
|
|
|
|
|
2017-03-21 18:32:27 -07:00
|
|
|
respond_to :json
|
|
|
|
|
|
|
|
def index
|
2017-11-14 16:53:33 -08:00
|
|
|
@search = Search.new(search)
|
2017-07-06 19:02:06 -07:00
|
|
|
render json: @search, serializer: REST::SearchSerializer
|
2017-05-30 18:11:54 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2017-11-14 16:53:33 -08:00
|
|
|
def search
|
|
|
|
search_results.tap do |search|
|
|
|
|
search[:statuses].keep_if do |status|
|
|
|
|
begin
|
|
|
|
authorize status, :show?
|
|
|
|
rescue Mastodon::NotPermittedError
|
|
|
|
false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-30 18:11:54 -07:00
|
|
|
def search_results
|
|
|
|
SearchService.new.call(
|
|
|
|
params[:q],
|
|
|
|
RESULTS_LIMIT,
|
|
|
|
resolving_search?,
|
|
|
|
current_account
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def resolving_search?
|
|
|
|
params[:resolve] == 'true'
|
2017-03-21 18:32:27 -07:00
|
|
|
end
|
|
|
|
end
|