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
|
2019-09-30 16:18:04 +00:00
|
|
|
alias Mobilizon.Users.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,
|
|
|
|
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)
|
|
|
|
|> 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,
|
|
|
|
%User{locale: locale, email: email} = _user},
|
2020-01-22 23:55:07 +00:00
|
|
|
%Event{} = old_event,
|
|
|
|
%Event{} = event,
|
|
|
|
diff
|
|
|
|
) do
|
2020-10-08 06:53:25 +00:00
|
|
|
email
|
|
|
|
|> Email.Event.event_updated(actor, old_event, event, diff, locale)
|
|
|
|
|> Email.Mailer.deliver_later()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp send_notification_for_event_update_to_participant(
|
|
|
|
{%Participant{metadata: %{email: email}} = _participant, %Actor{} = actor, nil},
|
|
|
|
%Event{} = old_event,
|
|
|
|
%Event{} = event,
|
|
|
|
diff
|
|
|
|
)
|
|
|
|
when not is_nil(email) do
|
|
|
|
locale = Gettext.get_locale()
|
|
|
|
|
|
|
|
email
|
2020-01-22 23:55:07 +00:00
|
|
|
|> Email.Event.event_updated(actor, old_event, event, diff, locale)
|
|
|
|
|> Email.Mailer.deliver_later()
|
|
|
|
end
|
2019-09-30 16:18:04 +00:00
|
|
|
end
|