Add get and add aliases endpoints
Ref: emit-move
This commit is contained in:
parent
60081a8818
commit
c1aa3c98ac
4 changed files with 173 additions and 1 deletions
|
|
@ -249,6 +249,69 @@ defmodule Pleroma.Web.ApiSpec.TwitterUtilOperation 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"],
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
|
|||
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)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue