diff --git a/docs/development/API/admin_api.md b/docs/development/API/admin_api.md index 8f855d251..82483fae7 100644 --- a/docs/development/API/admin_api.md +++ b/docs/development/API/admin_api.md @@ -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 diff --git a/lib/pleroma/web/admin_api/controllers/instance_controller.ex b/lib/pleroma/web/admin_api/controllers/instance_controller.ex index ad6ea89ef..2fc2fb442 100644 --- a/lib/pleroma/web/admin_api/controllers/instance_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/instance_controller.ex @@ -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), diff --git a/lib/pleroma/web/router.ex b/lib/pleroma/web/router.ex index dbc7df104..1f465b697 100644 --- a/lib/pleroma/web/router.ex +++ b/lib/pleroma/web/router.ex @@ -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) diff --git a/test/pleroma/web/admin_api/controllers/instance_controller_test.exs b/test/pleroma/web/admin_api/controllers/instance_controller_test.exs index b70efcf23..42248c6bd 100644 --- a/test/pleroma/web/admin_api/controllers/instance_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/instance_controller_test.exs @@ -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