activitypub-academy/app/controllers/api/push_controller.rb

74 lines
1.6 KiB
Ruby
Raw Normal View History

2016-11-28 21:36:47 +09:00
# frozen_string_literal: true
class Api::PushController < Api::BaseController
include SignatureVerification
2016-11-28 21:36:47 +09:00
def update
response, status = process_push_request
render plain: response, status: status
end
private
2016-11-28 21:36:47 +09:00
def process_push_request
case hub_mode
2016-11-28 21:36:47 +09:00
when 'subscribe'
Pubsubhubbub::SubscribeService.new.call(account_from_topic, hub_callback, hub_secret, hub_lease_seconds, verified_domain)
2016-11-28 21:36:47 +09:00
when 'unsubscribe'
Pubsubhubbub::UnsubscribeService.new.call(account_from_topic, hub_callback)
2016-11-28 21:36:47 +09:00
else
["Unknown mode: #{hub_mode}", 422]
2016-11-28 21:36:47 +09:00
end
end
2016-11-28 21:36:47 +09:00
def hub_mode
params['hub.mode']
2016-11-28 21:36:47 +09:00
end
def hub_topic
params['hub.topic']
end
def hub_callback
params['hub.callback']
end
def hub_lease_seconds
params['hub.lease_seconds']
end
def hub_secret
params['hub.secret']
end
2016-11-28 21:36:47 +09:00
def account_from_topic
if hub_topic.present? && local_domain? && account_feed_path?
Account.find_local(hub_topic_params[:username])
end
end
2016-11-28 21:36:47 +09:00
def hub_topic_params
@_hub_topic_params ||= Rails.application.routes.recognize_path(hub_topic_uri.path)
end
2016-11-28 21:36:47 +09:00
def hub_topic_uri
@_hub_topic_uri ||= Addressable::URI.parse(hub_topic).normalize
end
def local_domain?
TagManager.instance.web_domain?(hub_topic_domain)
end
def verified_domain
return signed_request_account.domain if signed_request_account
end
def hub_topic_domain
hub_topic_uri.host + (hub_topic_uri.port ? ":#{hub_topic_uri.port}" : '')
end
2016-11-28 21:36:47 +09:00
def account_feed_path?
hub_topic_params[:controller] == 'accounts' && hub_topic_params[:action] == 'show' && hub_topic_params[:format] == 'atom'
2016-11-28 21:36:47 +09:00
end
end