Fix scheduler for participation notifications

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2020-07-06 18:55:58 +02:00
parent 31aefde76d
commit e740fabf93

View File

@ -137,6 +137,7 @@ defmodule Mobilizon.Service.Notifications.Scheduler do
with %Actor{user_id: user_id} when not is_nil(user_id) <- with %Actor{user_id: user_id} when not is_nil(user_id) <-
Actors.get_actor(organizer_actor_id), Actors.get_actor(organizer_actor_id),
%User{ %User{
locale: locale,
settings: %Setting{ settings: %Setting{
notification_pending_participation: notification_pending_participation, notification_pending_participation: notification_pending_participation,
timezone: timezone timezone: timezone
@ -151,7 +152,7 @@ defmodule Mobilizon.Service.Notifications.Scheduler do
:direct :direct
:one_day -> :one_day ->
calculate_next_day_notification(Date.utc_today(), timezone) calculate_next_day_notification(Date.utc_today(), timezone, locale)
:one_hour -> :one_hour ->
DateTime.utc_now() DateTime.utc_now()
@ -200,14 +201,22 @@ defmodule Mobilizon.Service.Notifications.Scheduler do
else: calculate_first_day_of_week(Date.add(date, -1), locale) else: calculate_first_day_of_week(Date.add(date, -1), locale)
end end
defp calculate_next_day_notification(%Date{} = day, timezone) do defp calculate_next_day_notification(%Date{} = day, timezone, locale) do
{:ok, send_at} = NaiveDateTime.new(day, ~T[18:00:00]) send_at = date_to_datetime(day, ~T[18:00:00], timezone)
{:ok, send_at} = DateTime.from_naive(send_at, timezone)
if DateTime.compare(send_at, DateTime.utc_now()) == :lt do if DateTime.compare(send_at, DateTime.utc_now()) == :lt do
calculate_first_day_of_week(Date.add(day, 1), timezone) day
|> Date.add(1)
|> calculate_first_day_of_week(locale)
|> date_to_datetime(~T[18:00:00], timezone)
else else
send_at send_at
end end
end end
defp date_to_datetime(%Date{} = day, time, timezone) do
{:ok, datetime} = NaiveDateTime.new(day, time)
{:ok, datetime} = DateTime.from_naive(datetime, timezone)
datetime
end
end end