diff options
author | Roger Braun <roger@rogerbraun.net> | 2017-09-09 13:15:01 +0200 |
---|---|---|
committer | Roger Braun <roger@rogerbraun.net> | 2017-09-09 13:15:01 +0200 |
commit | be04f725e9398ebde446ef5664d4dbedd1eb262b (patch) | |
tree | db2e19c71e35442a8d18e3e7a63f3682eb22ee1e /lib | |
parent | 59dd240c0808bc895ca2b98030f5f8c2a27b9bba (diff) | |
download | pleroma-be04f725e9398ebde446ef5664d4dbedd1eb262b.tar.gz |
Add more Mastodon API methods.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 39 | ||||
-rw-r--r-- | lib/pleroma/web/router.ex | 10 |
2 files changed, 46 insertions, 3 deletions
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 62522439c..3a568cf2b 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -1,9 +1,11 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do use Pleroma.Web, :controller - alias Pleroma.{Repo} + alias Pleroma.{Repo, Activity} alias Pleroma.Web.OAuth.App alias Pleroma.Web - alias Pleroma.Web.MastodonAPI.AccountView + alias Pleroma.Web.MastodonAPI.{StatusView, AccountView} + alias Pleroma.Web.ActivityPub.ActivityPub + alias Pleroma.Web.TwitterAPI.TwitterAPI def create_app(conn, params) do with cs <- App.register_changeset(%App{}, params) |> IO.inspect, @@ -33,4 +35,37 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do json(conn, response) end + + def home_timeline(%{assigns: %{user: user}} = conn, params) do + activities = ActivityPub.fetch_activities([user.ap_id | user.following], Map.put(params, "type", "Create")) + render conn, StatusView, "index.json", %{activities: activities, for: user, as: :activity} + end + + def public_timeline(%{assigns: %{user: user}} = conn, params) do + params = params + |> Map.put("type", "Create") + |> Map.put("local_only", !!params["local"]) + + activities = ActivityPub.fetch_public_activities(params) + + render conn, StatusView, "index.json", %{activities: activities, for: user, as: :activity} + end + + def get_status(%{assigns: %{user: user}} = conn, %{"id" => id}) do + with %Activity{} = activity <- Repo.get(Activity, id) do + render conn, StatusView, "status.json", %{activity: activity, for: user} + end + end + + def post_status(%{assigns: %{user: user}} = conn, %{"status" => status} = params) do + l = status |> String.trim |> String.length + + params = params + |> Map.put("in_reply_to_status_id", params["in_reply_to_id"]) + + if l > 0 && l < 5000 do + {:ok, activity} = TwitterAPI.create_status(user, params) + render conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity} + end + end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index a8577c30b..46cbf4e4e 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -10,6 +10,7 @@ defmodule Pleroma.Web.Router do pipeline :api do plug :accepts, ["json"] plug :fetch_session + plug Pleroma.Plugs.OAuthPlug plug Pleroma.Plugs.AuthenticationPlug, %{fetcher: &Router.user_fetcher/1, optional: true} end @@ -40,14 +41,21 @@ defmodule Pleroma.Web.Router do scope "/api/v1", Pleroma.Web.MastodonAPI do pipe_through :api - get "/instance", MastodonAPO.Controller, :masto_instance + get "/instance", MastodonAPIController, :masto_instance post "/apps", MastodonAPIController, :create_app + + get "/timelines/public", MastodonAPIController, :public_timeline + + get "/statuses/:id", MastodonAPIController, :get_status end scope "/api/v1", Pleroma.Web.MastodonAPI do pipe_through :authenticated_api get "/accounts/verify_credentials", MastodonAPIController, :verify_credentials + get "/timelines/home", MastodonAPIController, :home_timeline + + post "/statuses", MastodonAPIController, :post_status end scope "/api", Pleroma.Web do |