From 7616b202ea6ab9cd2db107eea59aba1393f4f996 Mon Sep 17 00:00:00 2001 From: Roger Braun Date: Sun, 10 Sep 2017 17:46:43 +0200 Subject: [PATCH] Add user timelines to Masto Api. --- .../web/mastodon_api/mastodon_api_controller.ex | 15 ++++++++++++++- lib/pleroma/web/router.ex | 2 ++ .../mastodon_api_controller_test.exs | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 1aa7f43ab..16ee434c6 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -1,6 +1,6 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do use Pleroma.Web, :controller - alias Pleroma.{Repo, Activity} + alias Pleroma.{Repo, Activity, User} alias Pleroma.Web.OAuth.App alias Pleroma.Web alias Pleroma.Web.MastodonAPI.{StatusView, AccountView} @@ -55,6 +55,19 @@ def public_timeline(%{assigns: %{user: user}} = conn, params) do render conn, StatusView, "index.json", %{activities: activities, for: user, as: :activity} end + def user_statuses(%{assigns: %{user: user}} = conn, params) do + with %User{ap_id: ap_id} <- Repo.get(User, params["id"]) do + params = params + |> Map.put("type", "Create") + |> Map.put("actor_id", ap_id) + + activities = ActivityPub.fetch_activities([], params) + |> Enum.reverse + + render conn, StatusView, "index.json", %{activities: activities, for: user, as: :activity} + end + 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} diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 5246b3c41..9e725641d 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -48,6 +48,8 @@ def user_fetcher(username) do get "/statuses/:id", MastodonAPIController, :get_status get "/statuses/:id/context", MastodonAPIController, :get_context + + get "/accounts/:id/statuses", MastodonAPIController, :user_statuses end scope "/api/v1", Pleroma.Web.MastodonAPI do diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index dc925e2c8..e87430d3f 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -165,4 +165,20 @@ test "unfavorites a status and returns it", %{conn: conn} do assert activity.id == id end end + + describe "user timelines" do + test "gets a users statuses", %{conn: conn} do + _note = insert(:note_activity) + note_two = insert(:note_activity) + + user = User.get_by_ap_id(note_two.data["actor"]) + + conn = conn + |> get("/api/v1/accounts/#{user.id}/statuses") + + assert [%{"id" => id}] = json_response(conn, 200) + + assert id == note_two.id + end + end end