Add get and add aliases endpoints

Ref: emit-move
This commit is contained in:
Tusooa Zhu 2021-09-12 11:46:37 -04:00
parent 60081a8818
commit c1aa3c98ac
No known key found for this signature in database
GPG Key ID: 7B467EDE43A08224
4 changed files with 173 additions and 1 deletions

View File

@ -249,6 +249,69 @@ defp move_account_request do
}
end
def list_aliases_operation do
%Operation{
tags: ["Account credentials"],
summary: "List account aliases",
security: [%{"oAuth" => ["read:accounts"]}],
operationId: "UtilController.list_aliases",
responses: %{
200 =>
Operation.response("Success", "application/json", %Schema{
type: :object,
properties: %{
aliases: %Schema{
type: :array,
items: %Schema{type: :string},
example: ["foo@example.org"]
}
}
}),
400 => Operation.response("Error", "application/json", ApiError),
403 => Operation.response("Error", "application/json", ApiError)
}
}
end
def add_alias_operation do
%Operation{
tags: ["Account credentials"],
summary: "Add an alias to this account",
security: [%{"oAuth" => ["write:accounts"]}],
operationId: "UtilController.add_alias",
requestBody: request_body("Parameters", add_alias_request(), required: true),
responses: %{
200 =>
Operation.response("Success", "application/json", %Schema{
type: :object,
properties: %{
status: %Schema{
type: :string,
example: "success"
}
}
}),
400 => Operation.response("Error", "application/json", ApiError),
403 => Operation.response("Error", "application/json", ApiError)
}
}
end
defp add_alias_request do
%Schema{
title: "AddAliasRequest",
description: "PUT body for adding aliases",
type: :object,
required: [:alias],
properties: %{
alias: %Schema{
type: :string,
description: "The nickname of the account to add to aliases"
}
}
}
end
def healthcheck_operation do
%Operation{
tags: ["Accounts"],

View File

@ -344,6 +344,9 @@ defmodule Pleroma.Web.Router do
put("/notification_settings", UtilController, :update_notificaton_settings)
post("/disable_account", UtilController, :disable_account)
post("/move_account", UtilController, :move_account)
put("/aliases", UtilController, :add_alias)
get("/aliases", UtilController, :list_aliases)
end
scope "/api/pleroma", Pleroma.Web.PleromaAPI do

View File

@ -28,7 +28,16 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
:delete_account,
:update_notificaton_settings,
:disable_account,
:move_account
:move_account,
:add_alias
]
)
plug(
OAuthScopesPlug,
%{scopes: ["read:accounts"]}
when action in [
:list_aliases
]
)
@ -179,6 +188,24 @@ def move_account(%{assigns: %{user: user}, body_params: body_params} = conn, %{}
end
end
def add_alias(%{assigns: %{user: user}, body_params: body_params} = conn, _) do
with {:ok, alias_user} <- find_user_by_nickname(body_params.alias),
{:ok, _user} <- user |> User.add_alias(alias_user) do
json(conn, %{status: "success"})
else
{:error, error} ->
json(conn, %{error: error})
end
end
def list_aliases(%{assigns: %{user: user}} = conn, %{}) do
alias_nicks = user
|> User.alias_users()
|> Enum.map(&User.full_nickname/1)
json(conn, %{aliases: alias_nicks})
end
defp find_user_by_nickname(nickname) do
user = User.get_cached_by_nickname(nickname)

View File

@ -651,4 +651,83 @@ test "prefix nickname by @ should work", %{
assert User.following?(follower, target_user)
end
end
describe "GET /api/pleroma/aliases" do
setup do: oauth_access(["read:accounts"])
test "without permissions", %{conn: conn} do
conn =
conn
|> assign(:token, nil)
|> get("/api/pleroma/aliases")
assert json_response_and_validate_schema(conn, 403) == %{
"error" => "Insufficient permissions: read:accounts."
}
end
test "with permissions", %{
conn: conn
} do
assert %{"aliases" => []} = conn
|> get("/api/pleroma/aliases")
|> json_response_and_validate_schema(200)
end
test "with permissions and aliases", %{} do
user = insert(:user)
user2 = insert(:user)
assert {:ok, user} = user |> User.add_alias(user2)
%{user: _user, conn: conn} = oauth_access(["read:accounts"], user: user)
assert %{"aliases" => aliases} = conn
|> get("/api/pleroma/aliases")
|> json_response_and_validate_schema(200)
assert aliases == [user2 |> User.full_nickname()]
end
end
describe "PUT /api/pleroma/aliases" do
setup do: oauth_access(["write:accounts"])
test "without permissions", %{conn: conn} do
conn =
conn
|> assign(:token, nil)
|> put_req_header("content-type", "application/json")
|> put("/api/pleroma/aliases", %{alias: "none"})
assert json_response_and_validate_schema(conn, 403) == %{
"error" => "Insufficient permissions: write:accounts."
}
end
test "with permissions, no alias param", %{
conn: conn
} do
conn =
conn
|> put_req_header("content-type", "application/json")
|> put("/api/pleroma/aliases", %{})
assert %{"error" => "Missing field: alias."} = json_response_and_validate_schema(conn, 400)
end
test "with permissions, with alias param", %{
conn: conn
} do
user2 = insert(:user)
conn =
conn
|> put_req_header("content-type", "application/json")
|> put("/api/pleroma/aliases", %{alias: user2 |> User.full_nickname()})
assert json_response_and_validate_schema(conn, 200) == %{
"status" => "success"
}
end
end
end