~cytrogen/masto-fe

ref: 33c8708a1ac7df363bf2bd74ab8fa2ed7168379c masto-fe/app/controllers/api/v1/directories_controller.rb -rw-r--r-- 1.3 KiB
33c8708a — Claire Change `GET /api/v1/directory` to use database replica rather than primary (#26856) 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
# frozen_string_literal: true

class Api::V1::DirectoriesController < Api::BaseController
  before_action :require_enabled!
  before_action :set_accounts

  def show
    cache_if_unauthenticated!
    render json: @accounts, each_serializer: REST::AccountSerializer
  end

  private

  def require_enabled!
    return not_found unless Setting.profile_directory
  end

  def set_accounts
    with_read_replica do
      @accounts = accounts_scope.offset(params[:offset]).limit(limit_param(DEFAULT_ACCOUNTS_LIMIT))
    end
  end

  def accounts_scope
    Account.discoverable.tap do |scope|
      scope.merge!(account_order_scope)
      scope.merge!(local_account_scope) if local_accounts?
      scope.merge!(account_exclusion_scope) if current_account
      scope.merge!(account_domain_block_scope) if current_account && !local_accounts?
    end
  end

  def local_accounts?
    truthy_param?(:local)
  end

  def account_order_scope
    case params[:order]
    when 'new'
      Account.order(id: :desc)
    when 'active', nil
      Account.by_recent_status
    end
  end

  def local_account_scope
    Account.local
  end

  def account_exclusion_scope
    Account.not_excluded_by_account(current_account)
  end

  def account_domain_block_scope
    Account.not_domain_blocked_by_account(current_account)
  end
end