Merge branch 'feature/1734-user-deletion' into 'develop'
User deletion Closes #1734 See merge request pleroma/pleroma!2493
This commit is contained in:
commit
423ea497bb
@ -1432,6 +1432,25 @@ defmodule Pleroma.User do
|
|||||||
BackgroundWorker.enqueue("delete_user", %{"user_id" => user.id})
|
BackgroundWorker.enqueue("delete_user", %{"user_id" => user.id})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
defp delete_and_invalidate_cache(%User{} = user) do
|
||||||
|
invalidate_cache(user)
|
||||||
|
Repo.delete(user)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp delete_or_deactivate(%User{local: false} = user), do: delete_and_invalidate_cache(user)
|
||||||
|
|
||||||
|
defp delete_or_deactivate(%User{local: true} = user) do
|
||||||
|
status = account_status(user)
|
||||||
|
|
||||||
|
if status == :confirmation_pending do
|
||||||
|
delete_and_invalidate_cache(user)
|
||||||
|
else
|
||||||
|
user
|
||||||
|
|> change(%{deactivated: true, email: nil})
|
||||||
|
|> update_and_set_cache()
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def perform(:force_password_reset, user), do: force_password_reset(user)
|
def perform(:force_password_reset, user), do: force_password_reset(user)
|
||||||
|
|
||||||
@spec perform(atom(), User.t()) :: {:ok, User.t()}
|
@spec perform(atom(), User.t()) :: {:ok, User.t()}
|
||||||
@ -1453,14 +1472,7 @@ defmodule Pleroma.User do
|
|||||||
|
|
||||||
delete_user_activities(user)
|
delete_user_activities(user)
|
||||||
|
|
||||||
if user.local do
|
delete_or_deactivate(user)
|
||||||
user
|
|
||||||
|> change(%{deactivated: true, email: nil})
|
|
||||||
|> update_and_set_cache()
|
|
||||||
else
|
|
||||||
invalidate_cache(user)
|
|
||||||
Repo.delete(user)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def perform(:deactivate_async, user, status), do: deactivate(user, status)
|
def perform(:deactivate_async, user, status), do: deactivate(user, status)
|
||||||
|
@ -1172,6 +1172,33 @@ defmodule Pleroma.UserTest do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "delete/1 when confirmation is pending" do
|
||||||
|
setup do
|
||||||
|
user = insert(:user, confirmation_pending: true)
|
||||||
|
{:ok, user: user}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "deletes user from database when activation required", %{user: user} do
|
||||||
|
clear_config([:instance, :account_activation_required], true)
|
||||||
|
|
||||||
|
{:ok, job} = User.delete(user)
|
||||||
|
{:ok, _} = ObanHelpers.perform(job)
|
||||||
|
|
||||||
|
refute User.get_cached_by_id(user.id)
|
||||||
|
refute User.get_by_id(user.id)
|
||||||
|
end
|
||||||
|
|
||||||
|
test "deactivates user when activation is not required", %{user: user} do
|
||||||
|
clear_config([:instance, :account_activation_required], false)
|
||||||
|
|
||||||
|
{:ok, job} = User.delete(user)
|
||||||
|
{:ok, _} = ObanHelpers.perform(job)
|
||||||
|
|
||||||
|
assert %{deactivated: true} = User.get_cached_by_id(user.id)
|
||||||
|
assert %{deactivated: true} = User.get_by_id(user.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
test "get_public_key_for_ap_id fetches a user that's not in the db" do
|
test "get_public_key_for_ap_id fetches a user that's not in the db" do
|
||||||
assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
|
assert {:ok, _key} = User.get_public_key_for_ap_id("http://mastodon.example.org/users/admin")
|
||||||
end
|
end
|
||||||
|
@ -140,6 +140,31 @@ defmodule Pleroma.Web.ActivityPub.SideEffectsTest do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "delete users with confirmation pending" do
|
||||||
|
setup do
|
||||||
|
user = insert(:user, confirmation_pending: true)
|
||||||
|
{:ok, delete_user_data, _meta} = Builder.delete(user, user.ap_id)
|
||||||
|
{:ok, delete_user, _meta} = ActivityPub.persist(delete_user_data, local: true)
|
||||||
|
{:ok, delete: delete_user, user: user}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "when activation is not required", %{delete: delete, user: user} do
|
||||||
|
clear_config([:instance, :account_activation_required], false)
|
||||||
|
{:ok, _, _} = SideEffects.handle(delete)
|
||||||
|
ObanHelpers.perform_all()
|
||||||
|
|
||||||
|
assert User.get_cached_by_id(user.id).deactivated
|
||||||
|
end
|
||||||
|
|
||||||
|
test "when activation is required", %{delete: delete, user: user} do
|
||||||
|
clear_config([:instance, :account_activation_required], true)
|
||||||
|
{:ok, _, _} = SideEffects.handle(delete)
|
||||||
|
ObanHelpers.perform_all()
|
||||||
|
|
||||||
|
refute User.get_cached_by_id(user.id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "Undo objects" do
|
describe "Undo objects" do
|
||||||
setup do
|
setup do
|
||||||
poster = insert(:user)
|
poster = insert(:user)
|
||||||
|
Loading…
Reference in New Issue
Block a user