2021-03-23 14:18:03 +00:00
|
|
|
defmodule Mobilizon.Service.Notifier.Push do
|
|
|
|
@moduledoc """
|
|
|
|
WebPush notifier
|
|
|
|
"""
|
|
|
|
alias Mobilizon.Activities.Activity
|
2021-05-06 10:27:04 +00:00
|
|
|
alias Mobilizon.{Config, Users}
|
2021-05-06 16:39:59 +00:00
|
|
|
alias Mobilizon.Service.Activity.{Renderer, Utils}
|
2021-03-23 14:18:03 +00:00
|
|
|
alias Mobilizon.Service.Notifier
|
2021-06-01 16:08:03 +00:00
|
|
|
alias Mobilizon.Service.Notifier.{Filter, Push}
|
2021-05-06 10:27:04 +00:00
|
|
|
alias Mobilizon.Storage.Page
|
2021-05-06 16:39:59 +00:00
|
|
|
alias Mobilizon.Users.{PushSubscription, User}
|
2021-03-23 14:18:03 +00:00
|
|
|
|
|
|
|
@behaviour Notifier
|
|
|
|
|
|
|
|
@impl Notifier
|
|
|
|
def ready? do
|
2021-06-03 15:17:13 +00:00
|
|
|
Config.get([__MODULE__, :enabled])
|
2021-03-23 14:18:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl Notifier
|
2021-05-06 16:39:59 +00:00
|
|
|
def send(user, activity, options \\ [])
|
|
|
|
|
2021-06-01 16:08:03 +00: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 16:39:59 +00:00
|
|
|
|
2021-06-01 16:08:03 +00: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 14:18:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@impl Notifier
|
2021-05-06 16:39:59 +00:00
|
|
|
def send(%User{} = user, activities, options) when is_list(activities) do
|
|
|
|
Enum.map(activities, &Push.send(user, &1, options))
|
2021-03-23 14:18:03 +00:00
|
|
|
end
|
|
|
|
|
2021-06-01 16:08:03 +00: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 13:00:49 +00: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 16:08:03 +00:00
|
|
|
@spec default_activity_behavior(String.t()) :: boolean()
|
|
|
|
defp default_activity_behavior(activity_setting) do
|
2021-06-03 13:00:49 +00:00
|
|
|
Map.get(@default_behavior, activity_setting, false)
|
2021-06-01 16:08:03 +00:00
|
|
|
end
|
|
|
|
|
2021-05-06 16:39:59 +00:00
|
|
|
defp send_subscription(activity, subscription, options) do
|
|
|
|
activity
|
|
|
|
|> payload(options)
|
|
|
|
|> WebPushEncryption.send_web_push(subscription)
|
2021-03-23 14:18:03 +00:00
|
|
|
end
|
2021-05-06 10:27:04 +00:00
|
|
|
|
2021-05-06 16:39:59 +00:00
|
|
|
defp payload(%Activity{} = activity, options) do
|
2021-05-06 10:27:04 +00:00
|
|
|
activity
|
2021-05-06 16:39:59 +00: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 10:27:04 +00:00
|
|
|
end
|
2021-03-23 14:18:03 +00:00
|
|
|
end
|