Purge a local user upon deletion, fixes #2062
authorAlex Gleason <alex@alexgleason.me>
Tue, 18 Aug 2020 02:52:28 +0000 (21:52 -0500)
committerAlex Gleason <alex@alexgleason.me>
Tue, 18 Aug 2020 02:52:28 +0000 (21:52 -0500)
lib/pleroma/user.ex
test/web/admin_api/controllers/admin_api_controller_test.exs

index ac065e9dcc6b86e1574d54d2eaaea36b426e36da..a8bdcdad70c8944ed5c0f3f69d9b8f9103ff69f3 100644 (file)
@@ -1583,6 +1583,18 @@ defmodule Pleroma.User do
     |> update_and_set_cache()
   end
 
+  @spec purge_user_changeset(User.t()) :: Changeset.t()
+  def purge_user_changeset(user) do
+    change(user, %{
+      deactivated: true,
+      email: nil,
+      avatar: %{},
+      banner: %{},
+      background: %{},
+      fields: []
+    })
+  end
+
   def delete(users) when is_list(users) do
     for user <- users, do: delete(user)
   end
@@ -1610,7 +1622,7 @@ defmodule Pleroma.User do
 
       _ ->
         user
-        |> change(%{deactivated: true, email: nil})
+        |> purge_user_changeset()
         |> update_and_set_cache()
     end
   end
index 66d4b1ef36bf03666cf32f4ad9610738aa479be8..f23d23e05b03d42b715e6f174684a1e056707ff1 100644 (file)
@@ -155,13 +155,28 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
 
   describe "DELETE /api/pleroma/admin/users" do
     test "single user", %{admin: admin, conn: conn} do
-      user = insert(:user)
       clear_config([:instance, :federating], true)
 
+      user =
+        insert(:user,
+          avatar: %{"url" => [%{"href" => "https://someurl"}]},
+          banner: %{"url" => [%{"href" => "https://somebanner"}]}
+        )
+
+      # Create some activities to check they got deleted later
+      follower = insert(:user)
+      {:ok, _} = CommonAPI.post(user, %{status: "test"})
+      {:ok, _, _, _} = CommonAPI.follow(user, follower)
+      {:ok, _, _, _} = CommonAPI.follow(follower, user)
+      user = Repo.get(User, user.id)
+      assert user.note_count == 1
+      assert user.follower_count == 1
+      assert user.following_count == 1
       refute user.deactivated
 
       with_mock Pleroma.Web.Federator,
-        publish: fn _ -> nil end do
+        publish: fn _ -> nil end,
+        perform: fn _, _ -> nil end do
         conn =
           conn
           |> put_req_header("accept", "application/json")
@@ -181,6 +196,12 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
         user = Repo.get(User, user.id)
         assert user.deactivated
 
+        assert user.avatar == %{}
+        assert user.banner == %{}
+        assert user.note_count == 0
+        assert user.follower_count == 0
+        assert user.following_count == 0
+
         assert called(Pleroma.Web.Federator.publish(:_))
       end
     end