about summary refs log tree commit diff
path: root/app/api
diff options
context:
space:
mode:
authorEugen Rochko <eugen@zeonfederated.com>2016-02-20 22:53:20 +0100
committerEugen Rochko <eugen@zeonfederated.com>2016-02-20 22:53:20 +0100
commit9c4856bdb11fc9311ab30a97224cee3dfaec492f (patch)
tree37fd831e505f040bbd3c583f56d3502ebd75e9c8 /app/api
Initial commit
Diffstat (limited to 'app/api')
-rw-r--r--app/api/mastodon/api.rb8
-rw-r--r--app/api/mastodon/entities.rb21
-rw-r--r--app/api/mastodon/ostatus.rb63
-rw-r--r--app/api/mastodon/rest.rb13
4 files changed, 105 insertions, 0 deletions
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