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

class PushUpdateWorker
  include Sidekiq::Worker
  include Redisable

  def perform(account_id, status_id, timeline_id = nil, options = {})
    @account     = Account.find(account_id)
    @status      = Status.includes(active_mentions: :account, reblog: { active_mentions: :account }).find(status_id)
    @timeline_id = timeline_id || "timeline:#{account.id}"
    @options     = options.symbolize_keys

    publish!
  rescue ActiveRecord::RecordNotFound
    true
  end

  private

  def payload
    InlineRenderer.render(@status, @account, :status)
  end

  def message
    Oj.dump(
      event: update? ? :'status.update' : :update,
      payload: payload,
      queued_at: (Time.now.to_f * 1000.0).to_i
    )
  end

  def publish!
    redis.publish(@timeline_id, message)
  end

  def update?
    @options[:update]
  end
end