push_controller.rb 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # frozen_string_literal: true
  2. class Api::PushController < Api::BaseController
  3. include SignatureVerification
  4. def update
  5. response, status = process_push_request
  6. render plain: response, status: status
  7. end
  8. private
  9. def process_push_request
  10. case hub_mode
  11. when 'subscribe'
  12. Pubsubhubbub::SubscribeService.new.call(account_from_topic, hub_callback, hub_secret, hub_lease_seconds, verified_domain)
  13. when 'unsubscribe'
  14. Pubsubhubbub::UnsubscribeService.new.call(account_from_topic, hub_callback)
  15. else
  16. ["Unknown mode: #{hub_mode}", 422]
  17. end
  18. end
  19. def hub_mode
  20. params['hub.mode']
  21. end
  22. def hub_topic
  23. params['hub.topic']
  24. end
  25. def hub_callback
  26. params['hub.callback']
  27. end
  28. def hub_lease_seconds
  29. params['hub.lease_seconds']
  30. end
  31. def hub_secret
  32. params['hub.secret']
  33. end
  34. def account_from_topic
  35. if hub_topic.present? && local_domain? && account_feed_path?
  36. Account.find_local(hub_topic_params[:username])
  37. end
  38. end
  39. def hub_topic_params
  40. @_hub_topic_params ||= Rails.application.routes.recognize_path(hub_topic_uri.path)
  41. end
  42. def hub_topic_uri
  43. @_hub_topic_uri ||= Addressable::URI.parse(hub_topic).normalize
  44. end
  45. def local_domain?
  46. TagManager.instance.web_domain?(hub_topic_domain)
  47. end
  48. def verified_domain
  49. return signed_request_account.domain if signed_request_account
  50. end
  51. def hub_topic_domain
  52. hub_topic_uri.host + (hub_topic_uri.port ? ":#{hub_topic_uri.port}" : '')
  53. end
  54. def account_feed_path?
  55. hub_topic_params[:controller] == 'accounts' && hub_topic_params[:action] == 'show' && hub_topic_params[:format] == 'atom'
  56. end
  57. end