闭社主体 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.

32 lines
1.1 KiB

  1. # frozen_string_literal: true
  2. class Pubsubhubbub::DeliveryWorker
  3. include Sidekiq::Worker
  4. include RoutingHelper
  5. sidekiq_options queue: 'push', retry: 5
  6. def perform(subscription_id, payload)
  7. subscription = Subscription.find(subscription_id)
  8. headers = {}
  9. headers['User-Agent'] = 'Mastodon/PubSubHubbub'
  10. headers['Link'] = LinkHeader.new([[api_push_url, [%w(rel hub)]], [account_url(subscription.account, format: :atom), [%w(rel self)]]]).to_s
  11. headers['X-Hub-Signature'] = signature(subscription.secret, payload) unless subscription.secret.blank?
  12. response = HTTP.timeout(:per_operation, write: 50, connect: 20, read: 50)
  13. .headers(headers)
  14. .post(subscription.callback_url, body: payload)
  15. raise "Delivery failed for #{subscription.callback_url}: HTTP #{response.code}" unless response.code > 199 && response.code < 300
  16. subscription.touch(:last_successful_delivery_at)
  17. end
  18. private
  19. def signature(secret, payload)
  20. hmac = OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha1'), secret, payload)
  21. "sha1=#{hmac}"
  22. end
  23. end