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.

67 lines
1.5 KiB

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