From b28ed9d10f607ccd7532905a7069750301818f15 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 13 Oct 2020 15:07:51 +0200 Subject: [PATCH] Completely delete user when user request self destruction Signed-off-by: Thomas Citharel --- lib/graphql/resolvers/user.ex | 20 +++++++++----------- test/graphql/resolvers/user_test.exs | 2 +- 2 files changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/graphql/resolvers/user.ex b/lib/graphql/resolvers/user.ex index 890ea2b68..fef857307 100644 --- a/lib/graphql/resolvers/user.ex +++ b/lib/graphql/resolvers/user.ex @@ -410,7 +410,7 @@ defmodule Mobilizon.GraphQL.Resolvers.User do {:moderator_actor, Users.get_actor_for_user(moderator_user)}, %User{disabled: false} = user <- Users.get_user(user_id), {:ok, %User{}} <- - do_delete_account(%User{} = user, Relay.get_actor()) do + do_delete_account(%User{} = user, actor_performing: Relay.get_actor()) do Admin.log_action(moderator_actor, "delete", user) else {:moderator_actor, nil} -> @@ -429,11 +429,11 @@ defmodule Mobilizon.GraphQL.Resolvers.User do {:confirmation_password, Map.get(args, :password)}, {:current_password, {:ok, _}} <- {:current_password, Authenticator.authenticate(email, password)} do - do_delete_account(user) + do_delete_account(user, reserve_email: false) else # If the user hasn't got any password (3rd-party auth) {:user_has_password, false} -> - do_delete_account(user) + do_delete_account(user, reserve_email: false) {:confirmation_password, nil} -> {:error, dgettext("errors", "The password provided is invalid")} @@ -447,23 +447,21 @@ defmodule Mobilizon.GraphQL.Resolvers.User do {:error, dgettext("errors", "You need to be logged-in to delete your account")} end - defp do_delete_account(%User{} = user, actor_performing \\ nil) do + @spec do_delete_account(User.t(), Keyword.t()) :: {:ok, User.t()} + defp do_delete_account(%User{} = user, options) do with actors <- Users.get_actors_for_user(user), activated <- not is_nil(user.confirmed_at), # Detach actors from user - :ok <- - if(activated, - do: :ok, - else: Enum.each(actors, fn actor -> Actors.update_actor(actor, %{user_id: nil}) end) - ), + :ok <- Enum.each(actors, fn actor -> Actors.update_actor(actor, %{user_id: nil}) end), # Launch a background job to delete actors :ok <- Enum.each(actors, fn actor -> - actor_performing = actor_performing || actor + actor_performing = Keyword.get(options, :actor_performing, actor) ActivityPub.delete(actor, actor_performing, true) end), # Delete user - {:ok, user} <- Users.delete_user(user, reserve_email: activated) do + {:ok, user} <- + Users.delete_user(user, reserve_email: Keyword.get(options, :reserve_email, activated)) do {:ok, user} end end diff --git a/test/graphql/resolvers/user_test.exs b/test/graphql/resolvers/user_test.exs index 13650c532..d989a4b42 100644 --- a/test/graphql/resolvers/user_test.exs +++ b/test/graphql/resolvers/user_test.exs @@ -1379,7 +1379,7 @@ defmodule Mobilizon.GraphQL.Resolvers.UserTest do assert MapSet.new([actor1.id, actor2.id]) == MapSet.new([actor1_id, actor2_id]) - assert Users.get_user(user.id).disabled == true + assert is_nil(Users.get_user(user.id)) assert %{success: 2, failure: 0} == Oban.drain_queue(queue: :background)