d9c3410fef82b0b242782d23de51cd5f5e0179e4
[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 @gcm_api_key nil
13
14 def start_link() do
15 GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
16 end
17
18 def init(:ok) do
19 case Application.get_env(:web_push_encryption, :vapid_details) do
20 nil ->
21 Logger.warn(
22 "VAPID key pair is not found. Please, add VAPID configuration to config. Run `mix web_push.gen.keypair` mix task to create a key pair"
23 )
24
25 :ignore
26
27 _ ->
28 {:ok, %{}}
29 end
30 end
31
32 def send(notification) do
33 if Application.get_env(:web_push_encryption, :vapid_details) do
34 GenServer.cast(Pleroma.Web.Push, {:send, notification})
35 end
36 end
37
38 def handle_cast(
39 {:send, %{activity: %{data: %{"type" => type}}, user_id: user_id} = notification},
40 state
41 )
42 when type in @types do
43 actor = User.get_cached_by_ap_id(notification.activity.data["actor"])
44
45 type = format_type(notification)
46
47 Subscription
48 |> where(user_id: ^user_id)
49 |> preload(:token)
50 |> Repo.all()
51 |> Enum.filter(fn subscription ->
52 get_in(subscription.data, ["alerts", type]) || false
53 end)
54 |> Enum.each(fn subscription ->
55 sub = %{
56 keys: %{
57 p256dh: subscription.key_p256dh,
58 auth: subscription.key_auth
59 },
60 endpoint: subscription.endpoint
61 }
62
63 body =
64 Jason.encode!(%{
65 title: format_title(notification),
66 access_token: subscription.token.token,
67 body: format_body(notification, actor),
68 notification_id: notification.id,
69 notification_type: type,
70 icon: User.avatar_url(actor),
71 preferred_locale: "en"
72 })
73
74 case WebPushEncryption.send_web_push(body, sub) do
75 {:ok, %{status_code: code}} when 400 <= code and code < 500 ->
76 Logger.debug("Removing subscription record")
77 Repo.delete!(subscription)
78 :ok
79
80 {:ok, %{status_code: code}} when 200 <= code and code < 300 ->
81 :ok
82
83 {:ok, %{status_code: code}} ->
84 Logger.error("Web Push Notification failed with code: #{code}")
85 :error
86
87 _ ->
88 Logger.error("Web Push Notification failed with unknown error")
89 :error
90 end
91 end)
92
93 {:noreply, state}
94 end
95
96 def handle_cast({:send, _}, state) do
97 Logger.warn("Unknown notification type")
98 {:noreply, state}
99 end
100
101 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
102 defp format_type(%{activity: %{data: %{"type" => type}}}) do
103 case type do
104 "Create" -> "mention"
105 "Follow" -> "follow"
106 "Announce" -> "reblog"
107 "Favorite" -> "favourite"
108 end
109 end
110
111 defp format_title(%{activity: %{data: %{"type" => type}}}) do
112 case type do
113 "Create" -> "New Mention"
114 "Follow" -> "New Follower"
115 "Announce" -> "New Repeat"
116 "Like" -> "New Favorite"
117 end
118 end
119
120 defp format_body(%{activity: %{data: %{"type" => type}}}, actor) do
121 case type do
122 "Create" -> "@#{actor.nickname} has mentiond you"
123 "Follow" -> "@#{actor.nickname} has followed you"
124 "Announce" -> "@#{actor.nickname} has repeated your post"
125 "Like" -> "@#{actor.nickname} has favorited your post"
126 end
127 end
128 end