From c56d28f96c343d445ec8d06baf351423691036f3 Mon Sep 17 00:00:00 2001 From: eal Date: Sun, 29 Oct 2017 00:07:38 +0300 Subject: [PATCH 1/3] Fix return type of /api/v1/follows --- .../mastodon_api/mastodon_api_controller.ex | 38 ++++++++----------- lib/pleroma/web/router.ex | 2 +- .../mastodon_api_controller_test.exs | 2 +- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex index 5032c735d..25c5418ab 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -269,8 +269,8 @@ def following(conn, %{"id" => id}) do end end - def follow(%{assigns: %{user: follower}} = conn, params) do - with {:ok, %User{} = followed} <- get_user(params), + 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 render conn, AccountView, "relationship.json", %{user: follower, target: followed} @@ -282,6 +282,19 @@ def follow(%{assigns: %{user: follower}} = conn, params) do end end + def follows(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do + with %User{} = followed <- Repo.get_by(User, nickname: uri), + {:ok, follower} <- User.follow(follower, followed), + {:ok, activity} <- ActivityPub.follow(follower, followed) do + render conn, AccountView, "account.json", %{user: followed} + else + {:error, message} = err -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Poison.encode!(%{"error" => message})) + end + end + # TODO: Clean up and unify def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do with %User{} = followed <- Repo.get(User, id), @@ -343,25 +356,4 @@ 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 45c47eefb..557d094b4 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -62,7 +62,7 @@ def user_fetcher(username) do post "/accounts/:id/mute", MastodonAPIController, :relationship_noop post "/accounts/:id/unmute", MastodonAPIController, :relationship_noop - post "/follows", MastodonAPIController, :follow + post "/follows", MastodonAPIController, :follows get "/blocks", MastodonAPIController, :empty_array get "/domain_blocks", MastodonAPIController, :empty_array diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index af2351706..4e5dc963f 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -287,7 +287,7 @@ test "following / unfollowing a user", %{conn: conn} do |> assign(:user, user) |> post("/api/v1/follows", %{"uri" => other_user.nickname}) - assert %{"id" => id, "following" => true} = json_response(conn, 200) + assert other_user = json_response(conn, 200) end test "unimplemented block/mute endpoints" do From 2ffc6da20799ac8e7882f5e72d3f37dadc1f2428 Mon Sep 17 00:00:00 2001 From: eal Date: Sun, 29 Oct 2017 00:30:10 +0300 Subject: [PATCH 2/3] Clean style. Use 'follow' instead of 'follows' and correct indentation. --- lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 6 +++--- lib/pleroma/web/router.ex | 2 +- 2 files changed, 4 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 25c5418ab..971772810 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -282,10 +282,10 @@ def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do end end - def follows(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do + def follow(%{assigns: %{user: follower}} = conn, %{"uri" => uri}) do with %User{} = followed <- Repo.get_by(User, nickname: uri), - {:ok, follower} <- User.follow(follower, followed), - {:ok, activity} <- ActivityPub.follow(follower, followed) do + {:ok, follower} <- User.follow(follower, followed), + {:ok, activity} <- ActivityPub.follow(follower, followed) do render conn, AccountView, "account.json", %{user: followed} else {:error, message} = err -> diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index 557d094b4..45c47eefb 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -62,7 +62,7 @@ def user_fetcher(username) do post "/accounts/:id/mute", MastodonAPIController, :relationship_noop post "/accounts/:id/unmute", MastodonAPIController, :relationship_noop - post "/follows", MastodonAPIController, :follows + post "/follows", MastodonAPIController, :follow get "/blocks", MastodonAPIController, :empty_array get "/domain_blocks", MastodonAPIController, :empty_array From 71f66bd4589faddc35137ed3197f685555b77aaf Mon Sep 17 00:00:00 2001 From: eal Date: Sun, 29 Oct 2017 14:25:11 +0200 Subject: [PATCH 3/3] Fix follow test semantics. --- test/web/mastodon_api/mastodon_api_controller_test.exs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs index 4e5dc963f..acdb08ac2 100644 --- a/test/web/mastodon_api/mastodon_api_controller_test.exs +++ b/test/web/mastodon_api/mastodon_api_controller_test.exs @@ -287,7 +287,8 @@ test "following / unfollowing a user", %{conn: conn} do |> assign(:user, user) |> post("/api/v1/follows", %{"uri" => other_user.nickname}) - assert other_user = json_response(conn, 200) + assert %{"id" => id} = json_response(conn, 200) + assert id == other_user.id end test "unimplemented block/mute endpoints" do