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.

40 lines
1.5 KiB

  1. # frozen_string_literal: true
  2. class Pubsubhubbub::DeliveryWorker
  3. include Sidekiq::Worker
  4. include RoutingHelper
  5. sidekiq_options queue: 'push', retry: 3, dead: false
  6. sidekiq_retry_in do |count|
  7. 5 * (count + 1)
  8. end
  9. def perform(subscription_id, payload)
  10. subscription = Subscription.find(subscription_id)
  11. headers = {}
  12. host = Addressable::URI.parse(subscription.callback_url).host
  13. return if DomainBlock.blocked?(host)
  14. headers['User-Agent'] = 'Mastodon/PubSubHubbub'
  15. headers['Link'] = LinkHeader.new([[api_push_url, [%w(rel hub)]], [account_url(subscription.account, format: :atom), [%w(rel self)]]]).to_s
  16. headers['X-Hub-Signature'] = signature(subscription.secret, payload) unless subscription.secret.blank?
  17. response = HTTP.timeout(:per_operation, write: 50, connect: 20, read: 50)
  18. .headers(headers)
  19. .post(subscription.callback_url, body: payload)
  20. return subscription.destroy! if response.code > 299 && response.code < 500 && response.code != 429 # HTTP 4xx means error is not temporary, except for 429 (throttling)
  21. raise "Delivery failed for #{subscription.callback_url}: HTTP #{response.code}" unless response.code > 199 && response.code < 300
  22. subscription.touch(:last_successful_delivery_at)
  23. end
  24. private
  25. def signature(secret, payload)
  26. hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), secret, payload)
  27. "sha1=#{hmac}"
  28. end
  29. end