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