Send Notifications when participation approval
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user