2020-11-26 14:48:10 +01:00
|
|
|
defmodule Mobilizon.Service.CleanUnconfirmedUsers do
|
|
|
|
@moduledoc """
|
|
|
|
Service to clean unconfirmed users
|
|
|
|
"""
|
|
|
|
|
|
|
|
alias Mobilizon.Federation.ActivityPub.Relay
|
2021-09-24 16:46:42 +02:00
|
|
|
alias Mobilizon.Service.ActorSuspension
|
2020-11-26 14:48:10 +01:00
|
|
|
alias Mobilizon.Storage.Repo
|
2021-09-24 16:46:42 +02:00
|
|
|
alias Mobilizon.Users
|
2020-11-26 14:48:10 +01:00
|
|
|
alias Mobilizon.Users.User
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Clean unattached media
|
|
|
|
|
|
|
|
Remove media that is not attached to an entity, such as media uploads that were never used in entities.
|
|
|
|
"""
|
2021-10-05 17:43:45 +02:00
|
|
|
@spec clean(Keyword.t()) :: {:ok, list(Media.t())}
|
2020-11-26 14:48:10 +01:00
|
|
|
def clean(opts \\ []) do
|
|
|
|
users_to_delete = find_unconfirmed_users_to_clean(opts)
|
|
|
|
|
|
|
|
if Keyword.get(opts, :dry_run, false) do
|
|
|
|
{:ok, users_to_delete}
|
|
|
|
else
|
|
|
|
users_to_delete = Enum.map(users_to_delete, &delete_user/1)
|
|
|
|
|
|
|
|
{:ok, users_to_delete}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 16:46:42 +02:00
|
|
|
@spec delete_user(User.t()) :: User.t() | {:error, Ecto.Changeset.t()} | no_return
|
2020-11-26 14:48:10 +01:00
|
|
|
defp delete_user(%User{} = user) do
|
2021-09-24 16:46:42 +02:00
|
|
|
actors = Users.get_actors_for_user(user)
|
|
|
|
%{id: actor_performing_id} = Relay.get_actor()
|
|
|
|
|
|
|
|
Enum.each(actors, fn actor ->
|
|
|
|
ActorSuspension.suspend_actor(actor,
|
|
|
|
author_id: actor_performing_id,
|
|
|
|
reserve_username: false
|
|
|
|
)
|
|
|
|
end)
|
|
|
|
|
|
|
|
case Users.delete_user(user, reserve_email: false) do
|
|
|
|
{:ok, %User{} = user} ->
|
|
|
|
%User{user | actors: actors}
|
2020-11-26 14:48:10 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec find_unconfirmed_users_to_clean(Keyword.t()) :: list(User.t())
|
|
|
|
defp find_unconfirmed_users_to_clean(opts) do
|
2021-06-07 10:24:57 +02:00
|
|
|
default_grace_period =
|
|
|
|
Mobilizon.Config.get([:instance, :unconfirmed_user_grace_period_hours], 48)
|
|
|
|
|
|
|
|
grace_period = Keyword.get(opts, :grace_period, default_grace_period)
|
2020-11-26 14:48:10 +01:00
|
|
|
expiration_date = DateTime.add(DateTime.utc_now(), grace_period * -3600)
|
|
|
|
|
|
|
|
User
|
|
|
|
|> where([u], is_nil(u.confirmed_at) and u.confirmation_sent_at < ^expiration_date)
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
end
|