AdminAPI: add DELETE /instances/:instance to delete all content from a remote instance

This commit is contained in:
Alex Gleason 2021-07-17 14:55:05 -05:00
parent f67d00d12b
commit 54dbcfe02a
No known key found for this signature in database
GPG key ID: 7211D1F99744FBB7
4 changed files with 48 additions and 0 deletions

View file

@ -319,6 +319,22 @@ Note: Available `:permission_group` is currently moderator and admin. 404 is ret
}
```
## `DELETE /api/v1/pleroma/admin/instances/:instance`
### Delete all users and activities from a remote instance
Note: this will trigger a job to remove instance content in the background.
It may take some time.
- Params:
- `instance`: remote instance host
- Response:
- The `instance` name as a string
```json
"lain.com"
```
## `GET /api/v1/pleroma/admin/statuses`
### Retrives all latest statuses

View file

@ -7,6 +7,7 @@ defmodule Pleroma.Web.AdminAPI.InstanceController do
import Pleroma.Web.ControllerHelper, only: [fetch_integer_param: 3]
alias Pleroma.Instances.Instance
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.AdminAPI
alias Pleroma.Web.Plugs.OAuthScopesPlug
@ -21,6 +22,12 @@ defmodule Pleroma.Web.AdminAPI.InstanceController do
when action in [:list_instance_statuses]
)
plug(
OAuthScopesPlug,
%{scopes: ["admin:write:accounts", "admin:write:statuses"]}
when action in [:delete_instance]
)
action_fallback(AdminAPI.FallbackController)
def list_instance_statuses(conn, %{"instance" => instance} = params) do
@ -41,6 +48,12 @@ def list_instance_statuses(conn, %{"instance" => instance} = params) do
|> render("index.json", %{total: result[:total], activities: result[:items], as: :activity})
end
def delete_instance(conn, %{"instance" => instance}) do
with {:ok, _job} <- Instance.delete_users_and_activities(instance) do
json(conn, instance)
end
end
defp page_params(params) do
{
fetch_integer_param(params, "page", 1),

View file

@ -210,6 +210,7 @@ defmodule Pleroma.Web.Router do
get("/users/:nickname/chats", AdminAPIController, :list_user_chats)
get("/instances/:instance/statuses", InstanceController, :list_instance_statuses)
delete("/instances/:instance", InstanceController, :delete_instance)
get("/instance_document/:name", InstanceDocumentController, :show)
patch("/instance_document/:name", InstanceDocumentController, :update)

View file

@ -8,6 +8,8 @@ defmodule Pleroma.Web.AdminAPI.InstanceControllerTest do
import Pleroma.Factory
alias Pleroma.Repo
alias Pleroma.Tests.ObanHelpers
alias Pleroma.Web.CommonAPI
setup_all do
@ -61,4 +63,20 @@ test "GET /instances/:instance/statuses", %{conn: conn} do
assert length(activities) == 3
end
end
test "DELETE /instances/:instance", %{conn: conn} do
user = insert(:user, nickname: "lain@lain.com")
post = insert(:note_activity, user: user)
response =
conn
|> delete("/api/pleroma/admin/instances/lain.com")
|> json_response(200)
[:ok] = ObanHelpers.perform_all()
assert response == "lain.com"
refute Repo.reload(user).is_active
refute Repo.reload(post)
end
end