From 813d2eaaf08efcb7aa87f6270e98c760b0b60b99 Mon Sep 17 00:00:00 2001 From: eal Date: Wed, 25 Oct 2017 21:02:42 +0300 Subject: [PATCH 1/3] Add mastodon API endpoint for follow --- .../mastodon_api/mastodon_api_controller.ex | 31 ++++++++++++++++--- lib/pleroma/web/router.ex | 2 ++ 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index a01a199fb..ea85e5bce 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -269,11 +269,13 @@ def following(conn, %{"id" => id}) do end end - def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do - with %User{} = followed <- Repo.get(User, id), - {:ok, follower} <- User.follow(follower, followed), - {:ok, activity} <- ActivityPub.follow(follower, followed) do + def follow(%{assigns: %{user: follower}} = conn, params) do + with {:ok, %User{} = followed} <- get_user(params), + {:ok, follower} <- User.follow(follower, followed), + {:ok, activity} <- ActivityPub.follow(follower, followed) do render conn, AccountView, "relationship.json", %{user: follower, target: followed} + else + err -> err end end @@ -338,4 +340,25 @@ def empty_array(conn, _) do Logger.debug("Unimplemented, returning an empty array") json(conn, []) end + + defp get_user(params) do + case params do + %{"uri" => uri} -> + case target = Repo.get_by(User, nickname: uri) do + nil -> + {:error, "No user with such nickname"} + _ -> + {:ok, target} + end + %{"id" => id} -> + case target = Repo.get(User, id) do + nil -> + {:error, "No user with such id"} + _ -> + {:ok, target} + end + _ -> + {:error, "You need to specify uri or id"} + end + end end diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index cb818b3cc..45c47eefb 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -62,6 +62,8 @@ def user_fetcher(username) do post "/accounts/:id/mute", MastodonAPIController, :relationship_noop post "/accounts/:id/unmute", MastodonAPIController, :relationship_noop + post "/follows", MastodonAPIController, :follow + get "/blocks", MastodonAPIController, :empty_array get "/domain_blocks", MastodonAPIController, :empty_array get "/follow_requests", MastodonAPIController, :empty_array From 11d2287476fe9518cd0a3721a0fe7a0a1fd937fd Mon Sep 17 00:00:00 2001 From: eal Date: Wed, 25 Oct 2017 21:25:37 +0300 Subject: [PATCH 2/3] Return error message on errors --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 5 ++++- 1 file changed, 4 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 ea85e5bce..5032c735d 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -275,7 +275,10 @@ def follow(%{assigns: %{user: follower}} = conn, params) do {:ok, activity} <- ActivityPub.follow(follower, followed) do render conn, AccountView, "relationship.json", %{user: follower, target: followed} else - err -> err + {:error, message} = err -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Poison.encode!(%{"error" => message})) end end From 8e94936553c978e5f0d3bb802a1932a6b505f86e Mon Sep 17 00:00:00 2001 From: eal Date: Fri, 27 Oct 2017 13:55:36 +0300 Subject: [PATCH 3/3] Add tests. --- test/web/mastodon_api/mastodon_api_controller_test.exs | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 56888140d..af2351706 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -281,6 +281,13 @@ test "following / unfollowing a user", %{conn: conn} do |> post("/api/v1/accounts/#{other_user.id}/unfollow") assert %{"id" => id, "following" => false} = json_response(conn, 200) + + user = Repo.get(User, user.id) + conn = build_conn() + |> assign(:user, user) + |> post("/api/v1/follows", %{"uri" => other_user.nickname}) + + assert %{"id" => id, "following" => true} = json_response(conn, 200) end test "unimplemented block/mute endpoints" do