2020-02-18 08:57:00 +01:00
|
|
|
defmodule Mobilizon.Service.Workers.Notification do
|
|
|
|
@moduledoc """
|
|
|
|
Worker to send notifications
|
|
|
|
"""
|
|
|
|
|
2021-10-18 11:09:27 +02:00
|
|
|
alias Mobilizon.{Actors, Events, Users}
|
2020-02-18 08:57:00 +01:00
|
|
|
alias Mobilizon.Actors.Actor
|
|
|
|
alias Mobilizon.Events.{Event, Participant}
|
|
|
|
alias Mobilizon.Storage.Page
|
|
|
|
alias Mobilizon.Users.{Setting, User}
|
|
|
|
alias Mobilizon.Web.Email.{Mailer, Notification}
|
2021-11-15 15:56:28 +01:00
|
|
|
require Logger
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2021-06-26 15:23:22 +02:00
|
|
|
import Mobilizon.Service.DateTime,
|
|
|
|
only: [
|
|
|
|
datetime_tz_convert: 2
|
|
|
|
]
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
use Mobilizon.Service.Workers.Helper, queue: "mailers"
|
|
|
|
|
|
|
|
@impl Oban.Worker
|
2020-08-12 14:34:19 +02:00
|
|
|
def perform(%Job{
|
|
|
|
args: %{"op" => "before_event_notification", "participant_id" => participant_id}
|
|
|
|
}) do
|
2021-10-18 11:09:27 +02:00
|
|
|
with %Participant{} = participant <- Events.get_participant(participant_id),
|
|
|
|
%Event{status: :confirmed} = event <-
|
|
|
|
Events.get_event_with_preload!(participant.event_id),
|
|
|
|
%Actor{user_id: user_id} = actor when not is_nil(user_id) <-
|
|
|
|
Actors.get_actor_with_preload!(participant.actor_id) do
|
|
|
|
%User{email: email, locale: locale, settings: %Setting{notification_before_event: true}} =
|
|
|
|
Users.get_user_with_settings!(user_id)
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
email
|
2021-10-18 11:09:27 +02:00
|
|
|
|> Notification.before_event_notification(
|
|
|
|
%Participant{participant | event: event, actor: actor},
|
|
|
|
locale
|
|
|
|
)
|
2021-04-20 15:02:24 +02:00
|
|
|
|> Mailer.send_email_later()
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-12 14:34:19 +02:00
|
|
|
def perform(%Job{
|
|
|
|
args: %{"op" => "on_day_notification", "user_id" => user_id}
|
|
|
|
}) do
|
2020-06-05 10:12:08 +02:00
|
|
|
with %User{locale: locale, settings: %Setting{timezone: timezone, notification_on_day: true}} =
|
|
|
|
user <- Users.get_user_with_settings!(user_id),
|
2022-03-29 10:38:50 +02:00
|
|
|
{start, tomorrow} <- calculate_start_end(1, timezone || "Etc/UTC"),
|
2020-06-05 10:12:08 +02:00
|
|
|
%Page{
|
2020-02-18 08:57:00 +01:00
|
|
|
elements: participations,
|
|
|
|
total: total
|
2020-06-05 10:12:08 +02:00
|
|
|
}
|
|
|
|
when total > 0 <-
|
2020-02-18 08:57:00 +01:00
|
|
|
Events.list_participations_for_user(user_id, start, tomorrow, 1, 5),
|
2020-06-05 10:12:08 +02:00
|
|
|
participations <-
|
|
|
|
Enum.filter(participations, fn participation ->
|
2020-02-18 08:57:00 +01:00
|
|
|
participation.event.status == :confirmed
|
|
|
|
end),
|
2021-10-18 11:09:27 +02:00
|
|
|
true <- length(participations) > 0,
|
|
|
|
participations <-
|
|
|
|
Enum.map(participations, fn participation ->
|
|
|
|
%Event{} = event = Events.get_event_with_preload!(participation.event_id)
|
|
|
|
%Participant{participation | event: event}
|
|
|
|
end) do
|
2020-02-18 08:57:00 +01:00
|
|
|
user
|
|
|
|
|> Notification.on_day_notification(participations, total, locale)
|
2021-04-20 15:02:24 +02:00
|
|
|
|> Mailer.send_email_later()
|
2020-02-18 08:57:00 +01:00
|
|
|
|
|
|
|
:ok
|
2020-06-05 10:12:08 +02:00
|
|
|
else
|
|
|
|
_ -> :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-12 14:34:19 +02:00
|
|
|
def perform(%Job{
|
|
|
|
args: %{"op" => "weekly_notification", "user_id" => user_id}
|
|
|
|
}) do
|
2020-06-05 10:12:08 +02:00
|
|
|
with %User{
|
|
|
|
locale: locale,
|
|
|
|
settings: %Setting{timezone: timezone, notification_each_week: true}
|
|
|
|
} = user <- Users.get_user_with_settings!(user_id),
|
2022-03-29 10:38:50 +02:00
|
|
|
{start, end_week} <- calculate_start_end(7, timezone || "Etc/UTC"),
|
2020-06-05 10:12:08 +02:00
|
|
|
%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),
|
2021-10-18 11:09:27 +02:00
|
|
|
true <- length(participations) > 0,
|
|
|
|
participations <-
|
|
|
|
Enum.map(participations, fn participation ->
|
|
|
|
%Event{} = event = Events.get_event_with_preload!(participation.event_id)
|
|
|
|
%Participant{participation | event: event}
|
|
|
|
end) do
|
2020-06-05 10:12:08 +02:00
|
|
|
user
|
|
|
|
|> Notification.weekly_notification(participations, total, locale)
|
2021-04-20 15:02:24 +02:00
|
|
|
|> Mailer.send_email_later()
|
2020-06-05 10:12:08 +02:00
|
|
|
|
|
|
|
:ok
|
|
|
|
else
|
|
|
|
_err ->
|
|
|
|
:ok
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-12 14:34:19 +02:00
|
|
|
def perform(%Job{
|
|
|
|
args: %{
|
2020-06-08 12:28:19 +02:00
|
|
|
"op" => "pending_participation_notification",
|
|
|
|
"user_id" => user_id,
|
|
|
|
"event_id" => event_id
|
2020-08-12 14:34:19 +02:00
|
|
|
}
|
|
|
|
}) do
|
2021-11-15 15:56:28 +01:00
|
|
|
with %User{} = user <- Users.get_user_with_settings!(user_id),
|
2021-10-18 11:09:27 +02:00
|
|
|
{:ok, %Event{} = event} <- Events.get_event_with_preload(event_id),
|
2020-06-08 12:28:19 +02:00
|
|
|
%Page{total: total} when total > 0 <-
|
|
|
|
Events.list_participants_for_event(event_id, [:not_approved]) do
|
|
|
|
user
|
|
|
|
|> Notification.pending_participation_notification(event, total)
|
2021-04-20 15:02:24 +02:00
|
|
|
|> Mailer.send_email_later()
|
2020-12-07 14:48:48 +01:00
|
|
|
|
|
|
|
:ok
|
2020-06-08 12:28:19 +02:00
|
|
|
else
|
|
|
|
err ->
|
2020-07-09 17:24:28 +02:00
|
|
|
Logger.debug(inspect(err))
|
2021-11-15 15:56:28 +01:00
|
|
|
err
|
2020-06-08 12:28:19 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-05 10:12:08 +02:00
|
|
|
defp calculate_start_end(days, timezone) do
|
|
|
|
now = DateTime.utc_now()
|
2021-06-26 15:23:22 +02:00
|
|
|
%DateTime{} = now_shifted = datetime_tz_convert(now, timezone)
|
2020-06-05 10:12:08 +02:00
|
|
|
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
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|