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.

48 lines
1.2 KiB

  1. # frozen_string_literal: true
  2. class ActivityPub::DeliveryWorker
  3. include Sidekiq::Worker
  4. sidekiq_options queue: 'push', retry: 16, 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. failure_tracker.track_success!
  12. rescue => e
  13. failure_tracker.track_failure!
  14. raise e.class, "Delivery failed for #{inbox_url}: #{e.message}", e.backtrace[0]
  15. end
  16. private
  17. def build_request
  18. request = Request.new(:post, @inbox_url, body: @json)
  19. request.on_behalf_of(@source_account, :uri)
  20. request.add_headers(HEADERS)
  21. end
  22. def perform_request
  23. light = Stoplight(@inbox_url) do
  24. build_request.perform do |response|
  25. raise Mastodon::UnexpectedResponseError, response unless response_successful?(response)
  26. end
  27. end
  28. light.run
  29. end
  30. def response_successful?(response)
  31. response.code > 199 && response.code < 300
  32. end
  33. def failure_tracker
  34. @failure_tracker ||= DeliveryFailureTracker.new(@inbox_url)
  35. end
  36. end