AdminAPI: add DELETE /instances/:instance to delete all content from a remote instance
authorAlex Gleason <alex@alexgleason.me>
Sat, 17 Jul 2021 19:55:05 +0000 (14:55 -0500)
committerAlex Gleason <alex@alexgleason.me>
Sat, 17 Jul 2021 19:55:05 +0000 (14:55 -0500)
docs/development/API/admin_api.md
lib/pleroma/web/admin_api/controllers/instance_controller.ex
lib/pleroma/web/router.ex
test/pleroma/web/admin_api/controllers/instance_controller_test.exs

index 8f855d251a5ac2b9c2cb945c0534b8699d3f4605..82483fae71deaf9f82c8ff6afb3ef2f913183083 100644 (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
index ad6ea89efb2b2d93b15eb2fcf955948e987add76..2fc2fb442038209c3c876d18a10a892e55afb572 100644 (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 @@ defmodule Pleroma.Web.AdminAPI.InstanceController 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),
index dbc7df104eaa5cdab68f7cece2dc60a2e14b16a9..1f465b697df8a790de277397b6e467166a03fcec 100644 (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)
index b70efcf23506b315299cba2214249dc5e7fc30c3..42248c6bd56401dcf57928ef1570b436f09c1897 100644 (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 @@ defmodule Pleroma.Web.AdminAPI.InstanceControllerTest 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