From ebd7d1365bccfc822b1df87f1a58c59570672a56 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Tue, 13 Oct 2020 17:16:03 -0500 Subject: [PATCH] Make setting user activation status explicit --- lib/mix/tasks/pleroma/user.ex | 2 +- lib/pleroma/user.ex | 18 +++++++++--------- .../admin_api/controllers/user_controller.ex | 6 +++--- .../twitter_api/controllers/util_controller.ex | 2 +- lib/pleroma/workers/background_worker.ex | 4 ++-- test/pleroma/user_test.exs | 12 ++++++------ .../controllers/user_controller_test.exs | 2 +- .../controllers/instance_controller_test.exs | 2 +- 8 files changed, 24 insertions(+), 24 deletions(-) diff --git a/lib/mix/tasks/pleroma/user.ex b/lib/mix/tasks/pleroma/user.ex index 133daf0f0..a1276d67b 100644 --- a/lib/mix/tasks/pleroma/user.ex +++ b/lib/mix/tasks/pleroma/user.ex @@ -146,7 +146,7 @@ defmodule Mix.Tasks.Pleroma.User do with %User{} = user <- User.get_cached_by_nickname(nickname) do shell_info("Deactivating #{user.nickname}") - User.deactivate(user) + User.set_activation(user, false) :timer.sleep(500) user = User.get_cached_by_id(user.id) diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index f002c077c..e53a0f313 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -1583,20 +1583,20 @@ defmodule Pleroma.User do defp maybe_filter_on_ap_id(query, _ap_ids), do: query - def deactivate_async(user, status \\ true) do - BackgroundWorker.enqueue("deactivate_user", %{"user_id" => user.id, "status" => status}) + def set_activation_async(user, status \\ true) do + BackgroundWorker.enqueue("user_activation", %{"user_id" => user.id, "status" => status}) end - def deactivate(user, status \\ true) - - def deactivate(users, status) when is_list(users) do + @spec set_activation([User.t()], boolean()) :: {:ok, User.t()} | {:error, Changeset.t()} + def set_activation(users, status) when is_list(users) do Repo.transaction(fn -> - for user <- users, do: deactivate(user, status) + for user <- users, do: set_activation(user, status) end) end - def deactivate(%User{} = user, status) do - with {:ok, user} <- set_activation_status(user, !status) do + @spec set_activation(User.t(), boolean()) :: {:ok, User.t()} | {:error, Changeset.t()} + def set_activation(%User{} = user, status) do + with {:ok, user} <- set_activation_status(user, status) do user |> get_followers() |> Enum.filter(& &1.local) @@ -1758,7 +1758,7 @@ defmodule Pleroma.User do delete_or_deactivate(user) end - def perform(:deactivate_async, user, status), do: deactivate(user, status) + def perform(:set_activation_async, user, status), do: set_activation(user, status) @spec external_users_query() :: Ecto.Query.t() def external_users_query do diff --git a/lib/pleroma/web/admin_api/controllers/user_controller.ex b/lib/pleroma/web/admin_api/controllers/user_controller.ex index 83dd3c918..a18b9f8d5 100644 --- a/lib/pleroma/web/admin_api/controllers/user_controller.ex +++ b/lib/pleroma/web/admin_api/controllers/user_controller.ex @@ -172,7 +172,7 @@ defmodule Pleroma.Web.AdminAPI.UserController do def toggle_activation(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do user = User.get_cached_by_nickname(nickname) - {:ok, updated_user} = User.deactivate(user, !user.is_active) + {:ok, updated_user} = User.set_activation(user, !user.is_active) action = if !user.is_active, do: "activate", else: "deactivate" @@ -189,7 +189,7 @@ defmodule Pleroma.Web.AdminAPI.UserController do def activate(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do users = Enum.map(nicknames, &User.get_cached_by_nickname/1) - {:ok, updated_users} = User.deactivate(users, false) + {:ok, updated_users} = User.set_activation(users, true) ModerationLog.insert_log(%{ actor: admin, @@ -204,7 +204,7 @@ defmodule Pleroma.Web.AdminAPI.UserController do def deactivate(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do users = Enum.map(nicknames, &User.get_cached_by_nickname/1) - {:ok, updated_users} = User.deactivate(users, true) + {:ok, updated_users} = User.set_activation(users, false) ModerationLog.insert_log(%{ actor: admin, diff --git a/lib/pleroma/web/twitter_api/controllers/util_controller.ex b/lib/pleroma/web/twitter_api/controllers/util_controller.ex index 1e252f7bb..940a645bb 100644 --- a/lib/pleroma/web/twitter_api/controllers/util_controller.ex +++ b/lib/pleroma/web/twitter_api/controllers/util_controller.ex @@ -150,7 +150,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilController do def disable_account(%{assigns: %{user: user}} = conn, params) do case CommonAPI.Utils.confirm_current_password(user, params["password"]) do {:ok, user} -> - User.deactivate_async(user) + User.set_activation_async(user, false) json(conn, %{status: "success"}) {:error, msg} -> diff --git a/lib/pleroma/workers/background_worker.ex b/lib/pleroma/workers/background_worker.ex index e24b9c175..1e28384cb 100644 --- a/lib/pleroma/workers/background_worker.ex +++ b/lib/pleroma/workers/background_worker.ex @@ -9,9 +9,9 @@ defmodule Pleroma.Workers.BackgroundWorker do @impl Oban.Worker - def perform(%Job{args: %{"op" => "deactivate_user", "user_id" => user_id, "status" => status}}) do + def perform(%Job{args: %{"op" => "user_activation", "user_id" => user_id, "status" => status}}) do user = User.get_cached_by_id(user_id) - User.perform(:deactivate_async, user, status) + User.perform(:set_activation_async, user, status) end def perform(%Job{args: %{"op" => "delete_user", "user_id" => user_id}}) do diff --git a/test/pleroma/user_test.exs b/test/pleroma/user_test.exs index 42e37f0a5..36fe84871 100644 --- a/test/pleroma/user_test.exs +++ b/test/pleroma/user_test.exs @@ -1313,13 +1313,13 @@ defmodule Pleroma.UserTest do end end - describe ".deactivate" do + describe ".set_activation" do test "can de-activate then re-activate a user" do user = insert(:user) assert user.is_active - {:ok, user} = User.deactivate(user) + {:ok, user} = User.set_activation(user, false) refute user.is_active - {:ok, user} = User.deactivate(user, false) + {:ok, user} = User.set_activation(user, true) assert user.is_active end @@ -1328,7 +1328,7 @@ defmodule Pleroma.UserTest do user2 = insert(:user) {:ok, user, user2} = User.follow(user, user2) - {:ok, _user} = User.deactivate(user) + {:ok, _user} = User.set_activation(user, false) user2 = User.get_cached_by_id(user2.id) @@ -1344,7 +1344,7 @@ defmodule Pleroma.UserTest do assert user2.following_count == 1 assert User.following_count(user2) == 1 - {:ok, _user} = User.deactivate(user) + {:ok, _user} = User.set_activation(user, false) user2 = User.get_cached_by_id(user2.id) @@ -1374,7 +1374,7 @@ defmodule Pleroma.UserTest do user: user2 }) - {:ok, _user} = User.deactivate(user) + {:ok, _user} = User.set_activation(user, false) assert [] == ActivityPub.fetch_public_activities(%{}) assert [] == Pleroma.Notification.for_user(user2) diff --git a/test/pleroma/web/admin_api/controllers/user_controller_test.exs b/test/pleroma/web/admin_api/controllers/user_controller_test.exs index 675903217..42a135847 100644 --- a/test/pleroma/web/admin_api/controllers/user_controller_test.exs +++ b/test/pleroma/web/admin_api/controllers/user_controller_test.exs @@ -940,7 +940,7 @@ defmodule Pleroma.Web.AdminAPI.UserControllerTest do assert json_response(conn, 200) == user_response( user, - %{"is_active" => user.is_active} + %{"is_active" => !user.is_active} ) log_entry = Repo.one(ModerationLog) diff --git a/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs b/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs index d7bb0ffd8..0d4eebb73 100644 --- a/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs +++ b/test/pleroma/web/mastodon_api/controllers/instance_controller_test.exs @@ -57,7 +57,7 @@ defmodule Pleroma.Web.MastodonAPI.InstanceControllerTest do user = insert(:user, %{local: true}) user2 = insert(:user, %{local: true}) - {:ok, _user2} = User.deactivate(user2, user2.is_active) + {:ok, _user2} = User.set_activation(user2, false) insert(:user, %{local: false, nickname: "u@peer1.com"}) insert(:user, %{local: false, nickname: "u@peer2.com"})