debianize-mobilizon/lib/web/email/participation.ex

85 lines
2.1 KiB
Elixir
Raw Normal View History

2020-01-26 21:36:50 +01:00
defmodule Mobilizon.Web.Email.Participation do
@moduledoc """
Handles emails sent about participation.
"""
2020-01-26 21:36:50 +01:00
use Bamboo.Phoenix, view: Mobilizon.Web.EmailView
import Bamboo.Phoenix
2020-01-26 21:36:50 +01:00
import Mobilizon.Web.Gettext
alias Mobilizon.Actors.Actor
alias Mobilizon.Events.Participant
2020-01-28 20:15:59 +01:00
alias Mobilizon.Users.User
alias Mobilizon.Web.{Email, Gettext}
@doc """
Send emails to local user
"""
def send_emails_to_local_user(
%Participant{actor: %Actor{user_id: nil} = _actor} = _participation
),
do: :ok
@doc """
Send emails to local user
"""
def send_emails_to_local_user(
%Participant{actor: %Actor{user_id: user_id} = _actor} = participation
) do
with %User{} = user <- Mobilizon.Users.get_user!(user_id) do
user
|> participation_updated(participation)
|> Email.Mailer.deliver_later()
:ok
end
end
@spec participation_updated(User.t(), Participant.t(), String.t()) :: Bamboo.Email.t()
def participation_updated(user, participant, locale \\ "en")
def participation_updated(
%User{email: email},
%Participant{event: event, role: :rejected},
locale
) do
Gettext.put_locale(locale)
subject =
gettext(
"Your participation to event %{title} has been rejected",
title: event.title
)
Email.base_email(to: email, subject: subject)
|> assign(:locale, locale)
|> assign(:event, event)
|> assign(:subject, subject)
|> render(:event_participation_rejected)
end
@spec participation_updated(User.t(), Participant.t(), String.t()) :: Bamboo.Email.t()
def participation_updated(
%User{email: email},
%Participant{event: event, role: :participant},
locale
) do
Gettext.put_locale(locale)
subject =
gettext(
"Your participation to event %{title} has been approved",
title: event.title
)
Email.base_email(to: email, subject: subject)
|> assign(:locale, locale)
|> assign(:event, event)
|> assign(:subject, subject)
|> render(:event_participation_approved)
end
end