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.

32 lines
852 B

  1. # frozen_string_literal: true
  2. class Api::SubscriptionsController < ApiController
  3. before_action :set_account
  4. respond_to :txt
  5. def show
  6. if @account.subscription(api_subscription_url(@account.id)).valid?(params['hub.topic'])
  7. @account.update(subscription_expires_at: Time.now.utc + (params['hub.lease_seconds'] || 86_400).to_i.seconds)
  8. render plain: HTMLEntities.new.encode(params['hub.challenge']), status: 200
  9. else
  10. head 404
  11. end
  12. end
  13. def update
  14. body = request.body.read
  15. subscription = @account.subscription(api_subscription_url(@account.id))
  16. if subscription.verify(body, request.headers['HTTP_X_HUB_SIGNATURE'])
  17. ProcessingWorker.perform_async(@account.id, body.force_encoding('UTF-8'))
  18. end
  19. head 200
  20. end
  21. private
  22. def set_account
  23. @account = Account.find(params[:id])
  24. end
  25. end