Merge branch 'missing-mastodon-follow-endpoint' into 'develop'

Fix return type of /api/v1/follows

See merge request pleroma/pleroma!3
This commit is contained in:
lambda 2017-10-29 18:16:09 +00:00
commit 9168497f8f
2 changed files with 17 additions and 24 deletions

View File

@ -269,8 +269,8 @@ def following(conn, %{"id" => id}) do
end end
end end
def follow(%{assigns: %{user: follower}} = conn, params) do def follow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
with {:ok, %User{} = followed} <- get_user(params), with %User{} = followed <- Repo.get(User, id),
{:ok, follower} <- User.follow(follower, followed), {:ok, follower} <- User.follow(follower, followed),
{:ok, activity} <- ActivityPub.follow(follower, followed) do {:ok, activity} <- ActivityPub.follow(follower, followed) do
render conn, AccountView, "relationship.json", %{user: follower, target: followed} render conn, AccountView, "relationship.json", %{user: follower, target: followed}
@ -282,6 +282,19 @@ def follow(%{assigns: %{user: follower}} = conn, params) do
end end
end end
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
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 # TODO: Clean up and unify
def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do def unfollow(%{assigns: %{user: follower}} = conn, %{"id" => id}) do
with %User{} = followed <- Repo.get(User, id), with %User{} = followed <- Repo.get(User, id),
@ -343,25 +356,4 @@ def empty_array(conn, _) do
Logger.debug("Unimplemented, returning an empty array") Logger.debug("Unimplemented, returning an empty array")
json(conn, []) json(conn, [])
end 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 end

View File

@ -287,7 +287,8 @@ test "following / unfollowing a user", %{conn: conn} do
|> assign(:user, user) |> assign(:user, user)
|> post("/api/v1/follows", %{"uri" => other_user.nickname}) |> post("/api/v1/follows", %{"uri" => other_user.nickname})
assert %{"id" => id, "following" => true} = json_response(conn, 200) assert %{"id" => id} = json_response(conn, 200)
assert id == other_user.id
end end
test "unimplemented block/mute endpoints" do test "unimplemented block/mute endpoints" do