mirror of
https://git.kescher.at/CatCatNya/catstodon.git
synced 2024-12-05 02:09:03 +01:00
4eda233e09
Co-authored-by: Claire <claire.github-309c@sitedethib.com>
37 lines
985 B
Ruby
37 lines
985 B
Ruby
# frozen_string_literal: true
|
|
|
|
class Webhooks::DeliveryWorker
|
|
include Sidekiq::Worker
|
|
include JsonLdHelper
|
|
|
|
sidekiq_options queue: 'push', retry: 16, dead: false
|
|
|
|
def perform(webhook_id, body)
|
|
@webhook = Webhook.find(webhook_id)
|
|
@body = @webhook.template.blank? ? body : Webhooks::PayloadRenderer.new(body).render(@webhook.template)
|
|
@response = nil
|
|
|
|
perform_request
|
|
rescue ActiveRecord::RecordNotFound
|
|
true
|
|
end
|
|
|
|
private
|
|
|
|
def perform_request
|
|
request = Request.new(:post, @webhook.url, body: @body, allow_local: true)
|
|
|
|
request.add_headers(
|
|
'Content-Type' => 'application/json',
|
|
'X-Hub-Signature' => "sha256=#{signature}"
|
|
)
|
|
|
|
request.perform do |response|
|
|
raise Mastodon::UnexpectedResponseError, response unless response_successful?(response) || response_error_unsalvageable?(response)
|
|
end
|
|
end
|
|
|
|
def signature
|
|
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.new('sha256'), @webhook.secret, @body)
|
|
end
|
|
end
|