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}
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),
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
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