2016-11-28 13:36:47 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Pubsubhubbub::ConfirmationWorker
|
|
|
|
include Sidekiq::Worker
|
|
|
|
include RoutingHelper
|
|
|
|
|
2017-01-05 03:28:21 +01:00
|
|
|
sidekiq_options queue: 'push', retry: false
|
2016-11-29 02:07:14 +01:00
|
|
|
|
2016-11-28 13:36:47 +01:00
|
|
|
def perform(subscription_id, mode, secret = nil, lease_seconds = nil)
|
|
|
|
subscription = Subscription.find(subscription_id)
|
|
|
|
challenge = SecureRandom.hex
|
|
|
|
|
|
|
|
subscription.secret = secret
|
|
|
|
subscription.lease_seconds = lease_seconds
|
2016-11-28 19:24:49 +01:00
|
|
|
subscription.confirmed = true
|
2016-11-28 13:36:47 +01:00
|
|
|
|
|
|
|
response = HTTP.headers(user_agent: 'Mastodon/PubSubHubbub')
|
|
|
|
.timeout(:per_operation, write: 20, connect: 20, read: 50)
|
|
|
|
.get(subscription.callback_url, params: {
|
|
|
|
'hub.topic' => account_url(subscription.account, format: :atom),
|
|
|
|
'hub.mode' => mode,
|
|
|
|
'hub.challenge' => challenge,
|
|
|
|
'hub.lease_seconds' => subscription.lease_seconds,
|
|
|
|
})
|
|
|
|
|
2016-11-28 19:15:28 +01:00
|
|
|
body = response.body.to_s
|
|
|
|
|
2017-05-06 13:05:03 +02:00
|
|
|
logger.debug "Confirming PuSH subscription for #{subscription.callback_url} with challenge #{challenge}: #{body}"
|
2016-11-28 19:15:28 +01:00
|
|
|
|
|
|
|
if mode == 'subscribe' && body == challenge
|
2016-11-28 13:36:47 +01:00
|
|
|
subscription.save!
|
2016-11-28 19:15:28 +01:00
|
|
|
elsif (mode == 'unsubscribe' && body == challenge) || !subscription.confirmed?
|
2016-11-28 13:36:47 +01:00
|
|
|
subscription.destroy!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|