2021-03-23 14:18:03 +00:00
|
|
|
defmodule Mobilizon.Service.Notifier.Email do
|
|
|
|
@moduledoc """
|
|
|
|
Email notifier
|
|
|
|
"""
|
|
|
|
alias Mobilizon.Activities.Activity
|
2021-05-06 10:27:04 +00:00
|
|
|
alias Mobilizon.{Config, Users}
|
2021-03-23 14:18:03 +00:00
|
|
|
alias Mobilizon.Service.Notifier
|
2021-06-01 16:08:03 +00:00
|
|
|
alias Mobilizon.Service.Notifier.{Email, Filter}
|
2021-06-26 13:23:22 +00:00
|
|
|
alias Mobilizon.Users.{Setting, User}
|
2021-03-23 14:18:03 +00:00
|
|
|
alias Mobilizon.Web.Email.Activity, as: EmailActivity
|
|
|
|
alias Mobilizon.Web.Email.Mailer
|
|
|
|
|
2021-06-26 13:23:22 +00:00
|
|
|
import Mobilizon.Service.DateTime,
|
|
|
|
only: [
|
2021-09-24 14:46:42 +00:00
|
|
|
is_delay_ok_since_last_notification_sent?: 1
|
2021-06-26 13:23:22 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
2021-03-23 14:18:03 +00:00
|
|
|
@behaviour Notifier
|
|
|
|
|
|
|
|
@impl Notifier
|
|
|
|
def ready? do
|
2021-06-03 15:17:13 +00:00
|
|
|
Config.get([__MODULE__, :enabled])
|
2021-03-23 14:18:03 +00:00
|
|
|
end
|
|
|
|
|
2021-06-01 16:08:03 +00:00
|
|
|
def send(user, activity, options \\ [])
|
|
|
|
|
2021-03-23 14:18:03 +00:00
|
|
|
@impl Notifier
|
2021-05-06 10:27:04 +00:00
|
|
|
def send(%User{} = user, %Activity{} = activity, options) do
|
|
|
|
Email.send(user, [activity], options)
|
2021-03-23 14:18:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl Notifier
|
2021-05-06 10:27:04 +00:00
|
|
|
def send(%User{email: email, locale: locale} = user, activities, options)
|
|
|
|
when is_list(activities) do
|
2021-06-26 13:23:22 +00:00
|
|
|
activities = Enum.filter(activities, &can_send_activity?(&1, user, options))
|
|
|
|
|
|
|
|
if length(activities) > 0 do
|
|
|
|
Logger.debug("Found some activities to send by email")
|
2021-06-01 16:08:03 +00:00
|
|
|
|
2021-05-06 10:27:04 +00: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 13:23:22 +00:00
|
|
|
Logger.debug("No activities to send by email")
|
2021-05-06 10:27:04 +00:00
|
|
|
{:ok, :skipped}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-27 16:16:34 +00: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 13:23:22 +00: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 09:21:34 +00:00
|
|
|
:discussion_mention,
|
|
|
|
:event_new_comment
|
2021-06-26 13:23:22 +00: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(
|
|
|
|
NotificationPendingNotificationDelay.t(),
|
|
|
|
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 09:21:34 +00: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 14:46:42 +00:00
|
|
|
is_delay_ok_since_last_notification_sent?(last_notification_sent)
|
2021-06-27 09:21:34 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# This is a recap
|
2021-06-26 13:23:22 +00:00
|
|
|
defp match_group_notifications_setting(
|
2021-06-27 09:21:34 +00:00
|
|
|
_group_notifications,
|
2021-06-26 13:23:22 +00:00
|
|
|
_subject,
|
2021-06-27 09:21:34 +00:00
|
|
|
_last_notification_sent,
|
2021-06-26 13:23:22 +00:00
|
|
|
options
|
|
|
|
) do
|
2021-06-27 09:21:34 +00:00
|
|
|
Keyword.get(options, :recap, false) != false
|
2021-06-01 16:08:03 +00:00
|
|
|
end
|
|
|
|
|
2021-06-03 13:00:49 +00:00
|
|
|
@default_behavior %{
|
|
|
|
"participation_event_updated" => true,
|
|
|
|
"participation_event_comment" => true,
|
|
|
|
"event_new_pending_participation" => true,
|
|
|
|
"event_new_participation" => false,
|
|
|
|
"event_created" => false,
|
|
|
|
"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 16:08:03 +00:00
|
|
|
@spec default_activity_behavior(String.t()) :: boolean()
|
|
|
|
defp default_activity_behavior(activity_setting) do
|
2021-06-03 13:00:49 +00:00
|
|
|
Map.get(@default_behavior, activity_setting, false)
|
2021-06-01 16:08:03 +00:00
|
|
|
end
|
|
|
|
|
2021-05-06 10:27:04 +00:00
|
|
|
@spec save_last_notification_time(User.t()) :: {:ok, Setting.t()} | {:error, Ecto.Changeset.t()}
|
|
|
|
defp save_last_notification_time(%User{id: user_id}) do
|
|
|
|
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 14:18:03 +00:00
|
|
|
end
|
|
|
|
end
|