~cytrogen/masto-fe

ref: e754083e8a33778b5dd8d43efc5604ed50efb0e4 masto-fe/app/workers/webhooks/delivery_worker.rb -rw-r--r-- 985 bytes
e754083e — Eugen Rochko Fix unmatched quotes and prefixes causing search to fail (#26701) 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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      = @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