Merge remote-tracking branch 'remotes/upstream/develop' into twitter_oauth
[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.Web.Push.Impl
9
10 require Logger
11
12 ##############
13 # Client API #
14 ##############
15
16 def start_link do
17 GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
18 end
19
20 def vapid_config do
21 Application.get_env(:web_push_encryption, :vapid_details, [])
22 end
23
24 def enabled do
25 case vapid_config() do
26 [] -> false
27 list when is_list(list) -> true
28 _ -> false
29 end
30 end
31
32 def send(notification),
33 do: GenServer.cast(__MODULE__, {:send, notification})
34
35 ####################
36 # Server Callbacks #
37 ####################
38
39 @impl true
40 def init(:ok) do
41 if enabled() do
42 {:ok, nil}
43 else
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 end
54 end
55
56 @impl true
57 def handle_cast({:send, notification}, state) do
58 if enabled() do
59 Impl.perform_send(notification)
60 end
61
62 {:noreply, state}
63 end
64 end