Merge branch 'develop' into feature/compat/push-subscriptions
[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.error(
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 {:error, %{}}
26
27 _ ->
28 {:ok, %{}}
29 end
30 end
31
32 def send(notification) do
33 GenServer.cast(Pleroma.Web.Push, {:send, notification})
34 end
35
36 def handle_cast(
37 {:send, %{activity: %{data: %{"type" => type}}, user_id: user_id} = notification},
38 state
39 )
40 when type in @types do
41 actor = User.get_cached_by_ap_id(notification.activity.data["actor"])
42 body = notification |> format(actor) |> Jason.encode!()
43
44 Subscription
45 |> where(user_id: ^user_id)
46 |> Repo.all()
47 |> Enum.each(fn record ->
48 subscription = %{
49 keys: %{
50 p256dh: record.key_p256dh,
51 auth: record.key_auth
52 },
53 endpoint: record.endpoint
54 }
55
56 case WebPushEncryption.send_web_push(body, subscription, @gcm_api_key) do
57 {:ok, %{status_code: code}} when 400 <= code and code < 500 ->
58 Logger.debug("Removing subscription record")
59 Repo.delete!(record)
60 :ok
61
62 {:ok, %{status_code: code}} when 200 <= code and code < 300 ->
63 :ok
64
65 {:ok, %{status_code: code}} ->
66 Logger.error("Web Push Nonification failed with code: #{code}")
67 :error
68
69 data ->
70 Logger.error("Web Push Nonification failed with unknown error")
71 IO.inspect(data)
72 :error
73 end
74 end)
75
76 {:noreply, state}
77 end
78
79 def handle_cast({:send, _}, state) do
80 Logger.warn("Unknown notification type")
81 {:noreply, state}
82 end
83
84 def format(%{activity: %{data: %{"type" => "Create"}}}, actor) do
85 %{
86 title: "New Mention",
87 body: "@#{actor.nickname} has mentiond you",
88 icon: get_avatar_url(actor)
89 }
90 end
91
92 def format(%{activity: %{data: %{"type" => "Follow"}}}, actor) do
93 %{
94 title: "New Follower",
95 body: "@#{actor.nickname} has followed you",
96 icon: get_avatar_url(actor)
97 }
98 end
99
100 def format(%{activity: %{data: %{"type" => "Announce"}}}, actor) do
101 %{
102 title: "New Announce",
103 body: "@#{actor.nickname} has announced your post",
104 icon: get_avatar_url(actor)
105 }
106 end
107
108 def format(%{activity: %{data: %{"type" => "Like"}}}, actor) do
109 %{
110 title: "New Like",
111 body: "@#{actor.nickname} has liked your post",
112 icon: get_avatar_url(actor)
113 }
114 end
115
116 def get_avatar_url(%{avatar: %{"type" => "Image", "url" => urls}}) do
117 case List.first(urls) do
118 %{"href" => url} -> url
119 _ -> get_avatar_url(nil)
120 end
121 end
122
123 def get_avatar_url(_) do
124 Pleroma.Web.Endpoint.static_url() <> "/images/avi.png"
125 end
126 end