8b59e54cbae5170c3aaa0d97ce153377d9300698
[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 Subscription
46 |> where(user_id: ^user_id)
47 |> preload(:token)
48 |> Repo.all()
49 |> Enum.each(fn record ->
50 subscription = %{
51 keys: %{
52 p256dh: record.key_p256dh,
53 auth: record.key_auth
54 },
55 endpoint: record.endpoint
56 }
57
58 body =
59 Jason.encode!(%{
60 title: format_title(notification),
61 access_token: record.token.token,
62 body: format_body(notification, actor),
63 notification_id: notification.id,
64 notification_type: format_type(notification),
65 icon: User.avatar_url(actor),
66 preferred_locale: "en"
67 })
68
69 case WebPushEncryption.send_web_push(body, subscription) do
70 {:ok, %{status_code: code}} when 400 <= code and code < 500 ->
71 Logger.debug("Removing subscription record")
72 Repo.delete!(record)
73 :ok
74
75 {:ok, %{status_code: code}} when 200 <= code and code < 300 ->
76 :ok
77
78 {:ok, %{status_code: code}} ->
79 Logger.error("Web Push Nonification failed with code: #{code}")
80 :error
81
82 _ ->
83 Logger.error("Web Push Nonification failed with unknown error")
84 :error
85 end
86 end)
87
88 {:noreply, state}
89 end
90
91 def handle_cast({:send, _}, state) do
92 Logger.warn("Unknown notification type")
93 {:noreply, state}
94 end
95
96 # https://github.com/tootsuite/mastodon/blob/master/app/models/notification.rb#L19
97 defp format_type(%{activity: %{data: %{"type" => type}}}) do
98 case type do
99 "Create" -> "mention"
100 "Follow" -> "follow"
101 "Announce" -> "reblog"
102 "Favorite" -> "favourite"
103 end
104 end
105
106 defp format_title(%{activity: %{data: %{"type" => type}}}) do
107 case type do
108 "Create" -> "New Mention"
109 "Follow" -> "New Follower"
110 "Announce" -> "New Repeat"
111 "Like" -> "New Favorite"
112 end
113 end
114
115 defp format_body(%{activity: %{data: %{"type" => type}}}, actor) do
116 case type do
117 "Create" -> "@#{actor.nickname} has mentiond you"
118 "Follow" -> "@#{actor.nickname} has followed you"
119 "Announce" -> "@#{actor.nickname} has repeated your post"
120 "Like" -> "@#{actor.nickname} has favorited your post"
121 end
122 end
123 end