about summary refs log tree commit diff
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-11-12 14:49:28 +0100
committerEugen Rochko <eugen@zeonfederated.com>2016-11-12 14:49:28 +0100
commitafded319d2558369a025b1f3335cebacc9819881 (patch)
tree2e5c0cae17213c46faff3e25a07922338d0f1ce1
parent09218d4c0152013750dd1c127d3c8267dc45f880 (diff)
Add limit to search results
-rw-r--r--app/assets/javascripts/components/actions/compose.jsx3
-rw-r--r--app/controllers/api/v1/accounts_controller.rb3
-rw-r--r--app/services/search_service.rb20
3 files changed, 12 insertions, 14 deletions
diff --git a/app/assets/javascripts/components/actions/compose.jsx b/app/assets/javascripts/components/actions/compose.jsx
index c9be895f1..b77a9c727 100644
--- a/app/assets/javascripts/components/actions/compose.jsx
+++ b/app/assets/javascripts/components/actions/compose.jsx
@@ -148,7 +148,8 @@ export function fetchComposeSuggestions(token) {
     api(getState).get('/api/v1/accounts/search', {
       params: {
         q: token,
-        resolve: false
+        resolve: false,
+        limit: 4
       }
     }).then(response => {
       dispatch(readyComposeSuggestions(token, response.data));
diff --git a/app/controllers/api/v1/accounts_controller.rb b/app/controllers/api/v1/accounts_controller.rb
index 9b02c9981..9573326ef 100644
--- a/app/controllers/api/v1/accounts_controller.rb
+++ b/app/controllers/api/v1/accounts_controller.rb
@@ -92,7 +92,8 @@ class Api::V1::AccountsController < ApiController
   end
 
   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
   end
 
diff --git a/app/services/search_service.rb b/app/services/search_service.rb
index d9b627853..9e8ee6220 100644
--- a/app/services/search_service.rb
+++ b/app/services/search_service.rb
@@ -1,25 +1,21 @@
 class SearchService < BaseService
-  def call(query, resolve = false)
+  def call(query, limit, resolve = false)
     return if query.blank?
 
     username, domain = query.split('@')
 
-    if domain.nil?
-      search_all(username)
+    results = if domain.nil?
+      Account.search_for(username)
     else
-      search_or_resolve(username, domain, resolve)
+      Account.search_for("#{username} #{domain}")
     end
-  end
 
-  private
+    results = results.limit(limit).with_counters
 
-  def search_all(username)
-    Account.search_for(username)
-  end
+    if resolve && results.empty? && !domain.nil?
+      results = [FollowRemoteAccountService.new.call("#{username}@#{domain}")]
+    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
   end
 end