about summary refs log tree commit diff
path: root/app/workers/scheduler/werewolf_scheduler.rb
blob: 6b5764887b69b5553ebddaaa4a570682d3373db2 (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
# frozen_string_literal: true

class Scheduler::WerewolfScheduler
  include Sidekiq::Worker
  include Redisable

  STATUS = ENV.fetch('WEREWOLF_STATUS', 'Werewolves successful.')
  FOOTER = ENV.fetch('WEREWOLF_FOOTER', ':werewolf: werewolf-status')

  sidekiq_options unique: :until_executed

  def perform
    return if redis.exists('werewolf-status')
    return unless Setting.werewolf_status

    moon_fraction = SunCalc.moon_illumination(Time.now.utc)[:fraction]

    return unless moon_fraction >= 0.998

    redis.setex('werewolf-status', 1.day, 1)

    announcer = find_announcer_acct
    return if announcer.nil?

    s = PostStatusService.new.call(
      announcer,
      visibility: :public,
      text: STATUS,
      footer: FOOTER,
      content_type: 'text/console',
    )

    DistributionWorker.perform_async(s.id)
    ActivityPub::DistributionWorker.perform_async(s)
  end

  private

  def find_announcer_acct
    announcer = ENV['ANNOUNCEMENTS_USER'].to_i
    return if announcer == 0
    Account.find_by(id: announcer)
  end
end