From 7983b0bdfe40c88d159af2f0562d96e8812c31e3 Mon Sep 17 00:00:00 2001 From: Rin Toshaka Date: Sun, 2 Dec 2018 18:05:59 +0100 Subject: [PATCH] Move unsubscribe user task to pleroma/user.ex. Delete unsubscribe_user.ex. Fix pleroma.user toggle_activated to work not only on local users. --- lib/mix/tasks/pleroma/user.ex | 33 +++++++++++++++++++++++++-- lib/mix/tasks/unsubscribe_user.ex | 38 ------------------------------- 2 files changed, 31 insertions(+), 40 deletions(-) delete mode 100644 lib/mix/tasks/unsubscribe_user.ex diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index d7a6be844..d7e172994 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -132,12 +132,12 @@ def run(["rm", nickname]) do def run(["toggle_activated", nickname]) do Mix.Task.run("app.start") - with %User{local: true} = user <- User.get_by_nickname(nickname) do + with %User{} = user <- User.get_by_nickname(nickname) do User.deactivate(user, !user.info["deactivated"]) Mix.shell().info("Activation status of #{nickname}: #{user.info["deactivated"]}") else _ -> - Mix.shell().error("No local user #{nickname}") + Mix.shell().error("No user #{nickname}") end end @@ -163,6 +163,35 @@ def run(["reset_password", nickname]) do end end + def run(["unsubscribe", nickname]) do + Mix.Task.run("app.start") + + with %User{} = user <- User.get_by_nickname(nickname) do + Mix.shell().info("Deactivating #{user.nickname}") + User.deactivate(user) + + {:ok, friends} = User.get_friends(user) + + Enum.each(friends, fn friend -> + user = Repo.get(User, user.id) + + Mix.shell().info("Unsubscribing #{friend.nickname} from #{user.nickname}") + User.unfollow(user, friend) + end) + + :timer.sleep(500) + + user = Repo.get(User, user.id) + + if length(user.following) == 0 do + Mix.shell().info("Successfully unsubscribed all followers from #{user.nickname}") + end + else + _ -> + Mix.shell().error("No user #{nickname}") + end + end + def run(["set", nickname | rest]) do {options, [], []} = OptionParser.parse( diff --git a/lib/mix/tasks/unsubscribe_user.ex b/lib/mix/tasks/unsubscribe_user.ex deleted file mode 100644 index 62ea61a5c..000000000 --- a/lib/mix/tasks/unsubscribe_user.ex +++ /dev/null @@ -1,38 +0,0 @@ -defmodule Mix.Tasks.UnsubscribeUser do - use Mix.Task - alias Pleroma.{User, Repo} - require Logger - - @moduledoc """ - Deactivate and Unsubscribe local users from a user - - Usage: ``mix unsubscribe_user `` - - Example: ``mix unsubscribe_user lain`` - """ - def run([nickname]) do - Mix.Task.run("app.start") - - with %User{} = user <- User.get_by_nickname(nickname) do - Logger.info("Deactivating #{user.nickname}") - User.deactivate(user) - - {:ok, friends} = User.get_friends(user) - - Enum.each(friends, fn friend -> - user = Repo.get(User, user.id) - - Logger.info("Unsubscribing #{friend.nickname} from #{user.nickname}") - User.unfollow(user, friend) - end) - - :timer.sleep(500) - - user = Repo.get(User, user.id) - - if length(user.following) == 0 do - Logger.info("Successfully unsubscribed all followers from #{user.nickname}") - end - end - end -end