2021-03-23 15:18:03 +01:00
|
|
|
defmodule Mobilizon.Service.Notifier.Push do
|
|
|
|
@moduledoc """
|
|
|
|
WebPush notifier
|
|
|
|
"""
|
|
|
|
alias Mobilizon.Activities.Activity
|
2021-05-06 12:27:04 +02:00
|
|
|
alias Mobilizon.{Config, Users}
|
2021-05-06 18:39:59 +02:00
|
|
|
alias Mobilizon.Service.Activity.{Renderer, Utils}
|
2021-03-23 15:18:03 +01:00
|
|
|
alias Mobilizon.Service.Notifier
|
2021-06-01 18:08:03 +02:00
|
|
|
alias Mobilizon.Service.Notifier.{Filter, Push}
|
2021-05-06 12:27:04 +02:00
|
|
|
alias Mobilizon.Storage.Page
|
2021-05-06 18:39:59 +02:00
|
|
|
alias Mobilizon.Users.{PushSubscription, User}
|
2021-03-23 15:18:03 +01:00
|
|
|
|
|
|
|
@behaviour Notifier
|
|
|
|
|
|
|
|
@impl Notifier
|
|
|
|
def ready? do
|
2021-06-03 17:17:13 +02:00
|
|
|
Config.get([__MODULE__, :enabled])
|
2021-03-23 15:18:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl Notifier
|
2021-05-06 18:39:59 +02:00
|
|
|
def send(user, activity, options \\ [])
|
|
|
|
|
2021-06-01 18:08:03 +02:00
|
|
|
def send(%User{id: user_id, locale: locale} = user, %Activity{} = activity, options) do
|
|
|
|
if can_send_activity?(activity, user) do
|
|
|
|
options = Keyword.put_new(options, :locale, locale)
|
2021-05-06 18:39:59 +02:00
|
|
|
|
2021-06-01 18:08:03 +02:00
|
|
|
%Page{elements: subscriptions} = Users.list_user_push_subscriptions(user_id, 1, 100)
|
|
|
|
Enum.each(subscriptions, &send_subscription(activity, convert_subscription(&1), options))
|
|
|
|
{:ok, :sent}
|
|
|
|
else
|
|
|
|
{:ok, :skipped}
|
|
|
|
end
|
2021-03-23 15:18:03 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl Notifier
|
2021-05-06 18:39:59 +02:00
|
|
|
def send(%User{} = user, activities, options) when is_list(activities) do
|
|
|
|
Enum.map(activities, &Push.send(user, &1, options))
|
2021-03-23 15:18:03 +01:00
|
|
|
end
|
|
|
|
|
2021-06-01 18:08:03 +02:00
|
|
|
@spec can_send_activity?(Activity.t(), User.t()) :: boolean()
|
|
|
|
defp can_send_activity?(%Activity{} = activity, %User{} = user) do
|
|
|
|
Filter.can_send_activity?(activity, "push", user, &default_activity_behavior/1)
|
|
|
|
end
|
|
|
|
|
2021-06-03 15:00:49 +02:00
|
|
|
@default_behavior %{
|
|
|
|
"participation_event_updated" => true,
|
|
|
|
"participation_event_comment" => true,
|
|
|
|
"event_new_pending_participation" => true,
|
|
|
|
"event_new_participation" => false,
|
|
|
|
"event_created" => false,
|
|
|
|
"event_updated" => false,
|
|
|
|
"discussion_updated" => false,
|
|
|
|
"post_published" => false,
|
|
|
|
"post_updated" => false,
|
|
|
|
"resource_updated" => false,
|
|
|
|
"member_request" => true,
|
|
|
|
"member_updated" => false,
|
|
|
|
"user_email_password_updated" => false,
|
|
|
|
"event_comment_mention" => true,
|
|
|
|
"discussion_mention" => false,
|
|
|
|
"event_new_comment" => false
|
|
|
|
}
|
|
|
|
|
2021-06-01 18:08:03 +02:00
|
|
|
@spec default_activity_behavior(String.t()) :: boolean()
|
|
|
|
defp default_activity_behavior(activity_setting) do
|
2021-06-03 15:00:49 +02:00
|
|
|
Map.get(@default_behavior, activity_setting, false)
|
2021-06-01 18:08:03 +02:00
|
|
|
end
|
|
|
|
|
2021-05-06 18:39:59 +02:00
|
|
|
defp send_subscription(activity, subscription, options) do
|
|
|
|
activity
|
|
|
|
|> payload(options)
|
|
|
|
|> WebPushEncryption.send_web_push(subscription)
|
2021-03-23 15:18:03 +01:00
|
|
|
end
|
2021-05-06 12:27:04 +02:00
|
|
|
|
2021-05-06 18:39:59 +02:00
|
|
|
defp payload(%Activity{} = activity, options) do
|
2021-05-06 12:27:04 +02:00
|
|
|
activity
|
2021-05-06 18:39:59 +02:00
|
|
|
|> Utils.add_activity_object()
|
|
|
|
|> Renderer.render(options)
|
|
|
|
|> Jason.encode!()
|
|
|
|
end
|
|
|
|
|
|
|
|
defp convert_subscription(%PushSubscription{} = subscription) do
|
|
|
|
%{
|
|
|
|
endpoint: subscription.endpoint,
|
|
|
|
keys: %{auth: subscription.auth, p256dh: subscription.p256dh}
|
|
|
|
}
|
2021-05-06 12:27:04 +02:00
|
|
|
end
|
2021-03-23 15:18:03 +01:00
|
|
|
end
|