2020-01-26 21:36:50 +01:00
|
|
|
defmodule Mobilizon.Web.Email.Participation do
|
2019-09-30 13:48:47 +02:00
|
|
|
@moduledoc """
|
|
|
|
Handles emails sent about participation.
|
|
|
|
"""
|
2020-01-26 21:36:50 +01:00
|
|
|
use Bamboo.Phoenix, view: Mobilizon.Web.EmailView
|
2019-09-30 13:48:47 +02:00
|
|
|
|
|
|
|
import Bamboo.Phoenix
|
2020-01-26 21:36:50 +01:00
|
|
|
import Mobilizon.Web.Gettext
|
2019-09-30 13:48:47 +02:00
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
2019-12-20 13:04:34 +01:00
|
|
|
alias Mobilizon.Config
|
2019-09-30 13:48:47 +02:00
|
|
|
alias Mobilizon.Events.Participant
|
2019-12-20 13:04:34 +01:00
|
|
|
alias Mobilizon.Users
|
2020-01-28 20:15:59 +01:00
|
|
|
alias Mobilizon.Users.User
|
2020-01-28 19:18:33 +01:00
|
|
|
alias Mobilizon.Web.{Email, Gettext}
|
2019-09-30 13:48:47 +02:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Send emails to local user
|
|
|
|
"""
|
|
|
|
def send_emails_to_local_user(
|
2019-12-20 13:04:34 +01:00
|
|
|
%Participant{actor: %Actor{user_id: nil, id: actor_id} = _actor} = participation
|
|
|
|
) do
|
|
|
|
if actor_id == Config.anonymous_actor_id() do
|
|
|
|
%{email: email} = Map.get(participation, :metadata)
|
|
|
|
|
|
|
|
email
|
|
|
|
|> participation_updated(participation)
|
|
|
|
|> Email.Mailer.deliver_later()
|
|
|
|
end
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
2019-09-30 13:48:47 +02:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Send emails to local user
|
|
|
|
"""
|
|
|
|
def send_emails_to_local_user(
|
|
|
|
%Participant{actor: %Actor{user_id: user_id} = _actor} = participation
|
|
|
|
) do
|
2019-12-20 13:04:34 +01:00
|
|
|
with %User{} = user <- Users.get_user!(user_id) do
|
2019-09-30 13:48:47 +02:00
|
|
|
user
|
|
|
|
|> participation_updated(participation)
|
|
|
|
|> Email.Mailer.deliver_later()
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
@spec participation_updated(String.t() | User.t(), Participant.t(), String.t()) ::
|
|
|
|
Bamboo.Email.t()
|
2019-09-30 13:48:47 +02:00
|
|
|
def participation_updated(user, participant, locale \\ "en")
|
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
@spec participation_updated(User.t(), Participant.t(), String.t()) :: Bamboo.Email.t()
|
2019-09-30 13:48:47 +02:00
|
|
|
def participation_updated(
|
|
|
|
%User{email: email},
|
2019-12-20 13:04:34 +01:00
|
|
|
%Participant{} = participant,
|
|
|
|
locale
|
|
|
|
),
|
|
|
|
do: participation_updated(email, participant, locale)
|
|
|
|
|
|
|
|
@spec participation_updated(String.t(), Participant.t(), String.t()) :: Bamboo.Email.t()
|
|
|
|
def participation_updated(
|
|
|
|
email,
|
2019-09-30 13:48:47 +02:00
|
|
|
%Participant{event: event, role: :rejected},
|
|
|
|
locale
|
|
|
|
) do
|
2020-01-28 19:18:33 +01:00
|
|
|
Gettext.put_locale(locale)
|
2019-09-30 13:48:47 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2019-12-20 13:04:34 +01:00
|
|
|
@spec participation_updated(String.t(), Participant.t(), String.t()) :: Bamboo.Email.t()
|
2019-09-30 13:48:47 +02:00
|
|
|
def participation_updated(
|
2019-12-20 13:04:34 +01:00
|
|
|
email,
|
2019-09-30 13:48:47 +02:00
|
|
|
%Participant{event: event, role: :participant},
|
|
|
|
locale
|
|
|
|
) do
|
2020-01-28 19:18:33 +01:00
|
|
|
Gettext.put_locale(locale)
|
2019-09-30 13:48:47 +02:00
|
|
|
|
|
|
|
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
|
2019-12-20 13:04:34 +01:00
|
|
|
|
|
|
|
@spec anonymous_participation_confirmation(String.t(), Participant.t(), String.t()) ::
|
|
|
|
Bamboo.Email.t()
|
|
|
|
def anonymous_participation_confirmation(
|
|
|
|
email,
|
|
|
|
%Participant{event: event, role: :not_confirmed} = participant,
|
|
|
|
locale \\ "en"
|
|
|
|
) do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"Confirm your participation to event %{title}",
|
|
|
|
title: event.title
|
|
|
|
)
|
|
|
|
|
|
|
|
Email.base_email(to: email, subject: subject)
|
|
|
|
|> assign(:locale, locale)
|
|
|
|
|> assign(:participant, participant)
|
|
|
|
|> assign(:subject, subject)
|
|
|
|
|> render(:anonymous_participation_confirmation)
|
|
|
|
end
|
2019-09-30 13:48:47 +02:00
|
|
|
end
|