add `user delete_activities` mix task
authorEgor Kislitsyn <egor@kislitsyn.com>
Tue, 2 Apr 2019 09:30:11 +0000 (16:30 +0700)
committerEgor Kislitsyn <egor@kislitsyn.com>
Tue, 2 Apr 2019 09:30:11 +0000 (16:30 +0700)
lib/mix/tasks/pleroma/user.ex
lib/pleroma/user.ex
test/tasks/user_test.exs
test/user_test.exs

index 680422c198da3c2d78893b2e32bdb3811eb92cf6..62c9fceda3a80cafe914c4a686e0763d6c4b4ecd 100644 (file)
@@ -23,7 +23,7 @@ defmodule Mix.Tasks.Pleroma.User do
   - `--password PASSWORD` - the user's password
   - `--moderator`/`--no-moderator` - whether the user is a moderator
   - `--admin`/`--no-admin` - whether the user is an admin
-  - `-y`, `--assume-yes`/`--no-assume-yes` - whether to assume yes to all questions 
+  - `-y`, `--assume-yes`/`--no-assume-yes` - whether to assume yes to all questions
 
   ## Generate an invite link.
 
@@ -33,6 +33,10 @@ defmodule Mix.Tasks.Pleroma.User do
 
       mix pleroma.user rm NICKNAME
 
+  ## Delete the user's activities.
+
+      mix pleroma.user delete_activities NICKNAME
+
   ## Deactivate or activate the user's account.
 
       mix pleroma.user toggle_activated NICKNAME
@@ -309,7 +313,7 @@ defmodule Mix.Tasks.Pleroma.User do
 
     with %User{local: true} = user <- User.get_by_nickname(nickname) do
       User.delete_user_activities(user)
-      Mix.shell().info("User #{nickname} statuses deleted..")
+      Mix.shell().info("User #{nickname} statuses deleted.")
     else
       _ ->
         Mix.shell().error("No local user #{nickname}")
index 18cf374dd22f93e22c724cb935f82f4e17006295..a180c1a8b66e53a7842acd1e068792c1ee57d99a 100644 (file)
@@ -1088,29 +1088,30 @@ defmodule Pleroma.User do
     # Remove all relationships
     {:ok, followers} = User.get_followers(user)
 
-    followers
-    |> Enum.each(fn follower -> User.unfollow(follower, user) end)
+    Enum.each(followers, fn follower -> User.unfollow(follower, user) end)
 
     {:ok, friends} = User.get_friends(user)
 
-    friends
-    |> Enum.each(fn followed -> User.unfollow(user, followed) end)
+    Enum.each(friends, fn followed -> User.unfollow(user, followed) end)
 
-    query =
-      from(a in Activity, where: a.actor == ^user.ap_id)
-      |> Activity.with_preloaded_object()
+    delete_user_activities(user)
+  end
 
-    Repo.all(query)
-    |> Enum.each(fn activity ->
-      case activity.data["type"] do
-        "Create" ->
-          ActivityPub.delete(Object.normalize(activity))
+  def delete_user_activities(%User{ap_id: ap_id} = user) do
+    Activity
+    |> where(actor: ^ap_id)
+    |> Activity.with_preloaded_object()
+    |> Repo.all()
+    |> Enum.each(fn
+      %{data: %{"type" => "Create"}} = activity ->
+        activity |> Object.normalize() |> ActivityPub.delete()
 
-        # TODO: Do something with likes, follows, repeats.
-        _ ->
-          "Doing nothing"
-      end
+      # TODO: Do something with likes, follows, repeats.
+      _ ->
+        "Doing nothing"
     end)
+
+    {:ok, user}
   end
 
   def html_filter_policy(%User{info: %{no_rich_text: true}}) do
index 7b814d171dad113cd0677d4f6547a34117f0d771..1030bd555ef2bd154914899c1ee6ec8ccb7aa300 100644 (file)
@@ -248,4 +248,14 @@ defmodule Mix.Tasks.Pleroma.UserTest do
       assert message =~ "Generated"
     end
   end
+
+  describe "running delete_activities" do
+    test "activities are deleted" do
+      %{nickname: nickname} = insert(:user)
+
+      assert :ok == Mix.Tasks.Pleroma.User.run(["delete_activities", nickname])
+      assert_received {:mix_shell, :info, [message]}
+      assert message == "User #{nickname} statuses deleted."
+    end
+  end
 end
index 8cf2ba6ab1d5251c6b71ccb783853e9d7f43564d..a6fd35edeaf45769650a6abaacc85a256b64b3e0 100644 (file)
@@ -792,6 +792,16 @@ defmodule Pleroma.UserTest do
     assert false == user.info.deactivated
   end
 
+  test ".delete_user_activities deletes all create activities" do
+    user = insert(:user)
+
+    {:ok, activity} = CommonAPI.post(user, %{"status" => "2hu"})
+    {:ok, _} = User.delete_user_activities(user)
+
+    # TODO: Remove favorites, repeats, delete activities.
+    refute Activity.get_by_id(activity.id)
+  end
+
   test ".delete deactivates a user, all follow relationships and all create activities" do
     user = insert(:user)
     followed = insert(:user)