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.

51 lines
1.0 KiB

  1. # frozen_string_literal: true
  2. class Api::SubscriptionsController < Api::BaseController
  3. before_action :set_account
  4. respond_to :txt
  5. def show
  6. if subscription.valid?(params['hub.topic'])
  7. @account.update(subscription_expires_at: future_expires)
  8. render plain: encoded_challenge, status: 200
  9. else
  10. head 404
  11. end
  12. end
  13. def update
  14. if subscription.verify(body, request.headers['HTTP_X_HUB_SIGNATURE'])
  15. ProcessingWorker.perform_async(@account.id, body.force_encoding('UTF-8'))
  16. end
  17. head 200
  18. end
  19. private
  20. def subscription
  21. @_subscription ||= @account.subscription(
  22. api_subscription_url(@account.id)
  23. )
  24. end
  25. def body
  26. @_body ||= request.body.read
  27. end
  28. def encoded_challenge
  29. HTMLEntities.new.encode(params['hub.challenge'])
  30. end
  31. def future_expires
  32. Time.now.utc + lease_seconds_or_default
  33. end
  34. def lease_seconds_or_default
  35. (params['hub.lease_seconds'] || 1.day).to_i.seconds
  36. end
  37. def set_account
  38. @account = Account.find(params[:id])
  39. end
  40. end