add VAPID to test config
[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 body = notification |> format(actor) |> Jason.encode!()
45
46 Subscription
47 |> where(user_id: ^user_id)
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 case WebPushEncryption.send_web_push(body, subscription, @gcm_api_key) do
59 {:ok, %{status_code: code}} when 400 <= code and code < 500 ->
60 Logger.debug("Removing subscription record")
61 Repo.delete!(record)
62 :ok
63
64 {:ok, %{status_code: code}} when 200 <= code and code < 300 ->
65 :ok
66
67 {:ok, %{status_code: code}} ->
68 Logger.error("Web Push Nonification failed with code: #{code}")
69 :error
70
71 data ->
72 Logger.error("Web Push Nonification failed with unknown error")
73 IO.inspect(data)
74 :error
75 end
76 end)
77
78 {:noreply, state}
79 end
80
81 def handle_cast({:send, _}, state) do
82 Logger.warn("Unknown notification type")
83 {:noreply, state}
84 end
85
86 def format(%{activity: %{data: %{"type" => "Create"}}}, actor) do
87 %{
88 title: "New Mention",
89 body: "@#{actor.nickname} has mentiond you",
90 icon: get_avatar_url(actor)
91 }
92 end
93
94 def format(%{activity: %{data: %{"type" => "Follow"}}}, actor) do
95 %{
96 title: "New Follower",
97 body: "@#{actor.nickname} has followed you",
98 icon: get_avatar_url(actor)
99 }
100 end
101
102 def format(%{activity: %{data: %{"type" => "Announce"}}}, actor) do
103 %{
104 title: "New Announce",
105 body: "@#{actor.nickname} has announced your post",
106 icon: get_avatar_url(actor)
107 }
108 end
109
110 def format(%{activity: %{data: %{"type" => "Like"}}}, actor) do
111 %{
112 title: "New Like",
113 body: "@#{actor.nickname} has liked your post",
114 icon: get_avatar_url(actor)
115 }
116 end
117
118 def get_avatar_url(%{avatar: %{"type" => "Image", "url" => urls}}) do
119 case List.first(urls) do
120 %{"href" => url} -> url
121 _ -> get_avatar_url(nil)
122 end
123 end
124
125 def get_avatar_url(_) do
126 Pleroma.Web.Endpoint.static_url() <> "/images/avi.png"
127 end
128 end