2020-02-18 07:57:00 +00:00
|
|
|
defmodule Mobilizon.Web.Email.Notification do
|
|
|
|
@moduledoc """
|
|
|
|
Handles emails sent about event notifications.
|
|
|
|
"""
|
2022-04-05 10:16:22 +00:00
|
|
|
use Phoenix.Swoosh, view: Mobilizon.Web.EmailView
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
import Mobilizon.Web.Gettext
|
|
|
|
|
2020-06-08 10:28:19 +00:00
|
|
|
alias Mobilizon.Events.{Event, Participant}
|
2020-02-18 07:57:00 +00:00
|
|
|
alias Mobilizon.Users.{Setting, User}
|
2021-07-27 17:47:54 +00:00
|
|
|
alias Mobilizon.Web.Email
|
2021-10-18 09:09:02 +00:00
|
|
|
alias Mobilizon.Web.JsonLD.ObjectView
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
@spec before_event_notification(String.t(), Participant.t(), String.t()) ::
|
2022-04-05 10:16:22 +00:00
|
|
|
Swoosh.Email.t()
|
2020-02-18 07:57:00 +00:00
|
|
|
def before_event_notification(
|
|
|
|
email,
|
|
|
|
%Participant{event: event, role: :participant} = participant,
|
|
|
|
locale \\ "en"
|
|
|
|
) do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"Don't forget to go to %{title}",
|
|
|
|
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(:before_event_notification, %{
|
|
|
|
locale: locale,
|
|
|
|
participant: participant,
|
|
|
|
subject: subject,
|
|
|
|
jsonLDMetadata: build_json_ld(participant)
|
|
|
|
})
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec on_day_notification(User.t(), list(Participant.t()), pos_integer(), String.t()) ::
|
2022-04-05 10:16:22 +00:00
|
|
|
Swoosh.Email.t()
|
2020-02-18 07:57:00 +00:00
|
|
|
def on_day_notification(
|
|
|
|
%User{email: email, settings: %Setting{timezone: timezone}},
|
|
|
|
participations,
|
|
|
|
total,
|
|
|
|
locale \\ "en"
|
|
|
|
) do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
participation = hd(participations)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
ngettext("One event planned today", "%{nb_events} events planned today", total,
|
|
|
|
nb_events: total
|
|
|
|
)
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:on_day_notification, %{
|
|
|
|
locale: locale,
|
|
|
|
participation: participation,
|
|
|
|
participations: participations,
|
|
|
|
subject: subject,
|
|
|
|
total: total,
|
|
|
|
timezone: timezone,
|
|
|
|
jsonLDMetadata: build_json_ld(participations)
|
|
|
|
})
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|
2020-06-05 08:12:08 +00:00
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec weekly_notification(User.t(), list(Participant.t()), pos_integer(), String.t()) ::
|
2022-04-05 10:16:22 +00:00
|
|
|
Swoosh.Email.t()
|
2020-06-05 08:12:08 +00:00
|
|
|
def weekly_notification(
|
|
|
|
%User{email: email, settings: %Setting{timezone: timezone}},
|
|
|
|
participations,
|
|
|
|
total,
|
|
|
|
locale \\ "en"
|
|
|
|
) do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
participation = hd(participations)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
ngettext("One event planned this week", "%{nb_events} events planned this week", total,
|
|
|
|
nb_events: total
|
|
|
|
)
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:notification_each_week, %{
|
|
|
|
locale: locale,
|
|
|
|
participation: participation,
|
|
|
|
participations: participations,
|
|
|
|
subject: subject,
|
|
|
|
total: total,
|
|
|
|
timezone: timezone,
|
|
|
|
jsonLDMetadata: build_json_ld(participations)
|
|
|
|
})
|
2020-06-05 08:12:08 +00:00
|
|
|
end
|
2020-06-08 10:28:19 +00:00
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
@spec pending_participation_notification(User.t(), Event.t(), pos_integer()) :: Swoosh.Email.t()
|
2020-06-08 10:28:19 +00:00
|
|
|
def pending_participation_notification(
|
2021-11-15 14:56:28 +00:00
|
|
|
%User{locale: locale, email: email, settings: %Setting{timezone: timezone}},
|
2020-06-08 10:28:19 +00:00
|
|
|
%Event{} = event,
|
|
|
|
total
|
|
|
|
) do
|
|
|
|
Gettext.put_locale(locale)
|
|
|
|
|
|
|
|
subject =
|
|
|
|
ngettext(
|
|
|
|
"One participation request for event %{title} to process",
|
|
|
|
"%{number_participation_requests} participation requests for event %{title} to process",
|
|
|
|
total,
|
|
|
|
number_participation_requests: total,
|
|
|
|
title: event.title
|
|
|
|
)
|
|
|
|
|
2022-04-05 10:16:22 +00:00
|
|
|
[to: email, subject: subject]
|
|
|
|
|> Email.base_email()
|
|
|
|
|> render_body(:pending_participation_notification, %{
|
|
|
|
locale: locale,
|
|
|
|
event: event,
|
|
|
|
subject: subject,
|
|
|
|
total: total,
|
|
|
|
timezone: timezone
|
|
|
|
})
|
2020-06-08 10:28:19 +00:00
|
|
|
end
|
2021-10-18 09:09:02 +00:00
|
|
|
|
|
|
|
@spec build_json_ld(Participant.t()) :: String.t()
|
|
|
|
defp build_json_ld(%Participant{} = participant) do
|
|
|
|
"participation.json"
|
|
|
|
|> ObjectView.render(%{participant: participant})
|
|
|
|
|> Jason.encode!()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp build_json_ld(participations) when is_list(participations) do
|
|
|
|
participations
|
|
|
|
|> Enum.map(&ObjectView.render("participation.json", %{participant: &1}))
|
|
|
|
|> Jason.encode!()
|
|
|
|
end
|
2020-02-18 07:57:00 +00:00
|
|
|
end
|