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-01-22 23:55:07 +00:00
|
|
|
|
2020-10-08 06:53:25 +00:00
|
|
|
alias Mobilizon.Web.Email
|
|
|
|
alias Mobilizon.Web.Gettext, as: GettextBackend
|
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
|
|
|
|
2020-10-08 06:53:25 +00:00
|
|
|
@spec event_updated(String.t(), Actor.t(), Event.t(), Event.t(), MapSet.t(), 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,
|
2019-09-30 16:18:04 +00:00
|
|
|
%Actor{} = actor,
|
|
|
|
%Event{} = old_event,
|
|
|
|
%Event{} = event,
|
|
|
|
changes,
|
2020-11-16 17:28:52 +00:00
|
|
|
timezone \\ "Etc/UTC",
|
2019-09-30 16:18:04 +00:00
|
|
|
locale \\ "en"
|
|
|
|
) do
|
2020-10-08 06:53:25 +00:00
|
|
|
GettextBackend.put_locale(locale)
|
2019-09-30 16:18:04 +00:00
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"Event %{title} has been updated",
|
|
|
|
title: old_event.title
|
|
|
|
)
|
|
|
|
|
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)
|
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
|
|
|
|
|
|
|
def calculate_event_diff_and_send_notifications(
|
|
|
|
%Event{} = old_event,
|
|
|
|
%Event{id: event_id} = event,
|
|
|
|
changes
|
|
|
|
) do
|
|
|
|
important = MapSet.new(@important_changes)
|
|
|
|
|
|
|
|
diff =
|
|
|
|
changes
|
|
|
|
|> Map.keys()
|
|
|
|
|> MapSet.new()
|
|
|
|
|> MapSet.intersection(important)
|
|
|
|
|
|
|
|
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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp send_notification_for_event_update_to_participant(
|
2020-10-08 06:53:25 +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(
|
|
|
|
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(
|
2020-11-16 17:28:52 +00:00
|
|
|
{%Participant{} = _participant, %Actor{} = actor,
|
|
|
|
%User{locale: locale, email: email} = _user, nil},
|
|
|
|
%Event{} = old_event,
|
|
|
|
%Event{} = event,
|
|
|
|
diff
|
|
|
|
) do
|
|
|
|
do_send_notification_for_event_update_to_participant(
|
|
|
|
email,
|
|
|
|
actor,
|
|
|
|
old_event,
|
|
|
|
event,
|
|
|
|
diff,
|
|
|
|
"Etc/UTC",
|
|
|
|
locale
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp send_notification_for_event_update_to_participant(
|
|
|
|
{%Participant{metadata: %{email: email}} = _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(
|
|
|
|
email,
|
|
|
|
actor,
|
|
|
|
old_event,
|
|
|
|
event,
|
|
|
|
diff,
|
|
|
|
"Etc/UTC",
|
|
|
|
locale
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
defp do_send_notification_for_event_update_to_participant(
|
|
|
|
email,
|
|
|
|
actor,
|
|
|
|
old_event,
|
|
|
|
event,
|
|
|
|
diff,
|
|
|
|
timezone,
|
|
|
|
locale
|
|
|
|
) do
|
2020-10-08 06:53:25 +00:00
|
|
|
email
|
2020-11-16 17:28:52 +00:00
|
|
|
|> Email.Event.event_updated(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
|