delivery_worker.rb 875 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # frozen_string_literal: true
  2. class Webhooks::DeliveryWorker
  3. include Sidekiq::Worker
  4. include JsonLdHelper
  5. sidekiq_options queue: 'push', retry: 16, dead: false
  6. def perform(webhook_id, body)
  7. @webhook = Webhook.find(webhook_id)
  8. @body = body
  9. @response = nil
  10. perform_request
  11. rescue ActiveRecord::RecordNotFound
  12. true
  13. end
  14. private
  15. def perform_request
  16. request = Request.new(:post, @webhook.url, body: @body)
  17. request.add_headers(
  18. 'Content-Type' => 'application/json',
  19. 'X-Hub-Signature' => "sha256=#{signature}"
  20. )
  21. request.perform do |response|
  22. raise Mastodon::UnexpectedResponseError, response unless response_successful?(response) || response_error_unsalvageable?(response)
  23. end
  24. end
  25. def signature
  26. OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @webhook.secret, @body)
  27. end
  28. end