Reset default actor id for an user when a profile is deleted

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-04-27 11:28:25 +02:00
parent e1cf7a817f
commit 493808a3c8
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773

View File

@ -333,28 +333,33 @@ defmodule Mobilizon.Actors do
|> Multi.run(:remove_avatar, fn _, _ -> remove_avatar(actor) end) |> Multi.run(:remove_avatar, fn _, _ -> remove_avatar(actor) end)
multi = multi =
if type == :Group do case type do
multi :Group ->
|> Multi.run(:delete_remote_members, fn _, _ -> multi
delete_group_elements(actor, :remote_members) |> Multi.run(:delete_remote_members, fn _, _ ->
end) delete_group_elements(actor, :remote_members)
|> Multi.run(:delete_group_organized_events, fn _, _ -> end)
delete_group_elements(actor, :events) |> Multi.run(:delete_group_organized_events, fn _, _ ->
end) delete_group_elements(actor, :events)
|> Multi.run(:delete_group_posts, fn _, _ -> end)
delete_group_elements(actor, :posts) |> Multi.run(:delete_group_posts, fn _, _ ->
end) delete_group_elements(actor, :posts)
|> Multi.run(:delete_group_resources, fn _, _ -> end)
delete_group_elements(actor, :resources) |> Multi.run(:delete_group_resources, fn _, _ ->
end) delete_group_elements(actor, :resources)
|> Multi.run(:delete_group_todo_lists, fn _, _ -> end)
delete_group_elements(actor, :todo_lists) |> Multi.run(:delete_group_todo_lists, fn _, _ ->
end) delete_group_elements(actor, :todo_lists)
|> Multi.run(:delete_group_discussions, fn _, _ -> end)
delete_group_elements(actor, :discussions) |> Multi.run(:delete_group_discussions, fn _, _ ->
end) delete_group_elements(actor, :discussions)
else end)
multi
:Person ->
# When deleting a profile, reset default_actor_id
Multi.run(multi, :reset_default_actor_id, fn _, _ ->
reset_default_actor_id(actor)
end)
end end
multi = multi =
@ -1825,6 +1830,24 @@ defmodule Mobilizon.Actors do
end end
end end
@spec reset_default_actor_id(Actor.t()) :: {:ok, User.t()} | {:error, :user_not_found}
defp reset_default_actor_id(%Actor{type: :Person, user: %User{id: user_id} = user, id: actor_id}) do
Logger.debug("reset_default_actor_id")
new_actor_id =
user
|> Users.get_actors_for_user()
|> Enum.map(& &1.id)
|> Enum.find(&(&1 !== actor_id))
{:ok, Users.update_user_default_actor(user_id, new_actor_id)}
rescue
_e in Ecto.NoResultsError ->
{:error, :user_not_found}
end
defp reset_default_actor_id(%Actor{type: :Person, user: nil}), do: {:ok, nil}
defp accumulate_paginated_elements( defp accumulate_paginated_elements(
%Actor{} = actor, %Actor{} = actor,
method, method,