You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

37 lines
905 B

  1. # frozen_string_literal: true
  2. class ActivityPub::DeliveryWorker
  3. include Sidekiq::Worker
  4. sidekiq_options queue: 'push', retry: 5, dead: false
  5. HEADERS = { 'Content-Type' => 'application/activity+json' }.freeze
  6. def perform(json, source_account_id, inbox_url)
  7. @json = json
  8. @source_account = Account.find(source_account_id)
  9. @inbox_url = inbox_url
  10. perform_request
  11. raise Mastodon::UnexpectedResponseError, @response unless response_successful?
  12. rescue => e
  13. raise e.class, "Delivery failed for #{inbox_url}: #{e.message}"
  14. end
  15. private
  16. def build_request
  17. request = Request.new(:post, @inbox_url, body: @json)
  18. request.on_behalf_of(@source_account, :uri)
  19. request.add_headers(HEADERS)
  20. end
  21. def perform_request
  22. @response = build_request.perform
  23. end
  24. def response_successful?
  25. @response.code > 199 && @response.code < 300
  26. end
  27. end