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.

36 lines
1.2 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. headers['User-Agent'] = 'Mastodon/PubSubHubbub'
  13. headers['Link'] = LinkHeader.new([[api_push_url, [%w(rel hub)]], [account_url(subscription.account, format: :atom), [%w(rel self)]]]).to_s
  14. headers['X-Hub-Signature'] = signature(subscription.secret, payload) unless subscription.secret.blank?
  15. response = HTTP.timeout(:per_operation, write: 50, connect: 20, read: 50)
  16. .headers(headers)
  17. .post(subscription.callback_url, body: payload)
  18. raise "Delivery failed for #{subscription.callback_url}: HTTP #{response.code}" unless response.code > 199 && response.code < 300
  19. subscription.touch(:last_successful_delivery_at)
  20. end
  21. private
  22. def signature(secret, payload)
  23. hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), secret, payload)
  24. "sha1=#{hmac}"
  25. end
  26. end