@@ -7,7 +7,7 @@ defmodule Mobilizon.Service.Notifications.Scheduler do
|
||||
alias Mobilizon.Events.{Event, Participant}
|
||||
alias Mobilizon.Service.Workers.Notification
|
||||
alias Mobilizon.Users
|
||||
alias Mobilizon.Users.Setting
|
||||
alias Mobilizon.Users.{Setting, User}
|
||||
require Logger
|
||||
|
||||
def before_event_notification(%Participant{
|
||||
@@ -73,10 +73,75 @@ defmodule Mobilizon.Service.Notifications.Scheduler do
|
||||
|
||||
def on_day_notification(_), do: {:ok, nil}
|
||||
|
||||
def weekly_notification(%Participant{
|
||||
event: %Event{begins_on: begins_on},
|
||||
actor: %Actor{user_id: user_id}
|
||||
})
|
||||
when not is_nil(user_id) do
|
||||
%User{settings: settings, locale: locale} = Users.get_user_with_settings!(user_id)
|
||||
|
||||
case settings do
|
||||
%Setting{notification_each_week: true, timezone: timezone} ->
|
||||
%DateTime{} = begins_on_shifted = shift_zone(begins_on, timezone)
|
||||
|
||||
Logger.debug(
|
||||
"Participation event start at #{inspect(begins_on_shifted)} (user timezone is #{
|
||||
timezone
|
||||
})"
|
||||
)
|
||||
|
||||
notification_date =
|
||||
unless begins_on < DateTime.utc_now() do
|
||||
notification_day = calculate_first_day_of_week(DateTime.to_date(begins_on), locale)
|
||||
|
||||
{:ok, %NaiveDateTime{} = notification_date} =
|
||||
notification_day |> NaiveDateTime.new(~T[08:00:00])
|
||||
|
||||
# This is the datetime when the notification should be sent
|
||||
{:ok, %DateTime{} = notification_date} =
|
||||
DateTime.from_naive(notification_date, timezone)
|
||||
|
||||
unless notification_date < DateTime.utc_now() do
|
||||
notification_date
|
||||
else
|
||||
nil
|
||||
end
|
||||
else
|
||||
nil
|
||||
end
|
||||
|
||||
Logger.debug(
|
||||
"Participation notification should be sent at #{inspect(notification_date)} (user timezone)"
|
||||
)
|
||||
|
||||
if is_nil(notification_date) do
|
||||
{:ok, "Too late to send weekly notifications"}
|
||||
else
|
||||
Notification.enqueue(:weekly_notification, %{user_id: user_id},
|
||||
scheduled_at: notification_date
|
||||
)
|
||||
end
|
||||
|
||||
_ ->
|
||||
{:ok, "User has disabled weekly notifications"}
|
||||
end
|
||||
end
|
||||
|
||||
def weekly_notification(_), do: {:ok, nil}
|
||||
|
||||
defp shift_zone(datetime, timezone) do
|
||||
case DateTime.shift_zone(datetime, timezone) do
|
||||
{:ok, shift_datetime} -> shift_datetime
|
||||
{:error, _} -> datetime
|
||||
end
|
||||
end
|
||||
|
||||
defp calculate_first_day_of_week(%Date{} = date, locale) do
|
||||
day_number = Date.day_of_week(date)
|
||||
first_day_number = Cldr.Calendar.first_day_for_locale(locale)
|
||||
|
||||
if day_number == first_day_number,
|
||||
do: date,
|
||||
else: calculate_first_day_of_week(Date.add(date, -1), locale)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -28,29 +28,55 @@ defmodule Mobilizon.Service.Workers.Notification do
|
||||
end
|
||||
|
||||
def perform(%{"op" => "on_day_notification", "user_id" => user_id}, _job) do
|
||||
%User{locale: locale, settings: %Setting{timezone: timezone, notification_on_day: true}} =
|
||||
user = Users.get_user_with_settings!(user_id)
|
||||
|
||||
now = DateTime.utc_now()
|
||||
%DateTime{} = now_shifted = shift_zone(now, timezone)
|
||||
start = %{now_shifted | hour: 8, minute: 0, second: 0, microsecond: {0, 0}}
|
||||
tomorrow = DateTime.add(start, 3600 * 24)
|
||||
|
||||
with %Page{
|
||||
with %User{locale: locale, settings: %Setting{timezone: timezone, notification_on_day: true}} =
|
||||
user <- Users.get_user_with_settings!(user_id),
|
||||
{start, tomorrow} <- calculate_start_end(1, timezone),
|
||||
%Page{
|
||||
elements: participations,
|
||||
total: total
|
||||
} <-
|
||||
}
|
||||
when total > 0 <-
|
||||
Events.list_participations_for_user(user_id, start, tomorrow, 1, 5),
|
||||
true <-
|
||||
Enum.all?(participations, fn participation ->
|
||||
participations <-
|
||||
Enum.filter(participations, fn participation ->
|
||||
participation.event.status == :confirmed
|
||||
end),
|
||||
true <- total > 0 do
|
||||
true <- length(participations) > 0 do
|
||||
user
|
||||
|> Notification.on_day_notification(participations, total, locale)
|
||||
|> Mailer.deliver_later()
|
||||
|
||||
:ok
|
||||
else
|
||||
_ -> :ok
|
||||
end
|
||||
end
|
||||
|
||||
def perform(%{"op" => "weekly_notification", "user_id" => user_id}, _job) do
|
||||
with %User{
|
||||
locale: locale,
|
||||
settings: %Setting{timezone: timezone, notification_each_week: true}
|
||||
} = user <- Users.get_user_with_settings!(user_id),
|
||||
{start, end_week} <- calculate_start_end(7, timezone),
|
||||
%Page{
|
||||
elements: participations,
|
||||
total: total
|
||||
}
|
||||
when total > 0 <-
|
||||
Events.list_participations_for_user(user_id, start, end_week, 1, 5),
|
||||
participations <-
|
||||
Enum.filter(participations, fn participation ->
|
||||
participation.event.status == :confirmed
|
||||
end),
|
||||
true <- length(participations) > 0 do
|
||||
user
|
||||
|> Notification.weekly_notification(participations, total, locale)
|
||||
|> Mailer.deliver_later()
|
||||
|
||||
:ok
|
||||
else
|
||||
_err ->
|
||||
:ok
|
||||
end
|
||||
end
|
||||
|
||||
@@ -60,4 +86,16 @@ defmodule Mobilizon.Service.Workers.Notification do
|
||||
{:error, _} -> datetime
|
||||
end
|
||||
end
|
||||
|
||||
defp calculate_start_end(days, timezone) do
|
||||
now = DateTime.utc_now()
|
||||
%DateTime{} = now_shifted = shift_zone(now, timezone)
|
||||
start = %{now_shifted | hour: 8, minute: 0, second: 0, microsecond: {0, 0}}
|
||||
|
||||
{:ok, %NaiveDateTime{} = tomorrow} =
|
||||
Date.utc_today() |> Date.add(days) |> NaiveDateTime.new(~T[08:00:00])
|
||||
|
||||
{:ok, %DateTime{} = tomorrow} = DateTime.from_naive(tomorrow, timezone)
|
||||
{start, tomorrow}
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user