Add status text to notifications (mentions and reposts)
[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.Activity
11 alias Pleroma.Object
12 alias Pleroma.Web.Push.Subscription
13 alias Pleroma.Web.Metadata.Utils
14
15 require Logger
16 import Ecto.Query
17
18 @types ["Create", "Follow", "Announce", "Like"]
19
20 def start_link() do
21 GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
22 end
23
24 def vapid_config() do
25 Application.get_env(:web_push_encryption, :vapid_details, [])
26 end
27
28 def enabled() do
29 case vapid_config() do
30 [] -> false
31 list when is_list(list) -> true
32 _ -> false
33 end
34 end
35
36 def send(notification) do
37 if enabled() do
38 GenServer.cast(Pleroma.Web.Push, {:send, notification})
39 end
40 end
41
42 def init(:ok) do
43 if !enabled() do
44 Logger.warn("""
45 VAPID key pair is not found. If you wish to enabled web push, please run
46
47 mix web_push.gen.keypair
48
49 and add the resulting output to your configuration file.
50 """)
51
52 :ignore
53 else
54 {:ok, nil}
55 end
56 end
57
58 def handle_cast(
59 {:send, %{activity: %{data: %{"type" => type}}, user_id: user_id} = notification},
60 state
61 )
62 when type in @types do
63 actor = User.get_cached_by_ap_id(notification.activity.data["actor"])
64
65 type = Pleroma.Activity.mastodon_notification_type(notification.activity)
66
67 Subscription
68 |> where(user_id: ^user_id)
69 |> preload(:token)
70 |> Repo.all()
71 |> Enum.filter(fn subscription ->
72 get_in(subscription.data, ["alerts", type]) || false
73 end)
74 |> Enum.each(fn subscription ->
75 sub = %{
76 keys: %{
77 p256dh: subscription.key_p256dh,
78 auth: subscription.key_auth
79 },
80 endpoint: subscription.endpoint
81 }
82
83 body =
84 Jason.encode!(%{
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"
92 })
93
94 case WebPushEncryption.send_web_push(
95 body,
96 sub,
97 Application.get_env(:web_push_encryption, :gcm_api_key)
98 ) do
99 {:ok, %{status_code: code}} when 400 <= code and code < 500 ->
100 Logger.debug("Removing subscription record")
101 Repo.delete!(subscription)
102 :ok
103
104 {:ok, %{status_code: code}} when 200 <= code and code < 300 ->
105 :ok
106
107 {:ok, %{status_code: code}} ->
108 Logger.error("Web Push Notification failed with code: #{code}")
109 :error
110
111 _ ->
112 Logger.error("Web Push Notification failed with unknown error")
113 :error
114 end
115 end)
116
117 {:noreply, state}
118 end
119
120 def handle_cast({:send, _}, state) do
121 Logger.warn("Unknown notification type")
122 {:noreply, state}
123 end
124
125 # Pleroma.Repo.all(Pleroma.Object)
126 # Pleroma.Repo.all(Pleroma.Activity)
127
128 def format_body(
129 %{activity: %{data: %{"type" => "Create", "object" => %{"content" => content}}}},
130 actor
131 ) do
132 "@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}"
133 end
134
135 def format_body(
136 %{activity: %{data: %{"type" => "Announce", "object" => activity_id}}},
137 actor
138 ) do
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)
141
142 "@#{actor.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}"
143 end
144
145 def format_body(
146 %{activity: %{data: %{"type" => type}}},
147 actor
148 )
149 when type in ["Follow", "Like"] do
150 case type do
151 "Follow" -> "@#{actor.nickname} has followed you"
152 "Like" -> "@#{actor.nickname} has favorited your post"
153 end
154 end
155
156 defp format_title(%{activity: %{data: %{"type" => type}}}) do
157 case type do
158 "Create" -> "New Mention"
159 "Follow" -> "New Follower"
160 "Announce" -> "New Repeat"
161 "Like" -> "New Favorite"
162 end
163 end
164 end