~cytrogen/masto-fe

ref: be991f1d18006a4820c1e9ca6625bf2bd2bfedac masto-fe/app/services/statuses_search_service.rb -rw-r--r-- 1.6 KiB
be991f1d — Gabriel Simmer Move to ioredis for streaming (#26581) 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# frozen_string_literal: true

class StatusesSearchService < BaseService
  def call(query, account = nil, options = {})
    @query   = query&.strip
    @account = account
    @options = options
    @limit   = options[:limit].to_i
    @offset  = options[:offset].to_i

    status_search_results
  end

  private

  def status_search_results
    definition = parsed_query.apply(
      Chewy::Search::Request.new(StatusesIndex, PublicStatusesIndex).filter(
        bool: {
          should: [
            publicly_searchable,
            non_publicly_searchable,
          ],

          minimum_should_match: 1,
        }
      )
    )

    results             = definition.collapse(field: :id).order(id: { order: :desc }).limit(@limit).offset(@offset).objects.compact
    account_ids         = results.map(&:account_id)
    account_domains     = results.map(&:account_domain)
    preloaded_relations = @account.relations_map(account_ids, account_domains)

    results.reject { |status| StatusFilter.new(status, @account, preloaded_relations).filtered? }
  rescue Faraday::ConnectionFailed, Parslet::ParseFailed
    []
  end

  def publicly_searchable
    {
      term: { _index: PublicStatusesIndex.index_name },
    }
  end

  def non_publicly_searchable
    {
      bool: {
        must: [
          {
            term: { _index: StatusesIndex.index_name },
          },
          {
            term: { searchable_by: @account.id },
          },
        ],
      },
    }
  end

  def parsed_query
    SearchQueryTransformer.new.apply(SearchQueryParser.new.parse(@query), current_account: @account)
  end
end