2020-01-26 21:36:50 +01:00
|
|
|
defmodule Mobilizon.Web.Email.Event do
|
2019-09-30 18:18:04 +02:00
|
|
|
@moduledoc """
|
|
|
|
Handles emails sent about events.
|
|
|
|
"""
|
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
use Bamboo.Phoenix, view: Mobilizon.Web.EmailView
|
2019-09-30 18:18:04 +02:00
|
|
|
|
|
|
|
import Bamboo.Phoenix
|
|
|
|
|
2020-01-26 21:36:50 +01:00
|
|
|
import Mobilizon.Web.Gettext
|
2019-09-30 18:18:04 +02:00
|
|
|
|
|
|
|
alias Mobilizon.Actors.Actor
|
2020-01-23 00:55:07 +01:00
|
|
|
alias Mobilizon.Events
|
|
|
|
alias Mobilizon.Events.Event
|
|
|
|
alias Mobilizon.Storage.Repo
|
2019-09-30 18:18:04 +02:00
|
|
|
alias Mobilizon.Users.User
|
2020-01-23 00:55:07 +01:00
|
|
|
|
2020-01-28 20:15:59 +01:00
|
|
|
alias Mobilizon.Web.{Email, Gettext}
|
2019-09-30 18:18:04 +02:00
|
|
|
|
2020-06-04 15:41:58 +02:00
|
|
|
@important_changes [:title, :begins_on, :ends_on, :status, :physical_address]
|
2020-01-23 00:55:07 +01:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@spec event_updated(User.t(), Actor.t(), Event.t(), Event.t(), MapSet.t(), String.t()) ::
|
2019-09-30 18:18:04 +02:00
|
|
|
Bamboo.Email.t()
|
|
|
|
def event_updated(
|
|
|
|
%User{} = user,
|
|
|
|
%Actor{} = actor,
|
|
|
|
%Event{} = old_event,
|
|
|
|
%Event{} = event,
|
|
|
|
changes,
|
|
|
|
locale \\ "en"
|
|
|
|
) do
|
2020-01-28 19:18:33 +01:00
|
|
|
Gettext.put_locale(locale)
|
2019-09-30 18:18:04 +02:00
|
|
|
|
|
|
|
subject =
|
|
|
|
gettext(
|
|
|
|
"Event %{title} has been updated",
|
|
|
|
title: old_event.title
|
|
|
|
)
|
|
|
|
|
|
|
|
Email.base_email(to: {Actor.display_name(actor), user.email}, subject: subject)
|
|
|
|
|> assign(:locale, locale)
|
|
|
|
|> assign(:event, event)
|
|
|
|
|> assign(:old_event, old_event)
|
|
|
|
|> assign(:changes, changes)
|
|
|
|
|> assign(:subject, subject)
|
|
|
|
|> render(:event_updated)
|
|
|
|
end
|
2020-01-23 00:55:07 +01: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(
|
|
|
|
{%Actor{} = actor, %User{locale: locale} = user},
|
|
|
|
%Event{} = old_event,
|
|
|
|
%Event{} = event,
|
|
|
|
diff
|
|
|
|
) do
|
|
|
|
user
|
|
|
|
|> Email.Event.event_updated(actor, old_event, event, diff, locale)
|
|
|
|
|> Email.Mailer.deliver_later()
|
|
|
|
end
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
defp send_notification_for_event_update_to_participant(user, old_event, new_event, diff) do
|
|
|
|
require Logger
|
|
|
|
Logger.error(inspect(user))
|
|
|
|
Logger.error(inspect(old_event))
|
|
|
|
Logger.error(inspect(new_event))
|
|
|
|
Logger.error(inspect(diff))
|
|
|
|
end
|
2019-09-30 18:18:04 +02:00
|
|
|
end
|