[#1149] Replaced RetryQueue with oban-based retries.
[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.Activity
7 alias Pleroma.Config
8 alias Pleroma.User
9
10 require Logger
11
12 @moduledoc """
13 Defines the contract used by federation implementations to publish messages to
14 their peers.
15 """
16
17 @doc """
18 Determine whether an activity can be relayed using the federation module.
19 """
20 @callback is_representable?(Pleroma.Activity.t()) :: boolean()
21
22 @doc """
23 Relays an activity to a specified peer, determined by the parameters. The
24 parameters used are controlled by the federation module.
25 """
26 @callback publish_one(Map.t()) :: {:ok, Map.t()} | {:error, any()}
27
28 @doc """
29 Enqueue publishing a single activity.
30 """
31 @spec enqueue_one(module(), Map.t()) :: :ok
32 def enqueue_one(module, %{} = params) do
33 %{module: to_string(module), params: params}
34 |> Pleroma.Workers.Publisher.new()
35 |> Pleroma.Repo.insert()
36 end
37
38 @doc """
39 Relays an activity to all specified peers.
40 """
41 @callback publish(User.t(), Activity.t()) :: :ok | {:error, any()}
42
43 @spec publish(User.t(), Activity.t()) :: :ok
44 def publish(%User{} = user, %Activity{} = activity) do
45 Config.get([:instance, :federation_publisher_modules])
46 |> Enum.each(fn module ->
47 if module.is_representable?(activity) do
48 Logger.info("Publishing #{activity.data["id"]} using #{inspect(module)}")
49 module.publish(user, activity)
50 end
51 end)
52
53 :ok
54 end
55
56 @doc """
57 Gathers links used by an outgoing federation module for WebFinger output.
58 """
59 @callback gather_webfinger_links(User.t()) :: list()
60
61 @spec gather_webfinger_links(User.t()) :: list()
62 def gather_webfinger_links(%User{} = user) do
63 Config.get([:instance, :federation_publisher_modules])
64 |> Enum.reduce([], fn module, links ->
65 links ++ module.gather_webfinger_links(user)
66 end)
67 end
68
69 @doc """
70 Gathers nodeinfo protocol names supported by the federation module.
71 """
72 @callback gather_nodeinfo_protocol_names() :: list()
73
74 @spec gather_nodeinfo_protocol_names() :: list()
75 def gather_nodeinfo_protocol_names do
76 Config.get([:instance, :federation_publisher_modules])
77 |> Enum.reduce([], fn module, links ->
78 links ++ module.gather_nodeinfo_protocol_names()
79 end)
80 end
81 end