MastoAPI: Add `GET /api/v1/accounts/lookup`
authormarcin mikołajczak <git@mkljczk.pl>
Tue, 28 Dec 2021 15:11:17 +0000 (16:11 +0100)
committermarcin mikołajczak <git@mkljczk.pl>
Tue, 28 Dec 2021 15:11:17 +0000 (16:11 +0100)
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
lib/pleroma/web/api_spec/operations/account_operation.ex
lib/pleroma/web/mastodon_api/controllers/account_controller.ex
lib/pleroma/web/router.ex
test/pleroma/web/mastodon_api/controllers/account_controller_test.exs

index 54e5ebc76e2ae943e02b3026d85799f7cf511641..5836cab50dcaca2b44759357699a7316094ff8ce 100644 (file)
@@ -371,6 +371,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"],
index 5fcbffc34d682d79cb801de8cea1c2e7abb61cf6..3eae0a64618728d261ed04ce3fc707d14deb9830 100644 (file)
@@ -477,6 +477,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(%{assigns: %{user: for_user}} = conn, %{acct: nickname} = _params) do
+    with %User{} = user <- User.get_by_nickname(nickname) do
+      render(conn, "show.json",
+        user: user,
+        for: for_user
+      )
+    else
+      error -> user_visibility_error(conn, error)
+    end
+  end
+
   @doc "GET /api/v1/endorsements"
   def endorsements(conn, params), do: MastodonAPIController.empty_array(conn, params)
 
index 5fbc2509e1034c7055b2a6dd5d1a48389e429f5f..ae373e58c7d4502b9ce7f72564a4f1daaf9dd14c 100644 (file)
@@ -573,6 +573,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)
index a92a582247ddf252eb83e6c61c9722107f6c870d..86349619e0fda677001388aeae8c0c33045ea0fe 100644 (file)
@@ -1776,4 +1776,28 @@ 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
 end