1
0
mirror of https://github.com/mastodon/mastodon synced 2024-12-28 21:48:13 +09:00
mastodon/app/services/pubsubhubbub/subscribe_service.rb

54 lines
1.3 KiB
Ruby
Raw Normal View History

2016-11-28 21:36:47 +09:00
# frozen_string_literal: true
class Pubsubhubbub::SubscribeService < BaseService
URL_PATTERN = /\A#{URI.regexp(%w(http https))}\z/
attr_reader :account, :callback, :secret,
:lease_seconds, :domain
def call(account, callback, secret, lease_seconds, verified_domain = nil)
@account = account
@callback = Addressable::URI.parse(callback).normalize.to_s
@secret = secret
@lease_seconds = lease_seconds
@domain = verified_domain
process_subscribe
end
private
def process_subscribe
2018-12-12 13:54:34 +09:00
if account.nil?
['Invalid topic URL', 422]
2018-12-12 13:54:34 +09:00
elsif !valid_callback?
['Invalid callback URL', 422]
2018-12-12 13:54:34 +09:00
elsif blocked_domain?
['Callback URL not allowed', 403]
2018-12-12 13:54:34 +09:00
else
confirm_subscription
['', 202]
end
end
2016-11-28 21:36:47 +09:00
def confirm_subscription
subscription = locate_subscription
2016-11-28 21:36:47 +09:00
Pubsubhubbub::ConfirmationWorker.perform_async(subscription.id, 'subscribe', secret, lease_seconds)
end
def valid_callback?
callback.present? && callback =~ URL_PATTERN
end
def blocked_domain?
DomainBlock.blocked? Addressable::URI.parse(callback).host
end
2016-11-28 21:36:47 +09:00
def locate_subscription
2018-12-12 13:54:09 +09:00
subscription = Subscription.find_or_initialize_by(account: account, callback_url: callback)
subscription.domain = domain
subscription.save!
subscription
2016-11-28 21:36:47 +09:00
end
end