about summary refs log tree commit diff
path: root/app/workers/scheduler/ambassador_scheduler.rb
blob: bedf0f08a50e401749e99bf4999127cbfff8f204 (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
56
57
# 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?

    ReblogStatusWorker.perform_async(@ambassador.id, status.id)
  end

  private

  def find_ambassador_acct
    ambassador = ENV['AMBASSADOR_USER'].to_i
    return if ambassador.zero?
    Account.find_by(id: 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
    query = ambassador_unboosted.excluding_silenced_accounts.not_excluded_by_account(@ambassador)

    unless !@ambassador.user.filters_enabled || @ambassador.custom_filters.enabled.blank?
      if @ambassador.user.invert_filters
        query = query.search_filtered_by_account(@ambassador.id)
      else
        query = query.search_not_filtered_by_account(@ambassador.id)
      end
    end

    query
  end

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

  def ambassador_boosts
    @ambassador.statuses.reblogs.reorder(nil).select(:reblog_of_id)
  end

  def locally_boostable
    Status.local.without_reblogs.without_replies.public_local_visibility
  end
end