about summary refs log tree commit diff
path: root/app/services/bootstrap_timeline_service.rb
blob: db2c83e5d1ced84ab75a70fdd61cd257ec498bb8 (plain) (blame)
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
# frozen_string_literal: true

class BootstrapTimelineService < BaseService
  def call(source_account)
    @source_account = source_account

    autofollow_inviter!
    autofollow_bootstrap_timeline_accounts!
  end

  private

  def autofollow_inviter!
    return unless @source_account&.user&.invite&.autofollow?
    FollowService.new.call(@source_account, @source_account.user.invite.user.account)
  end

  def autofollow_bootstrap_timeline_accounts!
    bootstrap_timeline_accounts.each do |target_account|
      FollowService.new.call(@source_account, target_account)
    end
  end

  def bootstrap_timeline_accounts
    return @bootstrap_timeline_accounts if defined?(@bootstrap_timeline_accounts)

    @bootstrap_timeline_accounts = bootstrap_timeline_accounts_usernames.empty? ? admin_accounts : local_unlocked_accounts(bootstrap_timeline_accounts_usernames)
  end

  def bootstrap_timeline_accounts_usernames
    @bootstrap_timeline_accounts_usernames ||= (Setting.bootstrap_timeline_accounts || '').split(',').map { |str| str.strip.gsub(/\A@/, '') }.reject(&:blank?)
  end

  def admin_accounts
    User.admins
        .includes(:account)
        .where(accounts: { locked: false })
        .map(&:account)
  end

  def local_unlocked_accounts(usernames)
    Account.local
           .where(username: usernames)
           .where(locked: false)
  end
end