2021-03-23 15:18:03 +01:00
|
|
|
defmodule Mobilizon.Service.Notifier.Email do
|
|
|
|
@moduledoc """
|
|
|
|
Email notifier
|
|
|
|
"""
|
|
|
|
alias Mobilizon.Activities.Activity
|
2021-05-06 12:27:04 +02:00
|
|
|
alias Mobilizon.{Config, Users}
|
2021-03-23 15:18:03 +01:00
|
|
|
alias Mobilizon.Service.Notifier
|
2021-06-01 18:08:03 +02:00
|
|
|
alias Mobilizon.Service.Notifier.{Email, Filter}
|
2021-06-26 15:23:22 +02:00
|
|
|
alias Mobilizon.Users.{Setting, User}
|
2021-03-23 15:18:03 +01:00
|
|
|
alias Mobilizon.Web.Email.Activity, as: EmailActivity
|
|
|
|
alias Mobilizon.Web.Email.Mailer
|
|
|
|
|
2021-06-26 15:23:22 +02:00
|
|
|
import Mobilizon.Service.DateTime,
|
|
|
|
only: [
|
2022-05-10 13:13:48 +02:00
|
|
|
is_delay_ok_since_last_notification_sent?: 1,
|
|
|
|
is_delay_ok_since_last_notification_sent?: 2
|
2021-06-26 15:23:22 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
2021-03-23 15:18:03 +01:00
|
|
|
@behaviour Notifier
|
|
|
|
|
|
|
|
@impl Notifier
|
|
|
|
def ready? do
|
2021-06-03 17:17:13 +02:00
|
|
|
Config.get([__MODULE__, :enabled])
|
2021-03-23 15:18:03 +01:00
|
|
|
end
|
|
|
|
|
2021-06-01 18:08:03 +02:00
|
|
|
def send(user, activity, options \\ [])
|
|
|
|
|
2021-03-23 15:18:03 +01:00
|
|
|
@impl Notifier
|
2021-05-06 12:27:04 +02:00
|
|
|
def send(%User{} = user, %Activity{} = activity, options) do
|
|
|
|
Email.send(user, [activity], options)
|
2021-03-23 15:18:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl Notifier
|
2021-05-06 12:27:04 +02:00
|
|
|
def send(%User{email: email, locale: locale} = user, activities, options)
|
|
|
|
when is_list(activities) do
|
2021-06-26 15:23:22 +02:00
|
|
|
activities = Enum.filter(activities, &can_send_activity?(&1, user, options))
|
2022-05-10 13:13:48 +02:00
|
|
|
nb_activities = length(activities)
|
2021-06-26 15:23:22 +02:00
|
|
|
|
2022-05-10 13:13:48 +02:00
|
|
|
if nb_activities > 0 do
|
|
|
|
Logger.info("Sending email containing #{nb_activities} activities to #{email}")
|
2021-06-01 18:08:03 +02:00
|
|
|
|
2021-05-06 12:27:04 +02:00
|
|
|
email
|
|
|
|
|> EmailActivity.direct_activity(activities, Keyword.put(options, :locale, locale))
|
|
|
|
|> Mailer.send_email()
|
|
|
|
|
|
|
|
save_last_notification_time(user)
|
|
|
|
{:ok, :sent}
|
|
|
|
else
|
2021-06-26 15:23:22 +02:00
|
|
|
Logger.debug("No activities to send by email")
|
2021-05-06 12:27:04 +02:00
|
|
|
{:ok, :skipped}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-27 18:16:34 +02:00
|
|
|
@doc """
|
|
|
|
Send an anonymous activity directly to an email, for anonymous participants for instance
|
|
|
|
"""
|
|
|
|
@spec send_anonymous_activity(String.t(), Activity.t(), Keyword.t()) :: {:ok, :sent}
|
|
|
|
def send_anonymous_activity(email, %Activity{} = activity, options) do
|
|
|
|
email
|
|
|
|
|> EmailActivity.anonymous_activity(activity, options)
|
|
|
|
|> Mailer.send_email()
|
|
|
|
|
|
|
|
{:ok, :sent}
|
|
|
|
end
|
|
|
|
|
2021-06-26 15:23:22 +02:00
|
|
|
# These notifications are using LegacyNotifierBuilder and don't have any history,
|
|
|
|
# so we always send them directly, as long as the setting isn't none
|
|
|
|
@always_direct_subjects [
|
|
|
|
:participation_event_comment,
|
|
|
|
:event_comment_mention,
|
2021-06-27 11:21:34 +02:00
|
|
|
:discussion_mention,
|
|
|
|
:event_new_comment
|
2021-06-26 15:23:22 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
@spec can_send_activity?(Activity.t(), User.t(), Keyword.t()) :: boolean()
|
|
|
|
defp can_send_activity?(
|
|
|
|
%Activity{subject: subject} = activity,
|
|
|
|
%User{
|
|
|
|
settings: %Setting{
|
|
|
|
group_notifications: group_notifications,
|
|
|
|
last_notification_sent: last_notification_sent
|
|
|
|
}
|
|
|
|
} = user,
|
|
|
|
options
|
|
|
|
) do
|
|
|
|
Filter.can_send_activity?(activity, "email", user, &default_activity_behavior/1) &&
|
|
|
|
match_group_notifications_setting(
|
|
|
|
group_notifications,
|
|
|
|
subject,
|
|
|
|
last_notification_sent,
|
|
|
|
options
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
@spec match_group_notifications_setting(
|
2022-04-18 14:38:57 +02:00
|
|
|
non_neg_integer(),
|
2021-06-26 15:23:22 +02:00
|
|
|
String.t(),
|
|
|
|
DateTime.t() | nil,
|
|
|
|
Keyword.t()
|
|
|
|
) :: boolean()
|
|
|
|
# No notifications at all
|
|
|
|
defp match_group_notifications_setting(:none, _, _, _), do: false
|
|
|
|
|
|
|
|
# Every notification
|
|
|
|
defp match_group_notifications_setting(:direct, _, _, _), do: true
|
|
|
|
|
|
|
|
# Direct notifications
|
|
|
|
defp match_group_notifications_setting(_, subject, _, _)
|
|
|
|
when subject in @always_direct_subjects,
|
|
|
|
do: true
|
|
|
|
|
2021-06-27 11:21:34 +02:00
|
|
|
# First notification EVER!
|
|
|
|
defp match_group_notifications_setting(:one_hour, _, last_notification_sent, _)
|
|
|
|
when is_nil(last_notification_sent),
|
|
|
|
do: true
|
|
|
|
|
|
|
|
# Delay ok since last notification
|
|
|
|
defp match_group_notifications_setting(:one_hour, _, %DateTime{} = last_notification_sent, _) do
|
2021-09-24 16:46:42 +02:00
|
|
|
is_delay_ok_since_last_notification_sent?(last_notification_sent)
|
2021-06-27 11:21:34 +02:00
|
|
|
end
|
|
|
|
|
2022-05-10 13:13:48 +02:00
|
|
|
# Delay ok since last notification
|
|
|
|
defp match_group_notifications_setting(
|
|
|
|
:one_day,
|
|
|
|
_,
|
|
|
|
%DateTime{} = last_notification_sent,
|
|
|
|
options
|
|
|
|
) do
|
|
|
|
is_delay_ok_since_last_notification_sent?(last_notification_sent, 3_600 * 23) and
|
|
|
|
Keyword.get(options, :recap, false) != false
|
|
|
|
end
|
|
|
|
|
|
|
|
defp match_group_notifications_setting(
|
|
|
|
:one_week,
|
|
|
|
_,
|
|
|
|
%DateTime{} = last_notification_sent,
|
|
|
|
options
|
|
|
|
) do
|
|
|
|
is_delay_ok_since_last_notification_sent?(last_notification_sent, 3_600 * 24 * 6) and
|
|
|
|
Keyword.get(options, :recap, false) != false
|
|
|
|
end
|
|
|
|
|
2021-06-27 11:21:34 +02:00
|
|
|
# This is a recap
|
2021-06-26 15:23:22 +02:00
|
|
|
defp match_group_notifications_setting(
|
2021-06-27 11:21:34 +02:00
|
|
|
_group_notifications,
|
2021-06-26 15:23:22 +02:00
|
|
|
_subject,
|
2021-06-27 11:21:34 +02:00
|
|
|
_last_notification_sent,
|
2021-06-26 15:23:22 +02:00
|
|
|
options
|
|
|
|
) do
|
2021-06-27 11:21:34 +02:00
|
|
|
Keyword.get(options, :recap, false) != false
|
2021-06-01 18:08:03 +02:00
|
|
|
end
|
|
|
|
|
2021-06-03 15:00:49 +02:00
|
|
|
@default_behavior %{
|
|
|
|
"participation_event_updated" => true,
|
|
|
|
"participation_event_comment" => true,
|
|
|
|
"event_new_pending_participation" => true,
|
|
|
|
"event_new_participation" => false,
|
2021-11-10 16:36:32 +01:00
|
|
|
"event_created" => true,
|
2021-06-03 15:00:49 +02:00
|
|
|
"event_updated" => false,
|
|
|
|
"discussion_updated" => false,
|
|
|
|
"post_published" => false,
|
|
|
|
"post_updated" => false,
|
|
|
|
"resource_updated" => false,
|
|
|
|
"member_request" => true,
|
|
|
|
"member_updated" => false,
|
|
|
|
"user_email_password_updated" => true,
|
|
|
|
"event_comment_mention" => true,
|
|
|
|
"discussion_mention" => true,
|
|
|
|
"event_new_comment" => true
|
|
|
|
}
|
|
|
|
|
2021-06-01 18:08:03 +02:00
|
|
|
@spec default_activity_behavior(String.t()) :: boolean()
|
|
|
|
defp default_activity_behavior(activity_setting) do
|
2021-06-03 15:00:49 +02:00
|
|
|
Map.get(@default_behavior, activity_setting, false)
|
2021-06-01 18:08:03 +02:00
|
|
|
end
|
|
|
|
|
2021-05-06 12:27:04 +02:00
|
|
|
@spec save_last_notification_time(User.t()) :: {:ok, Setting.t()} | {:error, Ecto.Changeset.t()}
|
2022-05-10 13:13:48 +02:00
|
|
|
defp save_last_notification_time(%User{id: user_id, email: email}) do
|
2022-05-11 09:47:18 +02:00
|
|
|
Logger.info("Saving last notification time for user #{email}")
|
2021-05-06 12:27:04 +02:00
|
|
|
attrs = %{user_id: user_id, last_notification_sent: DateTime.utc_now()}
|
|
|
|
|
|
|
|
case Users.get_setting(user_id) do
|
|
|
|
nil ->
|
|
|
|
Users.create_setting(attrs)
|
|
|
|
|
|
|
|
%Setting{} = setting ->
|
|
|
|
Users.update_setting(setting, attrs)
|
|
|
|
end
|
2021-03-23 15:18:03 +01:00
|
|
|
end
|
|
|
|
end
|