From 63477d07adb614413a382a87f06af2bc2495b432 Mon Sep 17 00:00:00 2001 From: Maksim Pechnikov Date: Tue, 12 May 2020 06:44:33 +0300 Subject: [PATCH 1/3] unsubscribes of friends when user deactivated --- lib/mix/tasks/pleroma/user.ex | 14 ++------------ lib/pleroma/user.ex | 20 +++++++++++++++----- test/tasks/user_test.exs | 8 ++++---- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index da140ac86..93ecb4631 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -150,22 +150,12 @@ def run(["unsubscribe", nickname]) do with %User{} = user <- User.get_cached_by_nickname(nickname) do shell_info("Deactivating #{user.nickname}") User.deactivate(user) - - user - |> User.get_friends() - |> Enum.each(fn friend -> - user = User.get_cached_by_id(user.id) - - shell_info("Unsubscribing #{friend.nickname} from #{user.nickname}") - User.unfollow(user, friend) - end) - :timer.sleep(500) user = User.get_cached_by_id(user.id) - if Enum.empty?(User.get_friends(user)) do - shell_info("Successfully unsubscribed all followers from #{user.nickname}") + if Enum.empty?(Enum.filter(User.get_friends(user), & &1.local)) do + shell_info("Successfully unsubscribed all local followers from #{user.nickname}") end else _ -> diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index a86cc3202..1c456b27c 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -750,7 +750,19 @@ def unfollow(%User{ap_id: ap_id}, %User{ap_id: ap_id}) do {:error, "Not subscribed!"} end + @spec unfollow(User.t(), User.t()) :: {:ok, User.t(), Activity.t()} | {:error, String.t()} def unfollow(%User{} = follower, %User{} = followed) do + case do_unfollow(follower, followed) do + {:ok, follower, followed} -> + {:ok, follower, Utils.fetch_latest_follow(follower, followed)} + + error -> + error + end + end + + @spec do_unfollow(User.t(), User.t()) :: {:ok, User.t(), User.t()} | {:error, String.t()} + defp do_unfollow(%User{} = follower, %User{} = followed) do case get_follow_state(follower, followed) do state when state in [:follow_pending, :follow_accept] -> FollowingRelationship.unfollow(follower, followed) @@ -761,7 +773,7 @@ def unfollow(%User{} = follower, %User{} = followed) do |> update_following_count() |> set_cache() - {:ok, follower, Utils.fetch_latest_follow(follower, followed)} + {:ok, follower, followed} nil -> {:error, "Not subscribed!"} @@ -1401,15 +1413,13 @@ def deactivate(%User{} = user, status) do user |> get_followers() |> Enum.filter(& &1.local) - |> Enum.each(fn follower -> - follower |> update_following_count() |> set_cache() - end) + |> Enum.each(&set_cache(update_following_count(&1))) # Only update local user counts, remote will be update during the next pull. user |> get_friends() |> Enum.filter(& &1.local) - |> Enum.each(&update_follower_count/1) + |> Enum.each(&do_unfollow(user, &1)) {:ok, user} end diff --git a/test/tasks/user_test.exs b/test/tasks/user_test.exs index b4f68d494..4b3ab5a87 100644 --- a/test/tasks/user_test.exs +++ b/test/tasks/user_test.exs @@ -172,23 +172,23 @@ test "no user to toggle" do describe "running unsubscribe" do test "user is unsubscribed" do followed = insert(:user) + remote_followed = insert(:user, local: false) user = insert(:user) + User.follow(user, followed, :follow_accept) + User.follow(user, remote_followed, :follow_accept) Mix.Tasks.Pleroma.User.run(["unsubscribe", user.nickname]) assert_received {:mix_shell, :info, [message]} assert message =~ "Deactivating" - assert_received {:mix_shell, :info, [message]} - assert message =~ "Unsubscribing" - # Note that the task has delay :timer.sleep(500) assert_received {:mix_shell, :info, [message]} assert message =~ "Successfully unsubscribed" user = User.get_cached_by_nickname(user.nickname) - assert Enum.empty?(User.get_friends(user)) + assert Enum.empty?(Enum.filter(User.get_friends(user), & &1.local)) assert user.deactivated end From 9bc5e18adeef2c68c5fae2435ed01555f1b29c93 Mon Sep 17 00:00:00 2001 From: Maksim Pechnikov Date: Thu, 21 May 2020 08:06:57 +0300 Subject: [PATCH 2/3] rename mix task: `pleroma.user unsubscribe` -> `pleroma.user deactivate` --- docs/administration/CLI_tasks/user.md | 19 +++++++++---------- lib/mix/tasks/pleroma/user.ex | 6 +++--- test/tasks/user_test.exs | 8 ++++---- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/docs/administration/CLI_tasks/user.md b/docs/administration/CLI_tasks/user.md index f535dad82..797641898 100644 --- a/docs/administration/CLI_tasks/user.md +++ b/docs/administration/CLI_tasks/user.md @@ -95,33 +95,33 @@ mix pleroma.user sign_out ``` -## Deactivate or activate a user +## Deactivate or activate a user ```sh tab="OTP" - ./bin/pleroma_ctl user toggle_activated + ./bin/pleroma_ctl user toggle_activated ``` ```sh tab="From Source" -mix pleroma.user toggle_activated +mix pleroma.user toggle_activated ``` -## Unsubscribe local users from a user and deactivate the user +## Deactivate a user and unsubscribes local users from the user ```sh tab="OTP" - ./bin/pleroma_ctl user unsubscribe NICKNAME + ./bin/pleroma_ctl user deactivate NICKNAME ``` ```sh tab="From Source" -mix pleroma.user unsubscribe NICKNAME +mix pleroma.user deactivate NICKNAME ``` -## Unsubscribe local users from an instance and deactivate all accounts on it +## Deactivate all accounts from an instance and unsubscribe local users on it ```sh tab="OTP" - ./bin/pleroma_ctl user unsubscribe_all_from_instance + ./bin/pleroma_ctl user deacitivate_all_from_instance ``` ```sh tab="From Source" -mix pleroma.user unsubscribe_all_from_instance +mix pleroma.user deactivate_all_from_instance ``` @@ -177,4 +177,3 @@ mix pleroma.user untag ```sh tab="From Source" mix pleroma.user toggle_confirmed ``` - diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index 93ecb4631..3635c02bc 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -144,7 +144,7 @@ def run(["reset_password", nickname]) do end end - def run(["unsubscribe", nickname]) do + def run(["deactivate", nickname]) do start_pleroma() with %User{} = user <- User.get_cached_by_nickname(nickname) do @@ -163,7 +163,7 @@ def run(["unsubscribe", nickname]) do end end - def run(["unsubscribe_all_from_instance", instance]) do + def run(["deactivate_all_from_instance", instance]) do start_pleroma() Pleroma.User.Query.build(%{nickname: "@#{instance}"}) @@ -171,7 +171,7 @@ def run(["unsubscribe_all_from_instance", instance]) do |> Stream.each(fn users -> users |> Enum.each(fn user -> - run(["unsubscribe", user.nickname]) + run(["deactivate", user.nickname]) end) end) |> Stream.run() diff --git a/test/tasks/user_test.exs b/test/tasks/user_test.exs index 4b3ab5a87..ab7637511 100644 --- a/test/tasks/user_test.exs +++ b/test/tasks/user_test.exs @@ -169,7 +169,7 @@ test "no user to toggle" do end end - describe "running unsubscribe" do + describe "running deactivate" do test "user is unsubscribed" do followed = insert(:user) remote_followed = insert(:user, local: false) @@ -178,7 +178,7 @@ test "user is unsubscribed" do User.follow(user, followed, :follow_accept) User.follow(user, remote_followed, :follow_accept) - Mix.Tasks.Pleroma.User.run(["unsubscribe", user.nickname]) + Mix.Tasks.Pleroma.User.run(["deactivate", user.nickname]) assert_received {:mix_shell, :info, [message]} assert message =~ "Deactivating" @@ -192,8 +192,8 @@ test "user is unsubscribed" do assert user.deactivated end - test "no user to unsubscribe" do - Mix.Tasks.Pleroma.User.run(["unsubscribe", "nonexistent"]) + test "no user to deactivate" do + Mix.Tasks.Pleroma.User.run(["deactivate", "nonexistent"]) assert_received {:mix_shell, :error, [message]} assert message =~ "No user" From 4ae2f75c3e5d293c24fac978b1ae10fdfa7a3c00 Mon Sep 17 00:00:00 2001 From: lain Date: Thu, 21 May 2020 10:27:06 +0000 Subject: [PATCH 3/3] Apply suggestion to docs/administration/CLI_tasks/user.md --- docs/administration/CLI_tasks/user.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/administration/CLI_tasks/user.md b/docs/administration/CLI_tasks/user.md index 797641898..afeb8d52f 100644 --- a/docs/administration/CLI_tasks/user.md +++ b/docs/administration/CLI_tasks/user.md @@ -117,7 +117,7 @@ mix pleroma.user deactivate NICKNAME ## Deactivate all accounts from an instance and unsubscribe local users on it ```sh tab="OTP" - ./bin/pleroma_ctl user deacitivate_all_from_instance + ./bin/pleroma_ctl user deactivate_all_from_instance ``` ```sh tab="From Source"