From 9c4856bdb11fc9311ab30a97224cee3dfaec492f Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 20 Feb 2016 22:53:20 +0100 Subject: Initial commit --- app/api/mastodon/api.rb | 8 ++++++ app/api/mastodon/entities.rb | 21 +++++++++++++++ app/api/mastodon/ostatus.rb | 63 ++++++++++++++++++++++++++++++++++++++++++++ app/api/mastodon/rest.rb | 13 +++++++++ 4 files changed, 105 insertions(+) create mode 100644 app/api/mastodon/api.rb create mode 100644 app/api/mastodon/entities.rb create mode 100644 app/api/mastodon/ostatus.rb create mode 100644 app/api/mastodon/rest.rb (limited to 'app/api') diff --git a/app/api/mastodon/api.rb b/app/api/mastodon/api.rb new file mode 100644 index 000000000..39469dc65 --- /dev/null +++ b/app/api/mastodon/api.rb @@ -0,0 +1,8 @@ +module Mastodon + class API < Grape::API + rescue_from :all + + mount Mastodon::Ostatus + mount Mastodon::Rest + end +end diff --git a/app/api/mastodon/entities.rb b/app/api/mastodon/entities.rb new file mode 100644 index 000000000..a3f40ec48 --- /dev/null +++ b/app/api/mastodon/entities.rb @@ -0,0 +1,21 @@ +module Mastodon + module Entities + class Account < Grape::Entity + expose :username + expose :domain + end + + class Status < Grape::Entity + format_with(:iso_timestamp) { |dt| dt.iso8601 } + + expose :uri + expose :text + expose :account, using: Mastodon::Entities::Account + + with_options(format_with: :iso_timestamp) do + expose :created_at + expose :updated_at + end + end + end +end diff --git a/app/api/mastodon/ostatus.rb b/app/api/mastodon/ostatus.rb new file mode 100644 index 000000000..fcde980f7 --- /dev/null +++ b/app/api/mastodon/ostatus.rb @@ -0,0 +1,63 @@ +module Mastodon + class Ostatus < Grape::API + format :txt + + before do + @account = Account.find(params[:id]) + end + + resource :subscriptions do + helpers do + def subscription_url(account) + "https://649841dc.ngrok.io/api#{subscriptions_path(id: account.id)}" + end + end + + desc 'Receive updates from a feed' + + params do + requires :id, type: String, desc: 'Account ID' + end + + post ':id' do + body = request.body.read + + if @account.subscription(subscription_url(@account)).verify(body, env['HTTP_X_HUB_SIGNATURE']) + ProcessFeedUpdateService.new.(body, @account) + status 201 + else + status 202 + end + end + + desc 'Confirm PuSH subscription to a feed' + + params do + requires :id, type: String, desc: 'Account ID' + requires 'hub.topic', type: String, desc: 'Topic URL' + requires 'hub.verify_token', type: String, desc: 'Verification token' + requires 'hub.challenge', type: String, desc: 'Hub challenge' + end + + get ':id' do + if @account.subscription(subscription_url(@account)).valid?(params['hub.topic'], params['hub.verify_token']) + params['hub.challenge'] + else + error! :not_found, 404 + end + end + end + + resource :salmon do + desc 'Receive Salmon updates' + + params do + requires :id, type: String, desc: 'Account ID' + end + + post ':id' do + # todo + end + end + end +end diff --git a/app/api/mastodon/rest.rb b/app/api/mastodon/rest.rb new file mode 100644 index 000000000..e011ab34d --- /dev/null +++ b/app/api/mastodon/rest.rb @@ -0,0 +1,13 @@ +module Mastodon + class Rest < Grape::API + version 'v1', using: :path + format :json + + resource :statuses do + desc 'Return a public timeline' + get :all do + present Status.all, with: Mastodon::Entities::Status + end + end + end +end -- cgit