Implement moving account

Ref: emit-move
This commit is contained in:
Tusooa Zhu 2021-09-11 22:11:18 -04:00
commit 0af77b20c1
No known key found for this signature in database
GPG key ID: 7B467EDE43A08224
4 changed files with 203 additions and 1 deletions

View file

@ -214,6 +214,41 @@ defmodule Pleroma.Web.ApiSpec.TwitterUtilOperation do
}
end
def move_account_operation do
%Operation{
tags: ["Account credentials"],
summary: "Move account",
security: [%{"oAuth" => ["write:accounts"]}],
operationId: "UtilController.move_account",
requestBody: request_body("Parameters", move_account_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 move_account_request do
%Schema{
title: "MoveAccountRequest",
description: "POST body for moving the account",
type: :object,
required: [:password, :target_account],
properties: %{
password: %Schema{type: :string, description: "Current password"},
target_account: %Schema{
type: :string,
description: "The nickname of the target account to move to"
}
}
}
end
def healthcheck_operation do
%Operation{
tags: ["Accounts"],

View file

@ -343,6 +343,7 @@ defmodule Pleroma.Web.Router do
post("/delete_account", UtilController, :delete_account)
put("/notification_settings", UtilController, :update_notificaton_settings)
post("/disable_account", UtilController, :disable_account)
post("/move_account", UtilController, :move_account)
end
scope "/api/pleroma", Pleroma.Web.PleromaAPI do

View file

@ -11,6 +11,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
alias Pleroma.Emoji
alias Pleroma.Healthcheck
alias Pleroma.User
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.CommonAPI
alias Pleroma.Web.Plugs.OAuthScopesPlug
alias Pleroma.Web.WebFinger
@ -26,7 +27,8 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
:change_password,
:delete_account,
:update_notificaton_settings,
:disable_account
:disable_account,
:move_account
]
)
@ -158,6 +160,35 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do
end
end
def move_account(%{assigns: %{user: user}, body_params: body_params} = conn, %{}) do
case CommonAPI.Utils.confirm_current_password(user, body_params.password) do
{:ok, user} ->
with {:ok, target_user} <- find_user_by_nickname(body_params.target_account),
{:ok, _user} <- ActivityPub.move(user, target_user) do
json(conn, %{status: "success"})
else
{:not_found} ->
json(conn, %{error: "Target account not found."})
{:error, error} ->
json(conn, %{error: error})
end
{:error, msg} ->
json(conn, %{error: msg})
end
end
defp find_user_by_nickname(nickname) do
user = User.get_cached_by_nickname(nickname)
if user == nil do
{:not_found, nil}
else
{:ok, user}
end
end
def captcha(conn, _params) do
json(conn, Pleroma.Captcha.new())
end