Properly disable Web Push if no VAPID key is set
[akkoma] / lib / pleroma / web / push / push.ex
1 defmodule Pleroma.Web.Push do
2 use GenServer
3
4 alias Pleroma.{Repo, User}
5 alias Pleroma.Web.Push.Subscription
6
7 require Logger
8 import Ecto.Query
9
10 @types ["Create", "Follow", "Announce", "Like"]
11
12 def start_link() do
13 GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
14 end
15
16 def vapid_config() do
17 Application.get_env(:web_push_encryption, :vapid_details, [])
18 end
19
20 def enabled() do
21 case vapid_config() do
22 [] -> false
23 list when is_list(list) -> true
24 _ -> false
25 end
26 end
27
28 def send(notification) do
29 if enabled() do
30 GenServer.cast(Pleroma.Web.Push, {:send, notification})
31 end
32 end
33
34 def init(:ok) do
35 if enabled() do
36 Logger.warn("""
37 VAPID key pair is not found. If you wish to enabled web push, please run
38
39 mix web_push.gen.keypair
40
41 and add the resulting output to your configuration file.
42 """)
43
44 :ignore
45 else
46 {:ok, nil}
47 end
48 end
49
50 def handle_cast(
51 {:send, %{activity: %{data: %{"type" => type}}, user_id: user_id} = notification},
52 state
53 )
54 when type in @types do
55 actor = User.get_cached_by_ap_id(notification.activity.data["actor"])
56
57 type = format_type(notification)
58
59 Subscription
60 |> where(user_id: ^user_id)
61 |> preload(:token)
62 |> Repo.all()
63 |> Enum.filter(fn subscription ->
64 get_in(subscription.data, ["alerts", type]) || false
65 end)
66 |> Enum.each(fn subscription ->
67 sub = %{
68 keys: %{
69 p256dh: subscription.key_p256dh,
70 auth: subscription.key_auth
71 },
72 endpoint: subscription.endpoint
73 }
74
75 body =
76 Jason.encode!(%{
77 title: format_title(notification),
78 access_token: subscription.token.token,
79 body: format_body(notification, actor),
80 notification_id: notification.id,
81 notification_type: type,
82 icon: User.avatar_url(actor),
83 preferred_locale: "en"
84 })
85
86 case WebPushEncryption.send_web_push(
87 body,
88 sub,
89 Application.get_env(:web_push_encryption, :gcm_api_key)
90 ) do
91 {:ok, %{status_code: code}} when 400 <= code and code < 500 ->
92 Logger.debug("Removing subscription record")
93 Repo.delete!(subscription)
94 :ok
95
96 {:ok, %{status_code: code}} when 200 <= code and code < 300 ->
97 :ok
98
99 {:ok, %{status_code: code}} ->
100 Logger.error("Web Push Notification failed with code: #{code}")
101 :error
102
103 _ ->
104 Logger.error("Web Push Notification failed with unknown error")
105 :error
106 end
107 end)
108
109 {:noreply, state}
110 end
111
112 def handle_cast({:send, _}, state) do
113 Logger.warn("Unknown notification type")
114 {:noreply, state}
115 end
116
117 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
118 defp format_type(%{activity: %{data: %{"type" => type}}}) do
119 case type do
120 "Create" -> "mention"
121 "Follow" -> "follow"
122 "Announce" -> "reblog"
123 "Like" -> "favourite"
124 end
125 end
126
127 defp format_title(%{activity: %{data: %{"type" => type}}}) do
128 case type do
129 "Create" -> "New Mention"
130 "Follow" -> "New Follower"
131 "Announce" -> "New Repeat"
132 "Like" -> "New Favorite"
133 end
134 end
135
136 defp format_body(%{activity: %{data: %{"type" => type}}}, actor) do
137 case type do
138 "Create" -> "@#{actor.nickname} has mentioned you"
139 "Follow" -> "@#{actor.nickname} has followed you"
140 "Announce" -> "@#{actor.nickname} has repeated your post"
141 "Like" -> "@#{actor.nickname} has favorited your post"
142 end
143 end
144 end