Add limit to search results

This commit is contained in:
Eugen Rochko 2016-11-12 14:49:28 +01:00
parent 09218d4c01
commit afded319d2
3 changed files with 12 additions and 14 deletions

View File

@ -148,7 +148,8 @@ export function fetchComposeSuggestions(token) {
api(getState).get('/api/v1/accounts/search', { api(getState).get('/api/v1/accounts/search', {
params: { params: {
q: token, q: token,
resolve: false resolve: false,
limit: 4
} }
}).then(response => { }).then(response => {
dispatch(readyComposeSuggestions(token, response.data)); dispatch(readyComposeSuggestions(token, response.data));

View File

@ -92,7 +92,8 @@ class Api::V1::AccountsController < ApiController
end end
def search def search
@accounts = SearchService.new.call(params[:q], params[:resolve] == 'true') limit = params[:limit] ? [DEFAULT_ACCOUNTS_LIMIT, params[:limit].to_i].min : DEFAULT_ACCOUNTS_LIMIT
@accounts = SearchService.new.call(params[:q], limit, params[:resolve] == 'true')
render action: :index render action: :index
end end

View File

@ -1,25 +1,21 @@
class SearchService < BaseService class SearchService < BaseService
def call(query, resolve = false) def call(query, limit, resolve = false)
return if query.blank? return if query.blank?
username, domain = query.split('@') username, domain = query.split('@')
if domain.nil? results = if domain.nil?
search_all(username)
else
search_or_resolve(username, domain, resolve)
end
end
private
def search_all(username)
Account.search_for(username) Account.search_for(username)
else
Account.search_for("#{username} #{domain}")
end
results = results.limit(limit).with_counters
if resolve && results.empty? && !domain.nil?
results = [FollowRemoteAccountService.new.call("#{username}@#{domain}")]
end end
def search_or_resolve(username, domain, resolve)
results = Account.search_for("#{username} #{domain}")
return [FollowRemoteAccountService.new.call("#{username}@#{domain}")] if results.empty? && resolve
results results
end end
end end