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
|
2020-09-30 10:44:01 +02:00
|
|
|
alias Mobilizon.Events.{Event, 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 """
|
2020-11-06 15:43:38 +01:00
|
|
|
Send participation emails to local user
|
|
|
|
|
|
|
|
If the actor is anonymous, use information in metadata
|
2019-09-30 13:48:47 +02:00
|
|
|
"""
|
|
|
|
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
|
2020-06-09 10:07:30 +02:00
|
|
|
%{email: email, locale: locale} = Map.get(participation, :metadata)
|
|
|
|
locale = locale || "en"
|
2019-12-20 13:04:34 +01:00
|
|
|
|
|
|
|
email
|
2020-06-09 10:07:30 +02:00
|
|
|
|> participation_updated(participation, locale)
|
2019-12-20 13:04:34 +01:00
|
|
|
|> Email.Mailer.deliver_later()
|
|
|
|
end
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
2019-09-30 13:48:47 +02:00
|
|
|
|
|
|
|
def send_emails_to_local_user(
|
|
|
|
%Participant{actor: %Actor{user_id: user_id} = _actor} = participation
|
|
|
|
) do
|
2020-06-09 10:07:30 +02:00
|
|
|
with %User{locale: locale} = user <- Users.get_user!(user_id) do
|
2019-09-30 13:48:47 +02:00
|
|
|
user
|
2020-06-09 10:07:30 +02:00
|
|
|
|> participation_updated(participation, locale)
|
2019-09-30 13:48:47 +02:00
|
|
|
|> 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
|
|
|
|
|
2020-09-30 10:44:01 +02:00
|
|
|
@spec participation_updated(String.t(), Participant.t(), String.t()) :: Bamboo.Email.t()
|
|
|
|
def participation_updated(
|
|
|
|
email,
|
|
|
|
%Participant{event: %Event{join_options: :free} = event, role: :participant},
|
|
|
|
locale
|
|
|
|
) do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"Your participation to event %{title} has been confirmed",
|
|
|
|
title: event.title
|
|
|
|
)
|
|
|
|
|
|
|
|
Email.base_email(to: email, subject: subject)
|
|
|
|
|> assign(:locale, locale)
|
|
|
|
|> assign(:event, event)
|
|
|
|
|> assign(:subject, subject)
|
|
|
|
|> render(:event_participation_confirmed)
|
|
|
|
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
|