2019-03-14 15:00:34 +01:00
|
|
|
defmodule Mobilizon.Service.Users.Activation do
|
2018-07-04 14:29:17 +02:00
|
|
|
@moduledoc false
|
|
|
|
|
2019-03-05 17:23:05 +01:00
|
|
|
alias Mobilizon.{Mailer, Users}
|
|
|
|
alias Mobilizon.Users.User
|
2018-10-11 17:37:39 +02:00
|
|
|
alias Mobilizon.Email.User, as: UserEmail
|
2019-03-14 15:00:34 +01:00
|
|
|
alias Mobilizon.Service.Users.Tools
|
2018-07-04 14:29:17 +02:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def check_confirmation_token(token) when is_binary(token) do
|
2019-03-05 17:23:05 +01:00
|
|
|
with %User{} = user <- Users.get_user_by_activation_token(token),
|
2018-07-27 10:45:35 +02:00
|
|
|
{:ok, %User{} = user} <-
|
2019-03-05 17:23:05 +01:00
|
|
|
Users.update_user(user, %{
|
2019-02-14 14:19:55 +01:00
|
|
|
"confirmed_at" => DateTime.utc_now() |> DateTime.truncate(:second),
|
2018-07-27 10:45:35 +02:00
|
|
|
"confirmation_sent_at" => nil,
|
|
|
|
"confirmation_token" => nil
|
|
|
|
}) do
|
2018-11-06 10:30:27 +01:00
|
|
|
Logger.info("User #{user.email} has been confirmed")
|
|
|
|
{:ok, user}
|
2018-07-04 14:29:17 +02:00
|
|
|
else
|
|
|
|
_err ->
|
2018-11-28 17:16:23 +01:00
|
|
|
{:error, :invalid_token}
|
2018-07-04 14:29:17 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def resend_confirmation_email(%User{} = user, locale \\ "en") do
|
2018-11-06 10:30:27 +01:00
|
|
|
with :ok <- Tools.we_can_send_email(user, :confirmation_sent_at),
|
2019-02-14 14:19:55 +01:00
|
|
|
{:ok, user} <-
|
2019-03-05 17:23:05 +01:00
|
|
|
Users.update_user(user, %{
|
2019-02-14 14:19:55 +01:00
|
|
|
"confirmation_sent_at" => DateTime.utc_now() |> DateTime.truncate(:second)
|
|
|
|
}) do
|
2018-11-06 10:30:27 +01:00
|
|
|
send_confirmation_email(user, locale)
|
|
|
|
Logger.info("Sent confirmation email again to #{user.email}")
|
|
|
|
{:ok, user.email}
|
|
|
|
end
|
2018-07-04 14:29:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def send_confirmation_email(%User{} = user, locale \\ "en") do
|
|
|
|
user
|
|
|
|
|> UserEmail.confirmation_email(locale)
|
|
|
|
|> Mailer.deliver_later()
|
|
|
|
end
|
|
|
|
end
|