logo

mastofe

My custom branche(s) on git.pleroma.social/pleroma/mastofe git clone https://hacktivis.me/git/mastofe.git

confirmation_worker.rb (1899B)


  1. # frozen_string_literal: true
  2. class Pubsubhubbub::ConfirmationWorker
  3. include Sidekiq::Worker
  4. include RoutingHelper
  5. sidekiq_options queue: 'push', retry: false
  6. attr_reader :subscription, :mode, :secret, :lease_seconds
  7. def perform(subscription_id, mode, secret = nil, lease_seconds = nil)
  8. @subscription = Subscription.find(subscription_id)
  9. @mode = mode
  10. @secret = secret
  11. @lease_seconds = lease_seconds
  12. process_confirmation
  13. end
  14. private
  15. def process_confirmation
  16. prepare_subscription
  17. callback_get_with_params
  18. logger.debug "Confirming PuSH subscription for #{subscription.callback_url} with challenge #{challenge}: #{@callback_response_body}"
  19. update_subscription
  20. end
  21. def update_subscription
  22. if successful_subscribe?
  23. subscription.save!
  24. elsif successful_unsubscribe?
  25. subscription.destroy!
  26. end
  27. end
  28. def successful_subscribe?
  29. subscribing? && response_matches_challenge?
  30. end
  31. def successful_unsubscribe?
  32. (unsubscribing? && response_matches_challenge?) || !subscription.confirmed?
  33. end
  34. def response_matches_challenge?
  35. @callback_response_body == challenge
  36. end
  37. def subscribing?
  38. mode == 'subscribe'
  39. end
  40. def unsubscribing?
  41. mode == 'unsubscribe'
  42. end
  43. def callback_get_with_params
  44. Request.new(:get, subscription.callback_url, params: callback_params).perform do |response|
  45. @callback_response_body = response.body_with_limit
  46. end
  47. end
  48. def callback_params
  49. {
  50. 'hub.topic': account_url(subscription.account, format: :atom),
  51. 'hub.mode': mode,
  52. 'hub.challenge': challenge,
  53. 'hub.lease_seconds': subscription.lease_seconds,
  54. }
  55. end
  56. def prepare_subscription
  57. subscription.secret = secret
  58. subscription.lease_seconds = lease_seconds
  59. subscription.confirmed = true
  60. end
  61. def challenge
  62. @_challenge ||= SecureRandom.hex
  63. end
  64. end