ostatus.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. module Mastodon
  2. class Ostatus < Grape::API
  3. format :txt
  4. before do
  5. @account = Account.find(params[:id])
  6. end
  7. resource :subscriptions do
  8. helpers do
  9. def subscription_url(account)
  10. "https://649841dc.ngrok.io/api#{subscriptions_path(id: account.id)}"
  11. end
  12. end
  13. desc 'Receive updates from a feed'
  14. params do
  15. requires :id, type: String, desc: 'Account ID'
  16. end
  17. post ':id' do
  18. body = request.body.read
  19. if @account.subscription(subscription_url(@account)).verify(body, env['HTTP_X_HUB_SIGNATURE'])
  20. ProcessFeedUpdateService.new.(body, @account)
  21. status 201
  22. else
  23. status 202
  24. end
  25. end
  26. desc 'Confirm PuSH subscription to a feed'
  27. params do
  28. requires :id, type: String, desc: 'Account ID'
  29. requires 'hub.topic', type: String, desc: 'Topic URL'
  30. requires 'hub.verify_token', type: String, desc: 'Verification token'
  31. requires 'hub.challenge', type: String, desc: 'Hub challenge'
  32. end
  33. get ':id' do
  34. if @account.subscription(subscription_url(@account)).valid?(params['hub.topic'], params['hub.verify_token'])
  35. params['hub.challenge']
  36. else
  37. error! :not_found, 404
  38. end
  39. end
  40. end
  41. resource :salmon do
  42. desc 'Receive Salmon updates'
  43. params do
  44. requires :id, type: String, desc: 'Account ID'
  45. end
  46. post ':id' do
  47. # todo
  48. end
  49. end
  50. end
  51. end