Use PleromaJobQueue in Pleroma.Web.Push

This commit is contained in:
Egor Kislitsyn 2019-04-05 19:38:44 +07:00
parent 9c9eec62c2
commit f1712cd2f1
6 changed files with 23 additions and 47 deletions

View File

@ -356,6 +356,7 @@
config :pleroma_job_queue, :queues, config :pleroma_job_queue, :queues,
federator_incoming: 50, federator_incoming: 50,
federator_outgoing: 50, federator_outgoing: 50,
web_push: 50,
mailer: 10 mailer: 10
config :pleroma, :fetch_initial_posts, config :pleroma, :fetch_initial_posts,

View File

@ -317,6 +317,7 @@ Pleroma has the following queues:
* `federator_outgoing` - Outgoing federation * `federator_outgoing` - Outgoing federation
* `federator_incoming` - Incoming federation * `federator_incoming` - Incoming federation
* `mailer` - Email sender, see [`Pleroma.Mailer`](#pleroma-mailer) * `mailer` - Email sender, see [`Pleroma.Mailer`](#pleroma-mailer)
* `web_push` - Web push notifications
Example: Example:

View File

@ -109,8 +109,8 @@ def start(_type, _args) do
[ [
worker(Pleroma.Web.Federator.RetryQueue, []), worker(Pleroma.Web.Federator.RetryQueue, []),
worker(Pleroma.Stats, []), worker(Pleroma.Stats, []),
worker(Pleroma.Web.Push, []), worker(Task, [&Pleroma.Web.Push.init/0], restart: :temporary, id: :web_push_init),
worker(Task, [&Pleroma.Web.Federator.init/0], restart: :temporary) worker(Task, [&Pleroma.Web.Federator.init/0], restart: :temporary, id: :federator_init)
] ++ ] ++
streamer_child() ++ streamer_child() ++
chat_child() ++ chat_child() ++

View File

@ -19,8 +19,8 @@ defmodule Pleroma.Web.Push.Impl do
@types ["Create", "Follow", "Announce", "Like"] @types ["Create", "Follow", "Announce", "Like"]
@doc "Performs sending notifications for user subscriptions" @doc "Performs sending notifications for user subscriptions"
@spec perform_send(Notification.t()) :: list(any) @spec perform(Notification.t()) :: list(any) | :error
def perform_send( def perform(
%{activity: %{data: %{"type" => activity_type}, id: activity_id}, user_id: user_id} = %{activity: %{data: %{"type" => activity_type}, id: activity_id}, user_id: user_id} =
notif notif
) )
@ -50,7 +50,7 @@ def perform_send(
end end
end end
def perform_send(_) do def perform(_) do
Logger.warn("Unknown notification type") Logger.warn("Unknown notification type")
:error :error
end end

View File

@ -3,18 +3,20 @@
# SPDX-License-Identifier: AGPL-3.0-only # SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Push do defmodule Pleroma.Web.Push do
use GenServer
alias Pleroma.Web.Push.Impl alias Pleroma.Web.Push.Impl
require Logger require Logger
############## def init() do
# Client API # unless enabled() do
############## Logger.warn("""
VAPID key pair is not found. If you wish to enabled web push, please run
def start_link do mix web_push.gen.keypair
GenServer.start_link(__MODULE__, :ok, name: __MODULE__)
and add the resulting output to your configuration file.
""")
end
end end
def vapid_config do def vapid_config do
@ -30,35 +32,5 @@ def enabled do
end end
def send(notification), def send(notification),
do: GenServer.cast(__MODULE__, {:send, notification}) do: PleromaJobQueue.enqueue(:web_push, Impl, [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
end end

View File

@ -64,17 +64,19 @@ test "performs sending notifications" do
} }
) )
assert Impl.perform_send(notif) == [:ok, :ok] assert Impl.perform(notif) == [:ok, :ok]
end end
@tag capture_log: true
test "returns error if notif does not match " do test "returns error if notif does not match " do
assert Impl.perform_send(%{}) == :error assert Impl.perform(%{}) == :error
end end
test "successful message sending" do test "successful message sending" do
assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok assert Impl.push_message(@message, @sub, @api_key, %Subscription{}) == :ok
end end
@tag capture_log: true
test "fail message sending" do test "fail message sending" do
assert Impl.push_message( assert Impl.push_message(
@message, @message,