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.

37 lines
1.1 KiB

  1. # frozen_string_literal: true
  2. class Api::PushController < ApiController
  3. def update
  4. mode = params['hub.mode']
  5. topic = params['hub.topic']
  6. callback = params['hub.callback']
  7. lease_seconds = params['hub.lease_seconds']
  8. secret = params['hub.secret']
  9. case mode
  10. when 'subscribe'
  11. response, status = Pubsubhubbub::SubscribeService.new.call(topic_to_account(topic), callback, secret, lease_seconds)
  12. when 'unsubscribe'
  13. response, status = Pubsubhubbub::UnsubscribeService.new.call(topic_to_account(topic), callback)
  14. else
  15. response = "Unknown mode: #{mode}"
  16. status = 422
  17. end
  18. render plain: response, status: status
  19. end
  20. private
  21. def topic_to_account(topic_url)
  22. return if topic_url.blank?
  23. uri = Addressable::URI.parse(topic_url)
  24. params = Rails.application.routes.recognize_path(uri.path)
  25. domain = uri.host + (uri.port ? ":#{uri.port}" : '')
  26. return unless TagManager.instance.local_domain?(domain) && params[:controller] == 'accounts' && params[:action] == 'show' && params[:format] == 'atom'
  27. Account.find_local(params[:username])
  28. end
  29. end