From d013b58e84f2c8213a5d26b1c3daf36e2f4807e5 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 12 Mar 2019 22:04:08 +0700 Subject: [PATCH 1/3] add `mix pleroma.user delete_activities NICKNAME` task --- lib/mix/tasks/pleroma/user.ex | 12 ++++++++++++ lib/pleroma/user.ex | 8 ++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index 037e44716..e232df14f 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -304,6 +304,18 @@ def run(["invite"]) do end end + def run(["delete_activities", nickname]) do + Common.start_pleroma() + + with %User{local: true} = user <- User.get_by_nickname(nickname) do + User.delete_user_activities(user) + Mix.shell().info("User #{nickname} deleted.") + else + _ -> + Mix.shell().error("No local user #{nickname}") + end + end + defp set_moderator(user, value) do info_cng = User.Info.admin_api_update(user.info, %{is_moderator: value}) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 50e7e7ccd..2d0a8cde4 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1083,6 +1083,12 @@ def delete(%User{} = user) do friends |> Enum.each(fn followed -> User.unfollow(user, followed) end) + delete_user_activities(user) + + {:ok, user} + end + + def delete_user_activities(user) do query = from(a in Activity, where: a.actor == ^user.ap_id) Repo.all(query) @@ -1096,8 +1102,6 @@ def delete(%User{} = user) do "Doing nothing" end end) - - {:ok, user} end def html_filter_policy(%User{info: %{no_rich_text: true}}) do From 16e598ec11cb9178b9fc53a1ec4b649d97c5f3b8 Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 12 Mar 2019 22:12:05 +0700 Subject: [PATCH 2/3] fix wording --- lib/mix/tasks/pleroma/user.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index e232df14f..ec06d908a 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -309,7 +309,7 @@ def run(["delete_activities", nickname]) do with %User{local: true} = user <- User.get_by_nickname(nickname) do User.delete_user_activities(user) - Mix.shell().info("User #{nickname} deleted.") + Mix.shell().info("User #{nickname} statuses deleted..") else _ -> Mix.shell().error("No local user #{nickname}") From a14742f495fac78f4dfd7ab02f4c3ae5c7c37c3b Mon Sep 17 00:00:00 2001 From: Egor Kislitsyn Date: Tue, 2 Apr 2019 16:30:11 +0700 Subject: [PATCH 3/3] add `user delete_activities` mix task --- lib/mix/tasks/pleroma/user.ex | 8 ++++++-- lib/pleroma/user.ex | 33 +++++++++++++++++---------------- test/tasks/user_test.exs | 10 ++++++++++ test/user_test.exs | 10 ++++++++++ 4 files changed, 43 insertions(+), 18 deletions(-) diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index 680422c19..62c9fceda 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -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 @@ def run(["delete_activities", nickname]) 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}") diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 18cf374dd..a180c1a8b 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1088,29 +1088,30 @@ def delete(%User{} = 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 diff --git a/test/tasks/user_test.exs b/test/tasks/user_test.exs index 7b814d171..1030bd555 100644 --- a/test/tasks/user_test.exs +++ b/test/tasks/user_test.exs @@ -248,4 +248,14 @@ test "invite token is generated" 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 diff --git a/test/user_test.exs b/test/user_test.exs index 8cf2ba6ab..a6fd35ede 100644 --- a/test/user_test.exs +++ b/test/user_test.exs @@ -792,6 +792,16 @@ test ".deactivate can de-activate then re-activate a user" 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)