2020-01-26 20:36:50 +00:00
|
|
|
defmodule Mobilizon.Web.Email.Participation do
|
2019-09-30 11:48:47 +00:00
|
|
|
@moduledoc """
|
|
|
|
Handles emails sent about participation.
|
|
|
|
"""
|
2022-04-05 10:16:22 +00:00
|
|
|
use Phoenix.Swoosh, view: Mobilizon.Web.EmailView
|
2019-09-30 11:48:47 +00:00
|
|
|
|
2020-01-26 20:36:50 +00:00
|
|
|
import Mobilizon.Web.Gettext
|
2019-09-30 11:48:47 +00:00
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
2021-10-13 10:57:54 +00:00
|
|
|
alias Mobilizon.{Config, Events, Users}
|
2020-09-30 08:44:01 +00:00
|
|
|
alias Mobilizon.Events.{Event, Participant}
|
2020-01-28 19:15:59 +00:00
|
|
|
alias Mobilizon.Users.User
|
2021-07-27 17:47:54 +00:00
|
|
|
alias Mobilizon.Web.Email
|
2021-10-13 10:57:54 +00:00
|
|
|
alias Mobilizon.Web.JsonLD.ObjectView
|
2019-09-30 11:48:47 +00:00
|
|
|
|
|
|
|
@doc """
|
2020-11-06 14:43:38 +00:00
|
|
|
Send participation emails to local user
|
|
|
|
|
|
|
|
If the actor is anonymous, use information in metadata
|
2019-09-30 11:48:47 +00:00
|
|
|
"""
|
|
|
|
def send_emails_to_local_user(
|
2019-12-20 12:04:34 +00:00
|
|
|
%Participant{actor: %Actor{user_id: nil, id: actor_id} = _actor} = participation
|
|
|
|
) do
|
|
|
|
if actor_id == Config.anonymous_actor_id() do
|
2020-06-09 08:07:30 +00:00
|
|
|
%{email: email, locale: locale} = Map.get(participation, :metadata)
|
|
|
|
locale = locale || "en"
|
2019-12-20 12:04:34 +00:00
|
|
|
|
|
|
|
email
|
2020-06-09 08:07:30 +00:00
|
|
|
|> participation_updated(participation, locale)
|
2022-04-05 10:16:22 +00:00
|
|
|
|> Email.Mailer.send_email()
|
2019-12-20 12:04:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
2019-09-30 11:48:47 +00:00
|
|
|
|
|
|
|
def send_emails_to_local_user(
|
|
|
|
%Participant{actor: %Actor{user_id: user_id} = _actor} = participation
|
|
|
|
) do
|
2020-06-09 08:07:30 +00:00
|
|
|
with %User{locale: locale} = user <- Users.get_user!(user_id) do
|
2019-09-30 11:48:47 +00:00
|
|
|
user
|
2020-06-09 08:07:30 +00:00
|
|
|
|> participation_updated(participation, locale)
|
2022-04-05 10:16:22 +00:00
|
|
|
|> Email.Mailer.send_email()
|
2019-09-30 11:48:47 +00:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-20 12:04:34 +00:00
|
|
|
@spec participation_updated(String.t() | User.t(), Participant.t(), String.t()) ::
|
2022-04-05 10:16:22 +00:00
|
|
|
Swoosh.Email.t()
|
2019-09-30 11:48:47 +00:00
|
|
|
def participation_updated(user, participant, locale \\ "en")
|
|
|
|
|
|
|
|
def participation_updated(
|
|
|
|
%User{email: email},
|
2019-12-20 12:04:34 +00:00
|
|
|
%Participant{} = participant,
|
|
|
|
locale
|
|
|
|
),
|
|
|
|
do: participation_updated(email, participant, locale)
|
|
|
|
|
|
|
|
def participation_updated(
|
|
|
|
email,
|
2021-10-13 10:55:49 +00:00
|
|
|
%Participant{event: event, role: :rejected} = participant,
|
2019-09-30 11:48:47 +00:00
|
|
|
locale
|
|
|
|
) do
|
2020-01-28 18:18:33 +00:00
|
|
|
Gettext.put_locale(locale)
|
2019-09-30 11:48:47 +00:00
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"Your participation to event %{title} has been rejected",
|
|
|
|
title: event.title
|
|
|
|
)
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:event_participation_rejected, %{
|
|
|
|
locale: locale,
|
|
|
|
event: event,
|
|
|
|
jsonLDMetadata: json_ld(participant),
|
|
|
|
subject: subject
|
|
|
|
})
|
2019-09-30 11:48:47 +00:00
|
|
|
end
|
|
|
|
|
2020-09-30 08:44:01 +00:00
|
|
|
def participation_updated(
|
|
|
|
email,
|
2021-10-13 10:55:49 +00:00
|
|
|
%Participant{event: %Event{join_options: :free} = event, role: :participant} =
|
|
|
|
participant,
|
2020-09-30 08:44:01 +00:00
|
|
|
locale
|
|
|
|
) do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"Your participation to event %{title} has been confirmed",
|
|
|
|
title: event.title
|
|
|
|
)
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:event_participation_confirmed, %{
|
|
|
|
locale: locale,
|
|
|
|
event: event,
|
|
|
|
jsonLDMetadata: json_ld(participant),
|
|
|
|
subject: subject
|
|
|
|
})
|
2020-09-30 08:44:01 +00:00
|
|
|
end
|
|
|
|
|
2019-09-30 11:48:47 +00:00
|
|
|
def participation_updated(
|
2019-12-20 12:04:34 +00:00
|
|
|
email,
|
2021-10-13 10:55:49 +00:00
|
|
|
%Participant{event: event, role: :participant} = participant,
|
2019-09-30 11:48:47 +00:00
|
|
|
locale
|
|
|
|
) do
|
2020-01-28 18:18:33 +00:00
|
|
|
Gettext.put_locale(locale)
|
2019-09-30 11:48:47 +00:00
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"Your participation to event %{title} has been approved",
|
|
|
|
title: event.title
|
|
|
|
)
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
2020-12-21 14:47:26 +00:00
|
|
|
|> Email.add_event_attachment(event)
|
2022-04-05 10:16:22 +00:00
|
|
|
|> render_body(:event_participation_approved, %{
|
|
|
|
locale: locale,
|
|
|
|
event: event,
|
|
|
|
jsonLDMetadata: json_ld(participant),
|
|
|
|
subject: subject
|
|
|
|
})
|
2019-09-30 11:48:47 +00:00
|
|
|
end
|
2019-12-20 12:04:34 +00:00
|
|
|
|
|
|
|
@spec anonymous_participation_confirmation(String.t(), Participant.t(), String.t()) ::
|
2022-04-05 10:16:22 +00:00
|
|
|
Swoosh.Email.t()
|
2019-12-20 12:04:34 +00:00
|
|
|
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
|
|
|
|
)
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:anonymous_participation_confirmation, %{
|
|
|
|
locale: locale,
|
|
|
|
event: event,
|
|
|
|
jsonLDMetadata: json_ld(participant),
|
|
|
|
participant: participant,
|
|
|
|
subject: subject
|
|
|
|
})
|
2019-12-20 12:04:34 +00:00
|
|
|
end
|
2021-10-13 10:57:54 +00:00
|
|
|
|
|
|
|
defp json_ld(participant) do
|
|
|
|
event = Events.get_event_with_preload!(participant.event_id)
|
|
|
|
|
|
|
|
"participation.json"
|
|
|
|
|> ObjectView.render(%{participant: %Participant{participant | event: event}})
|
|
|
|
|> Jason.encode!()
|
|
|
|
end
|
2019-09-30 11:48:47 +00:00
|
|
|
end
|