Send activity digests

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-06-26 15:23:22 +02:00
parent a542f94379
commit 784c607c65
95 changed files with 3259 additions and 2382 deletions

View File

@@ -6,10 +6,17 @@ defmodule Mobilizon.Service.Notifier.Email do
alias Mobilizon.{Config, Users}
alias Mobilizon.Service.Notifier
alias Mobilizon.Service.Notifier.{Email, Filter}
alias Mobilizon.Users.{NotificationPendingNotificationDelay, Setting, User}
alias Mobilizon.Users.{Setting, User}
alias Mobilizon.Web.Email.Activity, as: EmailActivity
alias Mobilizon.Web.Email.Mailer
import Mobilizon.Service.DateTime,
only: [
is_delay_ok_since_last_notification_sent: 1
]
require Logger
@behaviour Notifier
@impl Notifier
@@ -27,9 +34,11 @@ defmodule Mobilizon.Service.Notifier.Email do
@impl Notifier
def send(%User{email: email, locale: locale} = user, activities, options)
when is_list(activities) do
activities = Enum.filter(activities, &can_send_activity?(&1, user))
activities = Enum.filter(activities, &can_send_activity?(&1, user, options))
if length(activities) > 0 do
Logger.debug("Found some activities to send by email")
if can_send?(user) && length(activities) > 0 do
email
|> EmailActivity.direct_activity(activities, Keyword.put(options, :locale, locale))
|> Mailer.send_email()
@@ -37,13 +46,80 @@ defmodule Mobilizon.Service.Notifier.Email do
save_last_notification_time(user)
{:ok, :sent}
else
Logger.debug("No activities to send by email")
{:ok, :skipped}
end
end
@spec can_send_activity?(Activity.t(), User.t()) :: boolean()
defp can_send_activity?(%Activity{} = activity, %User{} = user) do
Filter.can_send_activity?(activity, "email", user, &default_activity_behavior/1)
# 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,
:discussion_mention
]
@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
defp match_group_notifications_setting(
group_notifications,
_subject,
last_notification_sent,
options
) do
cond do
# This is a recap
Keyword.get(options, :recap, false) != false ->
true
# First notification EVER!
group_notifications == :one_hour && is_nil(last_notification_sent) ->
true
# Delay ok since last notification
group_notifications == :one_hour &&
is_delay_ok_since_last_notification_sent(last_notification_sent) ->
true
# Otherwise, no thanks
true ->
false
end
end
@default_behavior %{
@@ -70,32 +146,6 @@ defmodule Mobilizon.Service.Notifier.Email do
Map.get(@default_behavior, activity_setting, false)
end
@type notification_type ::
:group_notifications
| :notification_pending_participation
| :notification_pending_membership
@spec user_notification_delay(User.t(), notification_type()) ::
NotificationPendingNotificationDelay.t()
defp user_notification_delay(%User{} = user, type \\ :group_notifications) do
Map.from_struct(user.settings)[type]
end
@spec can_send?(User.t()) :: boolean()
defp can_send?(%User{settings: %Setting{last_notification_sent: last_notification_sent}} = user) do
last_notification_sent_or_default = last_notification_sent || DateTime.utc_now()
notification_delay = user_notification_delay(user)
diff = DateTime.diff(DateTime.utc_now(), last_notification_sent_or_default)
cond do
notification_delay == :none -> false
is_nil(last_notification_sent) -> true
notification_delay == :direct -> true
notification_delay == :one_hour -> diff >= 60 * 60
notification_delay == :one_day -> diff >= 24 * 60 * 60
end
end
@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()}