summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Gleason <alex@alexgleason.me>2021-12-28 21:00:09 +0000
committerAlex Gleason <alex@alexgleason.me>2021-12-28 21:00:09 +0000
commitb5b98f9e10ebe9742aa5b00b93b6491dc1dddee3 (patch)
tree19dafa7a31281d8df4342bf071f33165276188de
parentde7f84deb3ebe57744630950860f8fbf64a414a2 (diff)
parent1657db656cef7a6947e76d5213a04a1764a19cde (diff)
Merge branch 'mastodon-lookup' into 'develop'
MastoAPI: Add `GET /api/v1/accounts/lookup` See merge request pleroma/pleroma!3584
-rw-r--r--lib/pleroma/web/api_spec/operations/account_operation.ex20
-rw-r--r--lib/pleroma/web/mastodon_api/controllers/account_controller.ex14
-rw-r--r--lib/pleroma/web/router.ex2
-rw-r--r--test/pleroma/web/mastodon_api/controllers/account_controller_test.exs24
4 files changed, 59 insertions, 1 deletions
diff --git a/lib/pleroma/web/api_spec/operations/account_operation.ex b/lib/pleroma/web/api_spec/operations/account_operation.ex
index 8cd2e824d..f5304d7d6 100644
--- a/lib/pleroma/web/api_spec/operations/account_operation.ex
+++ b/lib/pleroma/web/api_spec/operations/account_operation.ex
@@ -400,6 +400,26 @@ defmodule Pleroma.Web.ApiSpec.AccountOperation do
}
end
+ def lookup_operation do
+ %Operation{
+ tags: ["Account lookup"],
+ summary: "Find a user by nickname",
+ operationId: "AccountController.lookup",
+ parameters: [
+ Operation.parameter(
+ :acct,
+ :query,
+ :string,
+ "User nickname"
+ )
+ ],
+ responses: %{
+ 200 => Operation.response("Account", "application/json", Account),
+ 404 => Operation.response("Error", "application/json", ApiError)
+ }
+ }
+ end
+
def endorsements_operation do
%Operation{
tags: ["Retrieve account information"],
diff --git a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex
index 5dfbecf5a..a307807a9 100644
--- a/lib/pleroma/web/mastodon_api/controllers/account_controller.ex
+++ b/lib/pleroma/web/mastodon_api/controllers/account_controller.ex
@@ -32,7 +32,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
plug(Pleroma.Web.ApiSpec.CastAndValidate)
- plug(:skip_auth when action == :create)
+ plug(:skip_auth when action in [:create, :lookup])
plug(:skip_public_check when action in [:show, :statuses])
@@ -492,6 +492,18 @@ defmodule Pleroma.Web.MastodonAPI.AccountController do
|> render("index.json", users: users, for: user, as: :user)
end
+ @doc "GET /api/v1/accounts/lookup"
+ def lookup(conn, %{acct: nickname} = _params) do
+ with %User{} = user <- User.get_by_nickname(nickname) do
+ render(conn, "show.json",
+ user: user,
+ skip_visibility_check: true
+ )
+ else
+ error -> user_visibility_error(conn, error)
+ end
+ end
+
@doc "GET /api/v1/endorsements"
def endorsements(conn, params), do: MastodonAPIController.empty_array(conn, params)
diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex
index e439a279d..b9b52b1e5 100644
--- a/lib/pleroma/web/router.ex
+++ b/lib/pleroma/web/router.ex
@@ -585,6 +585,8 @@ defmodule Pleroma.Web.Router do
get("/accounts/search", SearchController, :account_search)
get("/search", SearchController, :search)
+ get("/accounts/lookup", AccountController, :lookup)
+
get("/accounts/:id/statuses", AccountController, :statuses)
get("/accounts/:id/followers", AccountController, :followers)
get("/accounts/:id/following", AccountController, :following)
diff --git a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs
index 966a4072d..374e2048a 100644
--- a/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs
+++ b/test/pleroma/web/mastodon_api/controllers/account_controller_test.exs
@@ -1798,6 +1798,30 @@ defmodule Pleroma.Web.MastodonAPI.AccountControllerTest do
assert [%{"id" => ^id2}] = result
end
+ test "account lookup", %{conn: conn} do
+ %{nickname: acct} = insert(:user, %{nickname: "nickname"})
+ %{nickname: acct_two} = insert(:user, %{nickname: "nickname@notlocaldoma.in"})
+
+ result =
+ conn
+ |> get("/api/v1/accounts/lookup?acct=#{acct}")
+ |> json_response_and_validate_schema(200)
+
+ assert %{"acct" => ^acct} = result
+
+ result =
+ conn
+ |> get("/api/v1/accounts/lookup?acct=#{acct_two}")
+ |> json_response_and_validate_schema(200)
+
+ assert %{"acct" => ^acct_two} = result
+
+ _result =
+ conn
+ |> get("/api/v1/accounts/lookup?acct=unexisting_nickname")
+ |> json_response_and_validate_schema(404)
+ end
+
test "create a note on a user" do
%{conn: conn} = oauth_access(["write:accounts", "read:follows"])
other_user = insert(:user)