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

class PublishScheduledStatusWorker
  include Sidekiq::Worker

  sidekiq_options lock: :until_executed

  def perform(scheduled_status_id)
    scheduled_status = ScheduledStatus.find(scheduled_status_id)
    scheduled_status.destroy!

    PostStatusService.new.call(
      scheduled_status.account,
      options_with_objects(scheduled_status.params.with_indifferent_access)
    )
  rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid
    true
  end

  def options_with_objects(options)
    options.tap do |options_hash|
      options_hash[:application] = Doorkeeper::Application.find(options_hash.delete(:application_id)) if options[:application_id]
      options_hash[:thread]      = Status.find(options_hash.delete(:in_reply_to_id)) if options_hash[:in_reply_to_id]
      options_hash[:mentions]    = Mention.where(id: options_hash.delete(:mention_ids)) if options_hash[:mention_ids]
      options_hash[:status]      = Status.find_by(id: options_hash.delete(:status_id)) if options_hash[:status_id]
    end
  end
end