2020-01-26 20:36:50 +00:00
|
|
|
defmodule Mobilizon.Web.Email.Event do
|
2019-09-30 16:18:04 +00:00
|
|
|
@moduledoc """
|
|
|
|
Handles emails sent about events.
|
|
|
|
"""
|
|
|
|
|
2020-01-26 20:36:50 +00:00
|
|
|
use Bamboo.Phoenix, view: Mobilizon.Web.EmailView
|
2019-09-30 16:18:04 +00:00
|
|
|
|
|
|
|
import Bamboo.Phoenix
|
|
|
|
|
2020-01-26 20:36:50 +00:00
|
|
|
import Mobilizon.Web.Gettext
|
2019-09-30 16:18:04 +00:00
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
2020-01-22 23:55:07 +00:00
|
|
|
alias Mobilizon.Events
|
2020-10-08 06:53:25 +00:00
|
|
|
alias Mobilizon.Events.{Event, Participant}
|
2020-01-22 23:55:07 +00:00
|
|
|
alias Mobilizon.Storage.Repo
|
2020-11-16 17:28:52 +00:00
|
|
|
alias Mobilizon.Users.{Setting, User}
|
2020-10-08 06:53:25 +00:00
|
|
|
alias Mobilizon.Web.Email
|
2021-10-13 10:55:49 +00:00
|
|
|
alias Mobilizon.Web.JsonLD.ObjectView
|
2019-09-30 16:18:04 +00:00
|
|
|
|
2020-06-04 13:41:58 +00:00
|
|
|
@important_changes [:title, :begins_on, :ends_on, :status, :physical_address]
|
2020-01-22 23:55:07 +00:00
|
|
|
|
2021-10-13 10:55:49 +00:00
|
|
|
@spec event_updated(
|
|
|
|
Participant.t(),
|
|
|
|
String.t(),
|
|
|
|
Actor.t(),
|
|
|
|
Event.t(),
|
|
|
|
Event.t(),
|
|
|
|
MapSet.t(),
|
2021-11-10 19:31:29 +00:00
|
|
|
String.t(),
|
2021-10-13 10:55:49 +00:00
|
|
|
String.t()
|
|
|
|
) ::
|
2019-09-30 16:18:04 +00:00
|
|
|
Bamboo.Email.t()
|
|
|
|
def event_updated(
|
2020-10-08 06:53:25 +00:00
|
|
|
email,
|
2021-10-13 10:55:49 +00:00
|
|
|
%Participant{} = participant,
|
2019-09-30 16:18:04 +00:00
|
|
|
%Actor{} = actor,
|
|
|
|
%Event{} = old_event,
|
|
|
|
%Event{} = event,
|
|
|
|
changes,
|
2021-11-10 19:31:29 +00:00
|
|
|
timezone,
|
|
|
|
locale
|
2019-09-30 16:18:04 +00:00
|
|
|
) do
|
2021-07-27 17:47:54 +00:00
|
|
|
Gettext.put_locale(locale)
|
2019-09-30 16:18:04 +00:00
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"Event %{title} has been updated",
|
|
|
|
title: old_event.title
|
|
|
|
)
|
|
|
|
|
2021-10-13 10:57:54 +00:00
|
|
|
json_ld =
|
|
|
|
"participation.json"
|
|
|
|
|> ObjectView.render(%{participant: %Participant{participant | event: event, actor: actor}})
|
|
|
|
|> Jason.encode!()
|
|
|
|
|
2020-10-08 06:53:25 +00:00
|
|
|
Email.base_email(to: {Actor.display_name(actor), email}, subject: subject)
|
2019-09-30 16:18:04 +00:00
|
|
|
|> assign(:locale, locale)
|
|
|
|
|> assign(:event, event)
|
|
|
|
|> assign(:old_event, old_event)
|
|
|
|
|> assign(:changes, changes)
|
|
|
|
|> assign(:subject, subject)
|
2020-11-16 17:28:52 +00:00
|
|
|
|> assign(:timezone, timezone)
|
2021-10-13 10:57:54 +00:00
|
|
|
|> assign(:jsonLDMetadata, json_ld)
|
2020-12-21 14:47:26 +00:00
|
|
|
|> Email.add_event_attachment(event)
|
2019-09-30 16:18:04 +00:00
|
|
|
|> render(:event_updated)
|
|
|
|
end
|
2020-01-22 23:55:07 +00:00
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec calculate_event_diff_and_send_notifications(Event.t(), Event.t(), map()) :: {:ok, :ok}
|
2020-01-22 23:55:07 +00:00
|
|
|
def calculate_event_diff_and_send_notifications(
|
|
|
|
%Event{} = old_event,
|
|
|
|
%Event{id: event_id} = event,
|
|
|
|
changes
|
|
|
|
) do
|
2021-11-10 19:31:29 +00:00
|
|
|
important = @important_changes |> Enum.map(&to_string/1) |> MapSet.new()
|
2020-01-22 23:55:07 +00:00
|
|
|
|
|
|
|
diff =
|
|
|
|
changes
|
|
|
|
|> Map.keys()
|
|
|
|
|> MapSet.new()
|
|
|
|
|> MapSet.intersection(important)
|
2021-11-10 19:31:29 +00:00
|
|
|
|> Enum.map(&String.to_existing_atom/1)
|
|
|
|
|> MapSet.new()
|
2020-01-22 23:55:07 +00:00
|
|
|
|
|
|
|
if MapSet.size(diff) > 0 do
|
|
|
|
Repo.transaction(fn ->
|
|
|
|
event_id
|
|
|
|
|> Events.list_local_emails_user_participants_for_event_query()
|
|
|
|
|> Repo.stream()
|
|
|
|
|> Enum.to_list()
|
|
|
|
|> Enum.each(
|
|
|
|
&send_notification_for_event_update_to_participant(&1, old_event, event, diff)
|
|
|
|
)
|
|
|
|
end)
|
2021-11-16 15:03:13 +00:00
|
|
|
else
|
|
|
|
{:ok, :ok}
|
2020-01-22 23:55:07 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec send_notification_for_event_update_to_participant(
|
|
|
|
{Participant.t(), Actor.t(), User.t() | nil, Setting.t() | nil},
|
|
|
|
Event.t(),
|
|
|
|
Event.t(),
|
|
|
|
MapSet.t()
|
|
|
|
) :: Bamboo.Email.t()
|
2020-01-22 23:55:07 +00:00
|
|
|
defp send_notification_for_event_update_to_participant(
|
2021-10-13 10:55:49 +00:00
|
|
|
{%Participant{} = participant, %Actor{} = actor,
|
2020-11-16 17:28:52 +00:00
|
|
|
%User{locale: locale, email: email} = _user, %Setting{timezone: timezone}},
|
2020-01-22 23:55:07 +00:00
|
|
|
%Event{} = old_event,
|
|
|
|
%Event{} = event,
|
|
|
|
diff
|
|
|
|
) do
|
2020-11-16 17:28:52 +00:00
|
|
|
do_send_notification_for_event_update_to_participant(
|
2021-10-13 10:55:49 +00:00
|
|
|
participant,
|
2020-11-16 17:28:52 +00:00
|
|
|
email,
|
|
|
|
actor,
|
|
|
|
old_event,
|
|
|
|
event,
|
|
|
|
diff,
|
|
|
|
timezone,
|
|
|
|
locale
|
|
|
|
)
|
2020-10-08 06:53:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
defp send_notification_for_event_update_to_participant(
|
2021-10-13 10:55:49 +00:00
|
|
|
{%Participant{} = participant, %Actor{} = actor,
|
2020-11-16 17:28:52 +00:00
|
|
|
%User{locale: locale, email: email} = _user, nil},
|
|
|
|
%Event{} = old_event,
|
|
|
|
%Event{} = event,
|
|
|
|
diff
|
|
|
|
) do
|
|
|
|
do_send_notification_for_event_update_to_participant(
|
2021-10-13 10:55:49 +00:00
|
|
|
participant,
|
2020-11-16 17:28:52 +00:00
|
|
|
email,
|
|
|
|
actor,
|
|
|
|
old_event,
|
|
|
|
event,
|
|
|
|
diff,
|
|
|
|
"Etc/UTC",
|
|
|
|
locale
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp send_notification_for_event_update_to_participant(
|
2021-10-13 10:55:49 +00:00
|
|
|
{%Participant{metadata: %{email: email} = participant_metadata} = participant,
|
|
|
|
%Actor{} = actor, nil, nil},
|
2020-10-08 06:53:25 +00:00
|
|
|
%Event{} = old_event,
|
|
|
|
%Event{} = event,
|
|
|
|
diff
|
|
|
|
)
|
|
|
|
when not is_nil(email) do
|
|
|
|
locale = Gettext.get_locale()
|
|
|
|
|
2020-11-16 17:28:52 +00:00
|
|
|
do_send_notification_for_event_update_to_participant(
|
2021-10-13 10:55:49 +00:00
|
|
|
participant,
|
2020-11-16 17:28:52 +00:00
|
|
|
email,
|
|
|
|
actor,
|
|
|
|
old_event,
|
|
|
|
event,
|
|
|
|
diff,
|
2021-10-13 10:55:49 +00:00
|
|
|
Map.get(participant_metadata, :timezone, "Etc/UTC"),
|
2020-11-16 17:28:52 +00:00
|
|
|
locale
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-09-28 17:40:37 +00:00
|
|
|
@spec do_send_notification_for_event_update_to_participant(
|
2021-10-13 10:55:49 +00:00
|
|
|
Participant.t(),
|
2021-09-28 17:40:37 +00:00
|
|
|
String.t(),
|
|
|
|
Actor.t(),
|
|
|
|
Event.t(),
|
|
|
|
Event.t(),
|
|
|
|
MapSet.t(),
|
|
|
|
String.t(),
|
|
|
|
String.t()
|
|
|
|
) :: Bamboo.Email.t()
|
2020-11-16 17:28:52 +00:00
|
|
|
defp do_send_notification_for_event_update_to_participant(
|
2021-10-13 10:55:49 +00:00
|
|
|
participant,
|
2020-11-16 17:28:52 +00:00
|
|
|
email,
|
|
|
|
actor,
|
|
|
|
old_event,
|
|
|
|
event,
|
|
|
|
diff,
|
|
|
|
timezone,
|
|
|
|
locale
|
|
|
|
) do
|
2020-10-08 06:53:25 +00:00
|
|
|
email
|
2021-11-10 19:31:29 +00:00
|
|
|
|> event_updated(participant, actor, old_event, event, diff, timezone, locale)
|
2021-04-20 13:02:24 +00:00
|
|
|
|> Email.Mailer.send_email_later()
|
2020-01-22 23:55:07 +00:00
|
|
|
end
|
2019-09-30 16:18:04 +00:00
|
|
|
end
|