about summary refs log tree commit diff
path: root/app/workers/scheduler/ambassador_scheduler.rb
blob: b7361821f3cb04918423841e072e1c95a09e9248 (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
47
48
49
50
51
52
53
54
55
# frozen_string_literal: true

class Scheduler::AmbassadorScheduler
  include Sidekiq::Worker

  def perform
    @ambassador = find_ambassador_acct
    return if @ambassador.nil?

    status = next_boost
    return if status.nil?

    ReblogService.new.call(@ambassador, status)
  end

  private

  def find_ambassador_acct
    ambassador = ENV['AMBASSADOR_USER'].to_i
    return Account.find_by(id: ambassador) unless ambassador.zero?

    ambassador = ENV['AMBASSADOR_USER']
    return if ambassador.blank?

    Account.find_local(ambassador)
  end

  def next_boost
    ambassador_boost_candidates.first
  end

  def ambassador_boost_candidates
    ambassador_boostable.joins(:status_stat).where('favourites_count + reblogs_count > 4')
  end

  def ambassador_boostable
    ambassador_unboosted.excluding_silenced_accounts.not_excluded_by_account(@ambassador)
  end

  def ambassador_unboosted
    locally_boostable.where.not(id: ambassador_boosts)
  end

  def ambassador_boosts
    @ambassador.statuses.where('statuses.reblog_of_id IS NOT NULL').reorder(nil).select(:reblog_of_id)
  end

  def locally_boostable
    Status.local
      .public_visibility
      .without_replies
      .without_reblogs
      .where('statuses.created_at > ?', 18.weeks.ago)
  end
end