federator: publisher: integrate job queue, simplify publish_one logic
[akkoma] / lib / pleroma / web / federator / publisher.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.Federator.Publisher do
6 alias Pleroma.Web.Federator.RetryQueue
7
8 require Logger
9
10 @moduledoc """
11 Defines the contract used by federation implementations to publish messages to
12 their peers.
13 """
14
15 @doc """
16 Determine whether an activity can be relayed using the federation module.
17 """
18 @callback is_representable?(Pleroma.Activity.t()) :: boolean()
19
20 @doc """
21 Relays an activity to a specified peer, determined by the parameters. The
22 parameters used are controlled by the federation module.
23 """
24 @callback publish_one(Map.t()) :: {:ok, Map.t()} | {:error, any()}
25
26 @doc """
27 Relays an activity to all specified peers.
28 """
29 @callback publish(Pleroma.User.t(), Pleroma.Activity.t()) :: :ok | {:error, any()}
30
31 @doc """
32 Enqueue publishing a single activity.
33 """
34 @spec enqueue_one(module(), Map.t()) :: :ok
35 def enqueue_one(module, %{} = params),
36 do: PleromaJobQueue.enqueue(:federation_outgoing, __MODULE__, [:publish_one, module, params])
37
38 @spec perform(atom(), module(), any()) :: {:ok, any()} | {:error, any()}
39 def perform(:publish_one, module, params) do
40 case apply(module, :publish_one, [params]) do
41 {:ok, _} ->
42 :ok
43
44 {:error, _} ->
45 RetryQueue.enqueue(params, module)
46 end
47 end
48
49 def perform(type, _, _) do
50 Logger.debug("Unknown task: #{type}")
51 {:error, "Don't know what to do with this"}
52 end
53 end