闭社主体 forked from https://github.com/tootsuite/mastodon
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.3 KiB

  1. # frozen_string_literal: true
  2. class Pubsubhubbub::ConfirmationWorker
  3. include Sidekiq::Worker
  4. include RoutingHelper
  5. sidekiq_options queue: 'push'
  6. def perform(subscription_id, mode, secret = nil, lease_seconds = nil)
  7. subscription = Subscription.find(subscription_id)
  8. challenge = SecureRandom.hex
  9. subscription.secret = secret
  10. subscription.lease_seconds = lease_seconds
  11. subscription.confirmed = true
  12. response = HTTP.headers(user_agent: 'Mastodon/PubSubHubbub')
  13. .timeout(:per_operation, write: 20, connect: 20, read: 50)
  14. .get(subscription.callback_url, params: {
  15. 'hub.topic' => account_url(subscription.account, format: :atom),
  16. 'hub.mode' => mode,
  17. 'hub.challenge' => challenge,
  18. 'hub.lease_seconds' => subscription.lease_seconds,
  19. })
  20. body = response.body.to_s
  21. Rails.logger.debug "Confirming PuSH subscription for #{subscription.callback_url} with challenge #{challenge}: #{body}"
  22. if mode == 'subscribe' && body == challenge
  23. subscription.save!
  24. elsif (mode == 'unsubscribe' && body == challenge) || !subscription.confirmed?
  25. subscription.destroy!
  26. end
  27. end
  28. end