diff options
author | eal <eal@waifu.club> | 2017-10-25 21:02:42 +0300 |
---|---|---|
committer | eal <eal@waifu.club> | 2017-10-28 17:09:32 +0300 |
commit | 813d2eaaf08efcb7aa87f6270e98c760b0b60b99 (patch) | |
tree | 06257a37fb92d98ebd3cffb2c68008ce545d9b43 /lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | |
parent | 529351673090d8165bb0c54f9e957e611fcacd72 (diff) | |
download | pleroma-813d2eaaf08efcb7aa87f6270e98c760b0b60b99.tar.gz |
Add mastodon API endpoint for follow
Diffstat (limited to 'lib/pleroma/web/mastodon_api/mastodon_api_controller.ex')
-rw-r--r-- | lib/pleroma/web/mastodon_api/mastodon_api_controller.ex | 31 |
1 files changed, 27 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 @@ 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 + err -> err end end @@ -338,4 +340,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 |