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.

33 lines
877 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. head 201
  19. else
  20. head 202
  21. end
  22. end
  23. private
  24. def set_account
  25. @account = Account.find(params[:id])
  26. end
  27. end