This repository has been archived on 2024-07-22. You can view files and clone it, but cannot push or open issues or pull requests.
2022-06-09 12:57:36 -07:00
|
|
|
# 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)
|
2023-06-06 01:42:47 -07:00
|
|
|
@body = @webhook.template.blank? ? body : Webhooks::PayloadRenderer.new(body).render(@webhook.template)
|
2022-06-09 12:57:36 -07:00
|
|
|
@response = nil
|
|
|
|
|
|
|
|
perform_request
|
|
|
|
rescue ActiveRecord::RecordNotFound
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def perform_request
|
2023-03-18 23:47:54 -07:00
|
|
|
request = Request.new(:post, @webhook.url, body: @body, allow_local: true)
|
2022-06-09 12:57:36 -07:00
|
|
|
|
|
|
|
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
|