commit: 4aa285e6ceb79ca8995ea9ff3585ea79e787650c
parent: f9a9639015b1ca50096bd7f7cedea3c77bd6ffe2
Author: kaniini <nenolod@gmail.com>
Date: Mon, 11 Mar 2019 21:43:46 +0000
Merge branch 'add-screenname-extension-to-masto-api' into 'develop'
MastoAPI Accounts: Add fetching by nickname.
See merge request pleroma/pleroma!922
Diffstat:
3 files changed, 18 insertions(+), 3 deletions(-)
diff --git a/docs/Differences-in-MastodonAPI-Responses.md b/docs/Differences-in-MastodonAPI-Responses.md
@@ -19,3 +19,7 @@ Adding the parameter `with_muted=true` to the timeline queries will also return
Has these additional fields under the `pleroma` object:
- `local`: true if the post was made on the local instance.
+
+## Accounts
+
+- `/api/v1/accounts/:id`: The `id` parameter can also be the `nickname` of the user. This only works in this endpoint, not the deeper nested ones for following etc.
diff --git a/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex b/lib/pleroma/web/mastodon_api/mastodon_api_controller.ex
@@ -131,8 +131,8 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
json(conn, account)
end
- def user(%{assigns: %{user: for_user}} = conn, %{"id" => id}) do
- with %User{} = user <- Repo.get(User, id),
+ def user(%{assigns: %{user: for_user}} = conn, %{"id" => nickname_or_id}) do
+ with %User{} = user <- User.get_cached_by_nickname_or_id(nickname_or_id),
true <- User.auth_active?(user) || user.id == for_user.id || User.superuser?(for_user) do
account = AccountView.render("account.json", %{user: user, for: for_user})
json(conn, account)
diff --git a/test/web/mastodon_api/mastodon_api_controller_test.exs b/test/web/mastodon_api/mastodon_api_controller_test.exs
@@ -1064,6 +1064,17 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
assert %{"error" => "Can't find user"} = json_response(conn, 404)
end
+ test "account fetching also works nickname", %{conn: conn} do
+ user = insert(:user)
+
+ conn =
+ conn
+ |> get("/api/v1/accounts/#{user.nickname}")
+
+ assert %{"id" => id} = json_response(conn, 200)
+ assert id == user.id
+ end
+
test "media upload", %{conn: conn} do
file = %Plug.Upload{
content_type: "image/jpg",
@@ -1950,7 +1961,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIControllerTest do
|> json_response(200)
end
- test "accound_id is required", %{
+ test "account_id is required", %{
conn: conn,
reporter: reporter,
activity: activity