about summary refs log tree commit diff
path: root/app/services
diff options
context:
space:
mode:
authorJenkins <jenkins@jenkins.ninjawedding.org>2018-02-11 04:10:16 +0000
committerJenkins <jenkins@jenkins.ninjawedding.org>2018-02-11 04:10:16 +0000
commitefc9ffcb80b995ef8c8d95ab1d8cb0af8efc094d (patch)
tree6ce57e5980ba0279355f97893d21852caace0232 /app/services
parentb6159bcb0a20f3aabfb4d17ad60842b7aa81925c (diff)
parent718802a05dfb3211d758513daf6070ffa22751dd (diff)
Merge remote-tracking branch 'tootsuite/master' into glitchsoc/master
Diffstat (limited to 'app/services')
-rw-r--r--app/services/search_service.rb43
1 files changed, 39 insertions, 4 deletions
diff --git a/app/services/search_service.rb b/app/services/search_service.rb
index 5f763b8f7..fe9856686 100644
--- a/app/services/search_service.rb
+++ b/app/services/search_service.rb
@@ -1,21 +1,43 @@
 # frozen_string_literal: true
 
 class SearchService < BaseService
-  attr_accessor :query
+  attr_accessor :query, :account, :limit, :resolve
 
   def call(query, limit, resolve = false, account = nil)
-    @query = query
+    @query   = query
+    @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] = AccountSearchService.new.call(query, limit, account, resolve: resolve)
-        results[:hashtags] = Tag.search_for(query.gsub(/\A#/, ''), limit) unless query.start_with?('@')
+        results[:accounts] = perform_accounts_search! if account_searchable?
+        results[:statuses] = perform_statuses_search! if full_text_searchable?
+        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_statuses_search!
+    statuses = StatusesIndex.filter(term: { searchable_by: account.id })
+                            .query(multi_match: { type: 'most_fields', query: query, operator: 'and', fields: %w(text text.stemmed) })
+                            .limit(limit).objects
+
+    statuses.reject { |status| StatusFilter.new(status, account).filtered? }
+  end
+
+  def perform_hashtags_search!
+    Tag.search_for(query.gsub(/\A#/, ''), limit)
+  end
+
   def default_results
     { accounts: [], hashtags: [], statuses: [] }
   end
@@ -35,4 +57,17 @@ class SearchService < BaseService
   def url_resource_symbol
     url_resource.class.name.downcase.pluralize.to_sym
   end
+
+  def full_text_searchable?
+    return false unless Chewy.enabled?
+    !account.nil? && !((query.start_with?('#') || query.include?('@')) && !query.include?(' '))
+  end
+
+  def account_searchable?
+    !(query.include?('@') && query.include?(' '))
+  end
+
+  def hashtag_searchable?
+    !query.include?('@')
+  end
 end