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