1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Push do
10 alias Pleroma.Activity
12 alias Pleroma.Web.Push.Subscription
13 alias Pleroma.Web.Metadata.Utils
18 @types ["Create", "Follow", "Announce", "Like"]
21 GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
25 Application.get_env(:web_push_encryption, :vapid_details, [])
29 case vapid_config() do
31 list when is_list(list) -> true
36 def send(notification) do
38 GenServer.cast(Pleroma.Web.Push, {:send, notification})
45 VAPID key pair is not found. If you wish to enabled web push, please run
47 mix web_push.gen.keypair
49 and add the resulting output to your configuration file.
59 {:send, %{activity: %{data: %{"type" => type}}, user_id: user_id} = notification},
62 when type in @types do
63 actor = User.get_cached_by_ap_id(notification.activity.data["actor"])
65 type = Pleroma.Activity.mastodon_notification_type(notification.activity)
68 |> where(user_id: ^user_id)
71 |> Enum.filter(fn subscription ->
72 get_in(subscription.data, ["alerts", type]) || false
74 |> Enum.each(fn subscription ->
77 p256dh: subscription.key_p256dh,
78 auth: subscription.key_auth
80 endpoint: subscription.endpoint
85 title: format_title(notification),
86 access_token: subscription.token.token,
87 body: format_body(notification, actor),
88 notification_id: notification.id,
89 notification_type: type,
90 icon: User.avatar_url(actor),
91 preferred_locale: "en"
94 case WebPushEncryption.send_web_push(
97 Application.get_env(:web_push_encryption, :gcm_api_key)
99 {:ok, %{status_code: code}} when 400 <= code and code < 500 ->
100 Logger.debug("Removing subscription record")
101 Repo.delete!(subscription)
104 {:ok, %{status_code: code}} when 200 <= code and code < 300 ->
107 {:ok, %{status_code: code}} ->
108 Logger.error("Web Push Notification failed with code: #{code}")
112 Logger.error("Web Push Notification failed with unknown error")
120 def handle_cast({:send, _}, state) do
121 Logger.warn("Unknown notification type")
125 # Pleroma.Repo.all(Pleroma.Object)
126 # Pleroma.Repo.all(Pleroma.Activity)
129 %{activity: %{data: %{"type" => "Create", "object" => %{"content" => content}}}},
132 "@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}"
136 %{activity: %{data: %{"type" => "Announce", "object" => activity_id}}},
139 %Activity{data: %{"object" => %{"id" => object_id}}} = Activity.get_by_ap_id(activity_id)
140 %Object{data: %{"content" => content}} = Object.get_by_ap_id(object_id)
142 "@#{actor.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}"
146 %{activity: %{data: %{"type" => type}}},
149 when type in ["Follow", "Like"] do
151 "Follow" -> "@#{actor.nickname} has followed you"
152 "Like" -> "@#{actor.nickname} has favorited your post"
156 defp format_title(%{activity: %{data: %{"type" => type}}}) do
158 "Create" -> "New Mention"
159 "Follow" -> "New Follower"
160 "Announce" -> "New Repeat"
161 "Like" -> "New Favorite"