2018-11-06 10:30:27 +01:00
|
|
|
defmodule Mobilizon.Actors.Service.Tools do
|
|
|
|
alias Mobilizon.Actors.User
|
|
|
|
|
2018-11-12 18:17:53 +01:00
|
|
|
@spec we_can_send_email(User.t(), atom()) :: :ok | {:error, :email_too_soon}
|
2018-11-06 10:30:27 +01:00
|
|
|
def we_can_send_email(%User{} = user, key \\ :reset_password_sent_at) do
|
|
|
|
case Map.get(user, key) do
|
|
|
|
nil ->
|
|
|
|
:ok
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
case Timex.before?(Timex.shift(Map.get(user, key), hours: 1), DateTime.utc_now()) do
|
|
|
|
true ->
|
|
|
|
:ok
|
|
|
|
|
|
|
|
false ->
|
|
|
|
{:error, :email_too_soon}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec random_string(integer) :: String.t()
|
|
|
|
def random_string(length) do
|
|
|
|
length
|
|
|
|
|> :crypto.strong_rand_bytes()
|
|
|
|
|> Base.url_encode64()
|
|
|
|
end
|
|
|
|
end
|