闭社主体 forked from https://github.com/tootsuite/mastodon
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
1.3 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. return subscription.destroy! if response.code > 299 && response.code < 500 && response.code != 429 # HTTP 4xx means error is not temporary, except for 429 (throttling)
  19. raise "Delivery failed for #{subscription.callback_url}: HTTP #{response.code}" unless response.code > 199 && response.code < 300
  20. subscription.touch(:last_successful_delivery_at)
  21. end
  22. private
  23. def signature(secret, payload)
  24. hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), secret, payload)
  25. "sha1=#{hmac}"
  26. end
  27. end