From c7ad21262cb88b4a2e8f41862521f97da8a9a340 Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Mon, 6 Jul 2020 18:55:58 +0200 Subject: [PATCH] Fix scheduler for participation notifications Signed-off-by: Thomas Citharel --- lib/service/notifications/scheduler.ex | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/service/notifications/scheduler.ex b/lib/service/notifications/scheduler.ex index 244746504..902e1e689 100644 --- a/lib/service/notifications/scheduler.ex +++ b/lib/service/notifications/scheduler.ex @@ -137,6 +137,7 @@ defmodule Mobilizon.Service.Notifications.Scheduler do with %Actor{user_id: user_id} when not is_nil(user_id) <- Actors.get_actor(organizer_actor_id), %User{ + locale: locale, settings: %Setting{ notification_pending_participation: notification_pending_participation, timezone: timezone @@ -151,7 +152,7 @@ defmodule Mobilizon.Service.Notifications.Scheduler do :direct :one_day -> - calculate_next_day_notification(Date.utc_today(), timezone) + calculate_next_day_notification(Date.utc_today(), timezone, locale) :one_hour -> DateTime.utc_now() @@ -200,14 +201,22 @@ defmodule Mobilizon.Service.Notifications.Scheduler do else: calculate_first_day_of_week(Date.add(date, -1), locale) end - defp calculate_next_day_notification(%Date{} = day, timezone) do - {:ok, send_at} = NaiveDateTime.new(day, ~T[18:00:00]) - {:ok, send_at} = DateTime.from_naive(send_at, timezone) + defp calculate_next_day_notification(%Date{} = day, timezone, locale) do + send_at = date_to_datetime(day, ~T[18:00:00], timezone) 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 send_at 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