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