Make setting user activation status explicit
This commit is contained in:
parent
7516660753
commit
ebd7d1365b
8 changed files with 24 additions and 24 deletions
|
@ -146,7 +146,7 @@ def run(["deactivate", nickname]) do
|
||||||
|
|
||||||
with %User{} = user <- User.get_cached_by_nickname(nickname) do
|
with %User{} = user <- User.get_cached_by_nickname(nickname) do
|
||||||
shell_info("Deactivating #{user.nickname}")
|
shell_info("Deactivating #{user.nickname}")
|
||||||
User.deactivate(user)
|
User.set_activation(user, false)
|
||||||
:timer.sleep(500)
|
:timer.sleep(500)
|
||||||
|
|
||||||
user = User.get_cached_by_id(user.id)
|
user = User.get_cached_by_id(user.id)
|
||||||
|
|
|
@ -1583,20 +1583,20 @@ defp maybe_filter_on_ap_id(query, ap_ids) when is_list(ap_ids) do
|
||||||
|
|
||||||
defp maybe_filter_on_ap_id(query, _ap_ids), do: query
|
defp maybe_filter_on_ap_id(query, _ap_ids), do: query
|
||||||
|
|
||||||
def deactivate_async(user, status \\ true) do
|
def set_activation_async(user, status \\ true) do
|
||||||
BackgroundWorker.enqueue("deactivate_user", %{"user_id" => user.id, "status" => status})
|
BackgroundWorker.enqueue("user_activation", %{"user_id" => user.id, "status" => status})
|
||||||
end
|
end
|
||||||
|
|
||||||
def deactivate(user, status \\ true)
|
@spec set_activation([User.t()], boolean()) :: {:ok, User.t()} | {:error, Changeset.t()}
|
||||||
|
def set_activation(users, status) when is_list(users) do
|
||||||
def deactivate(users, status) when is_list(users) do
|
|
||||||
Repo.transaction(fn ->
|
Repo.transaction(fn ->
|
||||||
for user <- users, do: deactivate(user, status)
|
for user <- users, do: set_activation(user, status)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
def deactivate(%User{} = user, status) do
|
@spec set_activation(User.t(), boolean()) :: {:ok, User.t()} | {:error, Changeset.t()}
|
||||||
with {:ok, user} <- set_activation_status(user, !status) do
|
def set_activation(%User{} = user, status) do
|
||||||
|
with {:ok, user} <- set_activation_status(user, status) do
|
||||||
user
|
user
|
||||||
|> get_followers()
|
|> get_followers()
|
||||||
|> Enum.filter(& &1.local)
|
|> Enum.filter(& &1.local)
|
||||||
|
@ -1758,7 +1758,7 @@ def perform(:delete, %User{} = user) do
|
||||||
delete_or_deactivate(user)
|
delete_or_deactivate(user)
|
||||||
end
|
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()
|
@spec external_users_query() :: Ecto.Query.t()
|
||||||
def external_users_query do
|
def external_users_query do
|
||||||
|
|
|
@ -172,7 +172,7 @@ def show(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do
|
||||||
def toggle_activation(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do
|
def toggle_activation(%{assigns: %{user: admin}} = conn, %{"nickname" => nickname}) do
|
||||||
user = User.get_cached_by_nickname(nickname)
|
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"
|
action = if !user.is_active, do: "activate", else: "deactivate"
|
||||||
|
|
||||||
|
@ -189,7 +189,7 @@ def toggle_activation(%{assigns: %{user: admin}} = conn, %{"nickname" => nicknam
|
||||||
|
|
||||||
def activate(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
|
def activate(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
|
||||||
users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
|
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(%{
|
ModerationLog.insert_log(%{
|
||||||
actor: admin,
|
actor: admin,
|
||||||
|
@ -204,7 +204,7 @@ def activate(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
|
||||||
|
|
||||||
def deactivate(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
|
def deactivate(%{assigns: %{user: admin}} = conn, %{"nicknames" => nicknames}) do
|
||||||
users = Enum.map(nicknames, &User.get_cached_by_nickname/1)
|
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(%{
|
ModerationLog.insert_log(%{
|
||||||
actor: admin,
|
actor: admin,
|
||||||
|
|
|
@ -150,7 +150,7 @@ def delete_account(%{assigns: %{user: user}} = conn, params) do
|
||||||
def disable_account(%{assigns: %{user: user}} = conn, params) do
|
def disable_account(%{assigns: %{user: user}} = conn, params) do
|
||||||
case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
|
case CommonAPI.Utils.confirm_current_password(user, params["password"]) do
|
||||||
{:ok, user} ->
|
{:ok, user} ->
|
||||||
User.deactivate_async(user)
|
User.set_activation_async(user, false)
|
||||||
json(conn, %{status: "success"})
|
json(conn, %{status: "success"})
|
||||||
|
|
||||||
{:error, msg} ->
|
{:error, msg} ->
|
||||||
|
|
|
@ -9,9 +9,9 @@ defmodule Pleroma.Workers.BackgroundWorker do
|
||||||
|
|
||||||
@impl Oban.Worker
|
@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 = User.get_cached_by_id(user_id)
|
||||||
User.perform(:deactivate_async, user, status)
|
User.perform(:set_activation_async, user, status)
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform(%Job{args: %{"op" => "delete_user", "user_id" => user_id}}) do
|
def perform(%Job{args: %{"op" => "delete_user", "user_id" => user_id}}) do
|
||||||
|
|
|
@ -1313,13 +1313,13 @@ test "has following" do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
describe ".deactivate" do
|
describe ".set_activation" do
|
||||||
test "can de-activate then re-activate a user" do
|
test "can de-activate then re-activate a user" do
|
||||||
user = insert(:user)
|
user = insert(:user)
|
||||||
assert user.is_active
|
assert user.is_active
|
||||||
{:ok, user} = User.deactivate(user)
|
{:ok, user} = User.set_activation(user, false)
|
||||||
refute user.is_active
|
refute user.is_active
|
||||||
{:ok, user} = User.deactivate(user, false)
|
{:ok, user} = User.set_activation(user, true)
|
||||||
assert user.is_active
|
assert user.is_active
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -1328,7 +1328,7 @@ test "hide a user from followers" do
|
||||||
user2 = insert(:user)
|
user2 = insert(:user)
|
||||||
|
|
||||||
{:ok, user, user2} = User.follow(user, user2)
|
{: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)
|
user2 = User.get_cached_by_id(user2.id)
|
||||||
|
|
||||||
|
@ -1344,7 +1344,7 @@ test "hide a user from friends" do
|
||||||
assert user2.following_count == 1
|
assert user2.following_count == 1
|
||||||
assert User.following_count(user2) == 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)
|
user2 = User.get_cached_by_id(user2.id)
|
||||||
|
|
||||||
|
@ -1374,7 +1374,7 @@ test "hide a user's statuses from timelines and notifications" do
|
||||||
user: user2
|
user: user2
|
||||||
})
|
})
|
||||||
|
|
||||||
{:ok, _user} = User.deactivate(user)
|
{:ok, _user} = User.set_activation(user, false)
|
||||||
|
|
||||||
assert [] == ActivityPub.fetch_public_activities(%{})
|
assert [] == ActivityPub.fetch_public_activities(%{})
|
||||||
assert [] == Pleroma.Notification.for_user(user2)
|
assert [] == Pleroma.Notification.for_user(user2)
|
||||||
|
|
|
@ -940,7 +940,7 @@ test "PATCH /api/pleroma/admin/users/:nickname/toggle_activation", %{admin: admi
|
||||||
assert json_response(conn, 200) ==
|
assert json_response(conn, 200) ==
|
||||||
user_response(
|
user_response(
|
||||||
user,
|
user,
|
||||||
%{"is_active" => user.is_active}
|
%{"is_active" => !user.is_active}
|
||||||
)
|
)
|
||||||
|
|
||||||
log_entry = Repo.one(ModerationLog)
|
log_entry = Repo.one(ModerationLog)
|
||||||
|
|
|
@ -57,7 +57,7 @@ test "get instance stats", %{conn: conn} do
|
||||||
user = insert(:user, %{local: true})
|
user = insert(:user, %{local: true})
|
||||||
|
|
||||||
user2 = 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@peer1.com"})
|
||||||
insert(:user, %{local: false, nickname: "u@peer2.com"})
|
insert(:user, %{local: false, nickname: "u@peer2.com"})
|
||||||
|
|
Loading…
Reference in a new issue