Send Notifications when participation approval

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2020-06-08 12:28:19 +02:00
parent 63efea7371
commit 3e74f59ee8
38 changed files with 2500 additions and 973 deletions

View File

@@ -3,10 +3,10 @@ defmodule Mobilizon.Service.Notifications.Scheduler do
Allows to insert jobs
"""
alias Mobilizon.{Actors, Users}
alias Mobilizon.Actors.Actor
alias Mobilizon.Events.{Event, Participant}
alias Mobilizon.Service.Workers.Notification
alias Mobilizon.Users
alias Mobilizon.Users.{Setting, User}
require Logger
@@ -129,6 +129,61 @@ defmodule Mobilizon.Service.Notifications.Scheduler do
def weekly_notification(_), do: {:ok, nil}
def pending_participation_notification(%Event{
id: event_id,
organizer_actor_id: organizer_actor_id,
local: true
}) do
with %Actor{user_id: user_id} when not is_nil(user_id) <-
Actors.get_actor(organizer_actor_id),
%User{
settings: %Setting{
notification_pending_participation: notification_pending_participation,
timezone: timezone
}
} <- Users.get_user_with_settings!(user_id) do
send_at =
case notification_pending_participation do
:none ->
nil
:direct ->
:direct
:one_day ->
calculate_next_day_notification(Date.utc_today(), timezone)
:one_hour ->
DateTime.utc_now()
|> DateTime.shift_zone!(timezone)
|> (&%{&1 | minute: 0, second: 0, microsecond: {0, 0}}).()
end
params = %{
user_id: user_id,
event_id: event_id
}
cond do
# Sending directly
send_at == :direct ->
Notification.enqueue(:pending_participation_notification, params)
# Not sending
is_nil(send_at) ->
{:ok, nil}
# Sending to calculated time
true ->
Notification.enqueue(:pending_participation_notification, params, scheduled_at: send_at)
end
else
_ -> {:ok, nil}
end
end
def pending_participation_notification(_), do: {:ok, nil}
defp shift_zone(datetime, timezone) do
case DateTime.shift_zone(datetime, timezone) do
{:ok, shift_datetime} -> shift_datetime
@@ -144,4 +199,15 @@ defmodule Mobilizon.Service.Notifications.Scheduler do
do: date,
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)
if send_at < DateTime.utc_now() do
calculate_first_day_of_week(Date.add(day, 1), timezone)
else
send_at
end
end
end