Add administrative webhooks (#18510)
* Add administrative webhooks * Fix error when webhook is deleted before delivery worker runs
This commit is contained in:
parent
17ba5e1e61
commit
a2871cd747
33 changed files with 530 additions and 8 deletions
37
app/workers/webhooks/delivery_worker.rb
Normal file
37
app/workers/webhooks/delivery_worker.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
# 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 = body
|
||||
@response = nil
|
||||
|
||||
perform_request
|
||||
rescue ActiveRecord::RecordNotFound
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def perform_request
|
||||
request = Request.new(:post, @webhook.url, body: @body)
|
||||
|
||||
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
|
Loading…
Add table
Add a link
Reference in a new issue