Add webpush front-end support
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
83
lib/service/activity/renderer/discussion.ex
Normal file
83
lib/service/activity/renderer/discussion.ex
Normal file
@@ -0,0 +1,83 @@
|
||||
defmodule Mobilizon.Service.Activity.Renderer.Discussion do
|
||||
@moduledoc """
|
||||
Insert a comment activity
|
||||
"""
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Service.Activity.Renderer
|
||||
alias Mobilizon.Web.{Endpoint, Gettext}
|
||||
alias Mobilizon.Web.Router.Helpers, as: Routes
|
||||
import Mobilizon.Web.Gettext, only: [dgettext: 3]
|
||||
|
||||
@behaviour Renderer
|
||||
|
||||
@impl Renderer
|
||||
def render(%Activity{} = activity, options) do
|
||||
locale = Keyword.get(options, :locale, "en")
|
||||
Gettext.put_locale(locale)
|
||||
|
||||
case activity.subject do
|
||||
:discussion_created ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} created the discussion %{discussion}.", %{
|
||||
profile: profile(activity),
|
||||
discussion: title(activity)
|
||||
}),
|
||||
url: discussion_url(activity)
|
||||
}
|
||||
|
||||
:discussion_replied ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} replied to the discussion %{discussion}.", %{
|
||||
profile: profile(activity),
|
||||
discussion: title(activity)
|
||||
}),
|
||||
url: discussion_url(activity)
|
||||
}
|
||||
|
||||
:discussion_renamed ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} renamed the discussion %{discussion}.", %{
|
||||
profile: profile(activity),
|
||||
discussion: title(activity)
|
||||
}),
|
||||
url: discussion_url(activity)
|
||||
}
|
||||
|
||||
:discussion_archived ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} archived the discussion %{discussion}.", %{
|
||||
profile: profile(activity),
|
||||
discussion: title(activity)
|
||||
}),
|
||||
url: discussion_url(activity)
|
||||
}
|
||||
|
||||
:discussion_deleted ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} deleted the discussion %{discussion}.", %{
|
||||
profile: profile(activity),
|
||||
discussion: title(activity)
|
||||
}),
|
||||
url: nil
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
defp discussion_url(activity) do
|
||||
Routes.page_url(
|
||||
Endpoint,
|
||||
:discussion,
|
||||
Actor.preferred_username_and_domain(activity.group),
|
||||
activity.subject_params["discussion_slug"]
|
||||
)
|
||||
end
|
||||
|
||||
defp profile(activity), do: Actor.display_name_and_username(activity.author)
|
||||
defp title(activity), do: activity.subject_params["discussion_title"]
|
||||
end
|
||||
83
lib/service/activity/renderer/event.ex
Normal file
83
lib/service/activity/renderer/event.ex
Normal file
@@ -0,0 +1,83 @@
|
||||
defmodule Mobilizon.Service.Activity.Renderer.Event do
|
||||
@moduledoc """
|
||||
Insert a comment activity
|
||||
"""
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Service.Activity.Renderer
|
||||
alias Mobilizon.Web.{Endpoint, Gettext}
|
||||
alias Mobilizon.Web.Router.Helpers, as: Routes
|
||||
import Mobilizon.Web.Gettext, only: [dgettext: 3]
|
||||
|
||||
@behaviour Renderer
|
||||
|
||||
@impl Renderer
|
||||
def render(%Activity{} = activity, options) do
|
||||
locale = Keyword.get(options, :locale, "en")
|
||||
Gettext.put_locale(locale)
|
||||
|
||||
case activity.subject do
|
||||
:event_created ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "The event %{event} was created by %{profile}.", %{
|
||||
profile: profile(activity),
|
||||
event: title(activity)
|
||||
}),
|
||||
url: event_url(activity)
|
||||
}
|
||||
|
||||
:event_updated ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "The event %{event} was updated by %{profile}.", %{
|
||||
profile: profile(activity),
|
||||
event: title(activity)
|
||||
}),
|
||||
url: event_url(activity)
|
||||
}
|
||||
|
||||
:event_deleted ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "The event %{event} was deleted by %{profile}.", %{
|
||||
profile: profile(activity),
|
||||
event: title(activity)
|
||||
}),
|
||||
url: nil
|
||||
}
|
||||
|
||||
:comment_posted ->
|
||||
if activity.subject_params["comment_reply_to"] do
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} replied to a comment on the event %{event}.", %{
|
||||
profile: profile(activity),
|
||||
event: title(activity)
|
||||
}),
|
||||
url: event_url(activity)
|
||||
}
|
||||
else
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} posted a comment on the event %{event}.", %{
|
||||
profile: profile(activity),
|
||||
event: title(activity)
|
||||
}),
|
||||
url: event_url(activity)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp event_url(activity) do
|
||||
Routes.page_url(
|
||||
Endpoint,
|
||||
:event,
|
||||
activity.subject_params["event_uuid"]
|
||||
)
|
||||
end
|
||||
|
||||
defp profile(activity), do: Actor.display_name_and_username(activity.author)
|
||||
defp title(activity), do: activity.subject_params["event_title"]
|
||||
end
|
||||
58
lib/service/activity/renderer/group.ex
Normal file
58
lib/service/activity/renderer/group.ex
Normal file
@@ -0,0 +1,58 @@
|
||||
defmodule Mobilizon.Service.Activity.Renderer.Group do
|
||||
@moduledoc """
|
||||
Insert a comment activity
|
||||
"""
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Service.Activity.Renderer
|
||||
alias Mobilizon.Web.{Endpoint, Gettext}
|
||||
alias Mobilizon.Web.Router.Helpers, as: Routes
|
||||
import Mobilizon.Web.Gettext, only: [dgettext: 3]
|
||||
|
||||
@behaviour Renderer
|
||||
|
||||
@impl Renderer
|
||||
def render(%Activity{} = activity, options) do
|
||||
locale = Keyword.get(options, :locale, "en")
|
||||
Gettext.put_locale(locale)
|
||||
|
||||
case activity.subject do
|
||||
:post_created ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "The post %{post} was created by %{profile}.", %{
|
||||
profile: profile(activity),
|
||||
post: title(activity)
|
||||
}),
|
||||
url: post_url(activity)
|
||||
}
|
||||
|
||||
:post_updated ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "The post %{post} was updated by %{profile}.", %{
|
||||
profile: profile(activity),
|
||||
post: title(activity)
|
||||
}),
|
||||
url: post_url(activity)
|
||||
}
|
||||
|
||||
:post_deleted ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "The post %{post} was deleted by %{profile}.", %{
|
||||
profile: profile(activity),
|
||||
post: title(activity)
|
||||
}),
|
||||
url: post_url(activity)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
defp post_url(activity) do
|
||||
Routes.page_url(Endpoint, :post, activity.subject_params["post_slug"])
|
||||
end
|
||||
|
||||
defp profile(activity), do: Actor.display_name_and_username(activity.author)
|
||||
defp title(activity), do: activity.subject_params["post_title"]
|
||||
end
|
||||
122
lib/service/activity/renderer/member.ex
Normal file
122
lib/service/activity/renderer/member.ex
Normal file
@@ -0,0 +1,122 @@
|
||||
defmodule Mobilizon.Service.Activity.Renderer.Member do
|
||||
@moduledoc """
|
||||
Insert a comment activity
|
||||
"""
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Service.Activity.Renderer
|
||||
alias Mobilizon.Web.{Endpoint, Gettext}
|
||||
alias Mobilizon.Web.Router.Helpers, as: Routes
|
||||
import Mobilizon.Web.Gettext, only: [dgettext: 3]
|
||||
|
||||
@behaviour Renderer
|
||||
|
||||
@impl Renderer
|
||||
def render(%Activity{} = activity, options) do
|
||||
locale = Keyword.get(options, :locale, "en")
|
||||
Gettext.put_locale(locale)
|
||||
|
||||
case activity.subject do
|
||||
:member_request ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{member} requested to join the group.", %{
|
||||
profile: profile(activity),
|
||||
member: title(activity)
|
||||
}),
|
||||
url: member_url(activity)
|
||||
}
|
||||
|
||||
:member_invited ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{member} was invited by %{profile}.", %{
|
||||
profile: profile(activity),
|
||||
member: title(activity)
|
||||
}),
|
||||
url: member_url(activity)
|
||||
}
|
||||
|
||||
:member_accepted_invitation ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{member} accepted the invitation to join the group.", %{
|
||||
profile: profile(activity),
|
||||
member: title(activity)
|
||||
}),
|
||||
url: member_url(activity)
|
||||
}
|
||||
|
||||
:member_rejected_invitation ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{member} rejected the invitation to join the group.", %{
|
||||
profile: profile(activity),
|
||||
member: title(activity)
|
||||
}),
|
||||
url: member_url(activity)
|
||||
}
|
||||
|
||||
:member_joined ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{member} joined the group.", %{
|
||||
member: title(activity)
|
||||
}),
|
||||
url: member_url(activity)
|
||||
}
|
||||
|
||||
:member_added ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} added the member %{member}.", %{
|
||||
profile: profile(activity),
|
||||
member: title(activity)
|
||||
}),
|
||||
url: member_url(activity)
|
||||
}
|
||||
|
||||
:member_updated ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} updated the member %{member}.", %{
|
||||
profile: profile(activity),
|
||||
member: title(activity)
|
||||
}),
|
||||
url: member_url(activity)
|
||||
}
|
||||
|
||||
:member_removed ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} excluded member %{member}.", %{
|
||||
profile: profile(activity),
|
||||
member: title(activity)
|
||||
}),
|
||||
url: member_url(activity)
|
||||
}
|
||||
|
||||
:member_quit ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} quit the group.", %{
|
||||
profile: profile(activity),
|
||||
member: title(activity)
|
||||
}),
|
||||
url: member_url(activity)
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
defp member_url(activity) do
|
||||
Routes.page_url(
|
||||
Endpoint,
|
||||
:discussion,
|
||||
Actor.preferred_username_and_domain(activity.group),
|
||||
activity.subject_params["discussion_slug"]
|
||||
)
|
||||
end
|
||||
|
||||
defp profile(activity), do: Actor.display_name_and_username(activity.author)
|
||||
defp title(activity), do: activity.subject_params["discussion_title"]
|
||||
end
|
||||
83
lib/service/activity/renderer/post.ex
Normal file
83
lib/service/activity/renderer/post.ex
Normal file
@@ -0,0 +1,83 @@
|
||||
defmodule Mobilizon.Service.Activity.Renderer.Post do
|
||||
@moduledoc """
|
||||
Insert a comment activity
|
||||
"""
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Service.Activity.Renderer
|
||||
alias Mobilizon.Web.{Endpoint, Gettext}
|
||||
alias Mobilizon.Web.Router.Helpers, as: Routes
|
||||
import Mobilizon.Web.Gettext, only: [dgettext: 3]
|
||||
|
||||
@behaviour Renderer
|
||||
|
||||
@impl Renderer
|
||||
def render(%Activity{} = activity, options) do
|
||||
locale = Keyword.get(options, :locale, "en")
|
||||
Gettext.put_locale(locale)
|
||||
|
||||
case activity.subject do
|
||||
:discussion_created ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} created the discussion %{discussion}.", %{
|
||||
profile: profile(activity),
|
||||
discussion: title(activity)
|
||||
}),
|
||||
url: discussion_url(activity)
|
||||
}
|
||||
|
||||
:discussion_replied ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} replied to the discussion %{discussion}.", %{
|
||||
profile: profile(activity),
|
||||
discussion: title(activity)
|
||||
}),
|
||||
url: discussion_url(activity)
|
||||
}
|
||||
|
||||
:discussion_renamed ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} renamed the discussion %{discussion}.", %{
|
||||
profile: profile(activity),
|
||||
discussion: title(activity)
|
||||
}),
|
||||
url: discussion_url(activity)
|
||||
}
|
||||
|
||||
:discussion_archived ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} archived the discussion %{discussion}.", %{
|
||||
profile: profile(activity),
|
||||
discussion: title(activity)
|
||||
}),
|
||||
url: discussion_url(activity)
|
||||
}
|
||||
|
||||
:discussion_deleted ->
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} deleted the discussion %{discussion}.", %{
|
||||
profile: profile(activity),
|
||||
discussion: title(activity)
|
||||
}),
|
||||
url: nil
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
defp discussion_url(activity) do
|
||||
Routes.page_url(
|
||||
Endpoint,
|
||||
:discussion,
|
||||
Actor.preferred_username_and_domain(activity.group),
|
||||
activity.subject_params["discussion_slug"]
|
||||
)
|
||||
end
|
||||
|
||||
defp profile(activity), do: Actor.display_name_and_username(activity.author)
|
||||
defp title(activity), do: activity.subject_params["discussion_title"]
|
||||
end
|
||||
47
lib/service/activity/renderer/renderer.ex
Normal file
47
lib/service/activity/renderer/renderer.ex
Normal file
@@ -0,0 +1,47 @@
|
||||
defmodule Mobilizon.Service.Activity.Renderer do
|
||||
@moduledoc """
|
||||
Behavior for Activity renderers
|
||||
"""
|
||||
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.Service.Activity.Renderer.{Discussion, Event, Group, Member, Post, Resource}
|
||||
require Logger
|
||||
import Mobilizon.Web.Gettext, only: [dgettext: 3]
|
||||
|
||||
@type render :: %{body: String.t(), url: String.t()}
|
||||
|
||||
@callback render(entity :: Activity.t(), Keyword.t()) :: render()
|
||||
|
||||
@spec render(Activity.t()) :: render()
|
||||
def render(%Activity{} = activity, options \\ []) do
|
||||
locale = Keyword.get(options, :locale, "en")
|
||||
Gettext.put_locale(locale)
|
||||
|
||||
res =
|
||||
activity
|
||||
|> do_render(options)
|
||||
|> Map.put(:timestamp, DateTime.utc_now() |> DateTime.to_iso8601())
|
||||
|> Map.put(:locale, Keyword.get(options, :locale, "en"))
|
||||
|> Map.put(
|
||||
:title,
|
||||
dgettext("activity", "Activity on %{instance}", %{instance: Config.instance_name()})
|
||||
)
|
||||
|
||||
Logger.debug("notification to be sent")
|
||||
Logger.debug(inspect(res))
|
||||
res
|
||||
end
|
||||
|
||||
defp do_render(%Activity{type: type} = activity, options) do
|
||||
case type do
|
||||
:discussion -> Discussion.render(activity, options)
|
||||
:event -> Event.render(activity, options)
|
||||
:group -> Group.render(activity, options)
|
||||
:member -> Member.render(activity, options)
|
||||
:post -> Post.render(activity, options)
|
||||
:resource -> Resource.render(activity, options)
|
||||
_ -> nil
|
||||
end
|
||||
end
|
||||
end
|
||||
122
lib/service/activity/renderer/resource.ex
Normal file
122
lib/service/activity/renderer/resource.ex
Normal file
@@ -0,0 +1,122 @@
|
||||
defmodule Mobilizon.Service.Activity.Renderer.Resource do
|
||||
@moduledoc """
|
||||
Insert a comment activity
|
||||
"""
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Service.Activity.Renderer
|
||||
alias Mobilizon.Web.{Endpoint, Gettext}
|
||||
alias Mobilizon.Web.Router.Helpers, as: Routes
|
||||
import Mobilizon.Web.Gettext, only: [dgettext: 3]
|
||||
|
||||
@behaviour Renderer
|
||||
|
||||
@impl Renderer
|
||||
def render(%Activity{} = activity, options) do
|
||||
locale = Keyword.get(options, :locale, "en")
|
||||
Gettext.put_locale(locale)
|
||||
|
||||
case activity.subject do
|
||||
:resource_created ->
|
||||
if activity.subject_params["is_folder"] do
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} created the folder %{resource}.", %{
|
||||
profile: profile(activity),
|
||||
resource: title(activity)
|
||||
}),
|
||||
url: resource_url(activity)
|
||||
}
|
||||
else
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} created the resource %{resource}.", %{
|
||||
profile: profile(activity),
|
||||
resource: title(activity)
|
||||
}),
|
||||
url: resource_url(activity)
|
||||
}
|
||||
end
|
||||
|
||||
:resource_renamed ->
|
||||
if activity.subject_params["is_folder"] do
|
||||
%{
|
||||
body:
|
||||
dgettext(
|
||||
"activity",
|
||||
"%{profile} renamed the folder from %{old_resource_title} to %{resource}.",
|
||||
%{
|
||||
profile: profile(activity),
|
||||
resource: title(activity),
|
||||
old_resource_title: activity.subject_params["old_resource_title"]
|
||||
}
|
||||
),
|
||||
url: resource_url(activity)
|
||||
}
|
||||
else
|
||||
%{
|
||||
body:
|
||||
dgettext(
|
||||
"activity",
|
||||
"%{profile} renamed the resource from %{old_resource_title} to %{resource}.",
|
||||
%{
|
||||
profile: profile(activity),
|
||||
resource: title(activity),
|
||||
old_resource_title: activity.subject_params["old_resource_title"]
|
||||
}
|
||||
),
|
||||
url: resource_url(activity)
|
||||
}
|
||||
end
|
||||
|
||||
:resource_moved ->
|
||||
if activity.subject_params["is_folder"] do
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} moved the folder %{resource}.", %{
|
||||
profile: profile(activity),
|
||||
resource: title(activity)
|
||||
}),
|
||||
url: resource_url(activity)
|
||||
}
|
||||
else
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} moved the resource %{resource}.", %{
|
||||
profile: profile(activity),
|
||||
resource: title(activity)
|
||||
}),
|
||||
url: resource_url(activity)
|
||||
}
|
||||
end
|
||||
|
||||
:resource_deleted ->
|
||||
if activity.subject_params["is_folder"] do
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} deleted the folder %{resource}.", %{
|
||||
profile: profile(activity),
|
||||
resource: title(activity)
|
||||
}),
|
||||
url: resource_url(activity)
|
||||
}
|
||||
else
|
||||
%{
|
||||
body:
|
||||
dgettext("activity", "%{profile} deleted the resource %{resource}.", %{
|
||||
profile: profile(activity),
|
||||
resource: title(activity)
|
||||
}),
|
||||
url: resource_url(activity)
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp resource_url(activity) do
|
||||
Routes.page_url(Endpoint, :resource, activity.subject_params["resource_uuid"])
|
||||
end
|
||||
|
||||
defp profile(activity), do: Actor.display_name_and_username(activity.author)
|
||||
defp title(activity), do: activity.subject_params["resource_title"]
|
||||
end
|
||||
30
lib/service/activity/utils.ex
Normal file
30
lib/service/activity/utils.ex
Normal file
@@ -0,0 +1,30 @@
|
||||
defmodule Mobilizon.Service.Activity.Utils do
|
||||
@moduledoc """
|
||||
Utils for activities
|
||||
"""
|
||||
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.Service.Activity, as: ActivityService
|
||||
|
||||
def transform_activity(%Activity{} = activity) do
|
||||
activity
|
||||
|> Map.update(:subject_params, %{}, &transform_params/1)
|
||||
|> add_activity_object()
|
||||
end
|
||||
|
||||
@spec add_activity_object(Activity.t()) :: Activity.t()
|
||||
def add_activity_object(%Activity{} = activity) do
|
||||
Map.put(activity, :object, ActivityService.object(activity))
|
||||
end
|
||||
|
||||
@spec transform_params(map()) :: list()
|
||||
defp transform_params(params) do
|
||||
Enum.map(params, fn {key, value} -> %{key: key, value: transform_value(value)} end)
|
||||
end
|
||||
|
||||
defp transform_value(value) when is_list(value) do
|
||||
Enum.join(value, ",")
|
||||
end
|
||||
|
||||
defp transform_value(value), do: value
|
||||
end
|
||||
@@ -4,10 +4,11 @@ defmodule Mobilizon.Service.Notifier.Push do
|
||||
"""
|
||||
alias Mobilizon.Activities.Activity
|
||||
alias Mobilizon.{Config, Users}
|
||||
alias Mobilizon.Service.Activity.{Renderer, Utils}
|
||||
alias Mobilizon.Service.Notifier
|
||||
alias Mobilizon.Service.Notifier.Push
|
||||
alias Mobilizon.Storage.Page
|
||||
alias Mobilizon.Users.User
|
||||
alias Mobilizon.Users.{PushSubscription, User}
|
||||
|
||||
@behaviour Notifier
|
||||
|
||||
@@ -17,26 +18,37 @@ defmodule Mobilizon.Service.Notifier.Push do
|
||||
end
|
||||
|
||||
@impl Notifier
|
||||
def send(%User{id: user_id} = _user, %Activity{} = activity, _opts) do
|
||||
def send(user, activity, options \\ [])
|
||||
|
||||
def send(%User{id: user_id, locale: locale} = _user, %Activity{} = activity, options) do
|
||||
options = Keyword.put_new(options, :locale, locale)
|
||||
|
||||
%Page{elements: subscriptions} = Users.list_user_push_subscriptions(user_id, 1, 100)
|
||||
Enum.each(subscriptions, &send_subscription(activity, &1))
|
||||
Enum.map(subscriptions, &send_subscription(activity, convert_subscription(&1), options))
|
||||
end
|
||||
|
||||
@impl Notifier
|
||||
def send(%User{} = user, activities, opts) when is_list(activities) do
|
||||
Enum.each(activities, &Push.send(user, &1, opts))
|
||||
def send(%User{} = user, activities, options) when is_list(activities) do
|
||||
Enum.map(activities, &Push.send(user, &1, options))
|
||||
end
|
||||
|
||||
defp payload(%Activity{subject: subject}) do
|
||||
%{
|
||||
title: subject
|
||||
}
|
||||
defp send_subscription(activity, subscription, options) do
|
||||
activity
|
||||
|> payload(options)
|
||||
|> WebPushEncryption.send_web_push(subscription)
|
||||
end
|
||||
|
||||
defp payload(%Activity{} = activity, options) do
|
||||
activity
|
||||
|> Utils.add_activity_object()
|
||||
|> Renderer.render(options)
|
||||
|> Jason.encode!()
|
||||
end
|
||||
|
||||
defp send_subscription(activity, subscription) do
|
||||
activity
|
||||
|> payload()
|
||||
|> WebPushEncryption.send_web_push(subscription)
|
||||
defp convert_subscription(%PushSubscription{} = subscription) do
|
||||
%{
|
||||
endpoint: subscription.endpoint,
|
||||
keys: %{auth: subscription.auth, p256dh: subscription.p256dh}
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user