From f1712cd2f1ec6061f70d259f8f5e2b7e9f408d8c Mon Sep 17 00:00:00 2001
From: Egor Kislitsyn <egor@kislitsyn.com>
Date: Fri, 5 Apr 2019 19:38:44 +0700
Subject: [PATCH] Use PleromaJobQueue in Pleroma.Web.Push

---
 config/config.exs            |  1 +
 docs/config.md               |  5 ++--
 lib/pleroma/application.ex   |  4 +--
 lib/pleroma/web/push/impl.ex |  6 ++---
 lib/pleroma/web/push/push.ex | 48 ++++++++----------------------------
 test/web/push/impl_test.exs  |  6 +++--
 6 files changed, 23 insertions(+), 47 deletions(-)

diff --git a/config/config.exs b/config/config.exs
index c143f79fc8..d97586a61d 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -356,6 +356,7 @@
 config :pleroma_job_queue, :queues,
   federator_incoming: 50,
   federator_outgoing: 50,
+  web_push: 50,
   mailer: 10
 
 config :pleroma, :fetch_initial_posts,
diff --git a/docs/config.md b/docs/config.md
index 06d6fd757d..6f31195737 100644
--- a/docs/config.md
+++ b/docs/config.md
@@ -218,14 +218,14 @@ This section is used to configure Pleroma-FE, unless ``:managed_config`` in ``:i
   - `port`
 * `url` - a list containing the configuration for generating urls, accepts
   - `host` - the host without the scheme and a post (e.g `example.com`, not `https://example.com:2020`)
-  - `scheme` - e.g `http`, `https` 
+  - `scheme` - e.g `http`, `https`
   - `port`
   - `path`
 
 
 **Important note**: if you modify anything inside these lists, default `config.exs` values will be overwritten, which may result in breakage, to make sure this does not happen please copy the default value for the list from `config.exs` and modify/add only what you need
 
-Example: 
+Example:
 ```elixir
 config :pleroma, Pleroma.Web.Endpoint,
   url: [host: "example.com", port: 2020, scheme: "https"],
@@ -317,6 +317,7 @@ Pleroma has the following queues:
 * `federator_outgoing` - Outgoing federation
 * `federator_incoming` - Incoming federation
 * `mailer` - Email sender, see [`Pleroma.Mailer`](#pleroma-mailer)
+* `web_push` - Web push notifications
 
 Example:
 
diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex
index 782d1d5899..8f8d26814e 100644
--- a/lib/pleroma/application.ex
+++ b/lib/pleroma/application.ex
@@ -109,8 +109,8 @@ def start(_type, _args) do
         [
           worker(Pleroma.Web.Federator.RetryQueue, []),
           worker(Pleroma.Stats, []),
-          worker(Pleroma.Web.Push, []),
-          worker(Task, [&Pleroma.Web.Federator.init/0], restart: :temporary)
+          worker(Task, [&Pleroma.Web.Push.init/0], restart: :temporary, id: :web_push_init),
+          worker(Task, [&Pleroma.Web.Federator.init/0], restart: :temporary, id: :federator_init)
         ] ++
         streamer_child() ++
         chat_child() ++
diff --git a/lib/pleroma/web/push/impl.ex b/lib/pleroma/web/push/impl.ex
index 8635731852..2233480c5e 100644
--- a/lib/pleroma/web/push/impl.ex
+++ b/lib/pleroma/web/push/impl.ex
@@ -19,8 +19,8 @@ defmodule Pleroma.Web.Push.Impl do
   @types ["Create", "Follow", "Announce", "Like"]
 
   @doc "Performs sending notifications for user subscriptions"
-  @spec perform_send(Notification.t()) :: list(any)
-  def perform_send(
+  @spec perform(Notification.t()) :: list(any) | :error
+  def perform(
         %{activity: %{data: %{"type" => activity_type}, id: activity_id}, user_id: user_id} =
           notif
       )
@@ -50,7 +50,7 @@ def perform_send(
     end
   end
 
-  def perform_send(_) do
+  def perform(_) do
     Logger.warn("Unknown notification type")
     :error
   end
diff --git a/lib/pleroma/web/push/push.ex b/lib/pleroma/web/push/push.ex
index 5259e8e330..cdd50005d3 100644
--- a/lib/pleroma/web/push/push.ex
+++ b/lib/pleroma/web/push/push.ex
@@ -3,18 +3,20 @@
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Web.Push do
-  use GenServer
-
   alias Pleroma.Web.Push.Impl
 
   require Logger
 
-  ##############
-  # Client API #
-  ##############
+  def init() do
+    unless enabled() do
+      Logger.warn("""
+      VAPID key pair is not found. If you wish to enabled web push, please run
+
+          mix web_push.gen.keypair
 
-  def start_link do
-    GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
+      and add the resulting output to your configuration file.
+      """)
+    end
   end
 
   def vapid_config do
@@ -30,35 +32,5 @@ def enabled do
   end
 
   def send(notification),
-    do: GenServer.cast(__MODULE__, {:send, notification})
-
-  ####################
-  # Server Callbacks #
-  ####################
-
-  @impl true
-  def init(:ok) do
-    if enabled() do
-      {:ok, nil}
-    else
-      Logger.warn("""
-      VAPID key pair is not found. If you wish to enabled web push, please run
-
-          mix web_push.gen.keypair
-
-      and add the resulting output to your configuration file.
-      """)
-
-      :ignore
-    end
-  end
-
-  @impl true
-  def handle_cast({:send, notification}, state) do
-    if enabled() do
-      Impl.perform_send(notification)
-    end
-
-    {:noreply, state}
-  end
+    do: PleromaJobQueue.enqueue(:web_push, Impl, [notification])
 end
diff --git a/test/web/push/impl_test.exs b/test/web/push/impl_test.exs
index 3f9f3d809f..6bac2c9f6e 100644
--- a/test/web/push/impl_test.exs
+++ b/test/web/push/impl_test.exs
@@ -64,17 +64,19 @@ test "performs sending notifications" do
         }
       )
 
-    assert Impl.perform_send(notif) == [:ok, :ok]
+    assert Impl.perform(notif) == [:ok, :ok]
   end
 
+  @tag capture_log: true
   test "returns error if notif does not match " do
-    assert Impl.perform_send(%{}) == :error
+    assert Impl.perform(%{}) == :error
   end
 
   test "successful message sending" do
     assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
   end
 
+  @tag capture_log: true
   test "fail message sending" do
     assert Impl.push_message(
              @message,
-- 
GitLab