Various refactoring and typespec improvements

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-09-24 16:46:42 +02:00
parent d235653876
commit 1893d9f55b
142 changed files with 1854 additions and 1297 deletions

60
lib/web/email/actor.ex Normal file
View File

@@ -0,0 +1,60 @@
defmodule Mobilizon.Web.Email.Actor do
@moduledoc """
Handles emails sent about actors status.
"""
use Bamboo.Phoenix, view: Mobilizon.Web.EmailView
import Bamboo.Phoenix
import Mobilizon.Web.Gettext
alias Mobilizon.Actors.Actor
alias Mobilizon.{Config, Users}
alias Mobilizon.Events.{Event, Participant}
alias Mobilizon.Users.User
alias Mobilizon.Web.Email
@doc """
Send a notification to participants from events organized by an actor that is going to be suspended
"""
@spec send_notification_event_participants_from_suspension(Participant.t(), Actor.t()) ::
:ok
def send_notification_event_participants_from_suspension(
%Participant{
actor: %Actor{user_id: nil}
},
_suspended
),
do: :ok
def send_notification_event_participants_from_suspension(%Participant{role: role}, _suspended)
when role not in [:participant, :moderator, :administrator],
do: :ok
def send_notification_event_participants_from_suspension(
%Participant{
actor: %Actor{user_id: user_id},
event: %Event{} = event,
role: member_role
},
%Actor{} = suspended
) do
with %User{email: email, locale: locale} <- Users.get_user!(user_id) do
Gettext.put_locale(locale)
instance = Config.instance_name()
subject = gettext("Your participation to %{event} has been cancelled!", event: event.title)
Email.base_email(to: email, subject: subject)
|> assign(:locale, locale)
|> assign(:actor, suspended)
|> assign(:event, event)
|> assign(:role, member_role)
|> assign(:subject, subject)
|> assign(:instance, instance)
|> render(:actor_suspension_participants)
|> Email.Mailer.send_email_later()
:ok
end
end
end

View File

@@ -79,6 +79,7 @@ defmodule Mobilizon.Web.Email.Group do
# TODO : def send_confirmation_to_inviter()
@member_roles [:administrator, :moderator, :member]
@spec send_group_suspension_notification(Member.t()) :: :ok
def send_group_suspension_notification(%Member{actor: %Actor{user_id: nil}}), do: :ok
def send_group_suspension_notification(%Member{role: role}) when role not in @member_roles,
@@ -112,64 +113,4 @@ defmodule Mobilizon.Web.Email.Group do
:ok
end
end
def send_group_deletion_notification(%Member{actor: %Actor{user_id: nil}}, _author), do: :ok
def send_group_deletion_notification(%Member{role: role}, _author)
when role not in @member_roles,
do: :ok
@spec send_group_deletion_notification(Member.t(), Actor.t()) :: :ok
def send_group_deletion_notification(
%Member{
actor: %Actor{user_id: user_id, id: actor_id} = member
},
%Actor{id: author_id} = author
) do
with %User{email: email, locale: locale} <- Users.get_user!(user_id),
{:member_not_author, true} <- {:member_not_author, author_id !== actor_id} do
do_send_group_deletion_notification(member, author: author, email: email, locale: locale)
else
# Skip if it's the author itself
{:member_not_author, _} ->
:ok
end
end
@spec send_group_deletion_notification(Member.t()) :: :ok
def send_group_deletion_notification(%Member{actor: %Actor{user_id: user_id}} = member) do
case Users.get_user!(user_id) do
%User{email: email, locale: locale} ->
do_send_group_deletion_notification(member, email: email, locale: locale)
end
end
defp do_send_group_deletion_notification(
%Member{role: member_role, parent: %Actor{domain: nil} = group},
options
) do
locale = Keyword.get(options, :locale)
Gettext.put_locale(locale)
instance = Config.instance_name()
author = Keyword.get(options, :author)
subject =
gettext(
"The group %{group} has been deleted on %{instance}",
group: group.name,
instance: instance
)
Email.base_email(to: Keyword.get(options, :email), subject: subject)
|> assign(:locale, locale)
|> assign(:group, group)
|> assign(:role, member_role)
|> assign(:subject, subject)
|> assign(:instance, instance)
|> assign(:author, author)
|> render(:group_deletion)
|> Email.Mailer.send_email_later()
:ok
end
end

View File

@@ -199,14 +199,14 @@ defmodule Mobilizon.Web.Email.User do
:ok
_ ->
case Timex.before?(
Timex.shift(Map.get(user, key), hours: 1),
case DateTime.compare(
DateTime.add(Map.get(user, key), 3600),
DateTime.utc_now() |> DateTime.truncate(:second)
) do
true ->
:lt ->
:ok
false ->
_ ->
{:error, :email_too_soon}
end
end