diff options
author | lambda <pleromagit@rogerbraun.net> | 2017-10-28 14:25:34 +0000 |
---|---|---|
committer | lambda <pleromagit@rogerbraun.net> | 2017-10-28 14:25:34 +0000 |
commit | 353b66e4326c27aca4a81ee133b73454d5eb0853 (patch) | |
tree | a376acab415bb79a7c251cd337aa01825e3af30e | |
parent | ab2d4503e720ffd2d004edd5ce67176cd4256a81 (diff) | |
parent | 8e94936553c978e5f0d3bb802a1932a6b505f86e (diff) | |
download | pleroma-353b66e4326c27aca4a81ee133b73454d5eb0853.tar.gz pleroma-353b66e4326c27aca4a81ee133b73454d5eb0853.zip |
Merge branch 'missing-mastodon-follow-endpoint' into 'develop'
Add missing mastodon follow endpoint.
See merge request pleroma/pleroma!2
-rw-r--r-- | lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 34 | ||||
-rw-r--r-- | lib/pleroma/web/router.ex | 2 | ||||
-rw-r--r-- | test/web/mastodon_api/mastodon_api_controller_test.exs | 7 |
3 files changed, 39 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..5032c735d 100644 --- a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex +++ b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex @@ -269,11 +269,16 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController 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 + {:error, message} = err -> + conn + |> put_resp_content_type("application/json") + |> send_resp(403, Poison.encode!(%{"error" => message})) end end @@ -338,4 +343,25 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController 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 @@ defmodule Pleroma.Web.Router 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 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 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest 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 |