1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Federator.Publisher do
9 alias Pleroma.Workers.Publisher, as: PublisherWorker
14 Defines the contract used by federation implementations to publish messages to
19 Determine whether an activity can be relayed using the federation module.
21 @callback is_representable?(Pleroma.Activity.t()) :: boolean()
24 Relays an activity to a specified peer, determined by the parameters. The
25 parameters used are controlled by the federation module.
27 @callback publish_one(Map.t()) :: {:ok, Map.t()} | {:error, any()}
30 Enqueue publishing a single activity.
32 @spec enqueue_one(module(), Map.t()) :: :ok
33 def enqueue_one(module, %{} = params) do
35 if max_attempts = Pleroma.Config.get([:workers, :retries, :federator_outgoing]) do
36 [max_attempts: max_attempts]
41 %{"op" => "publish_one", "module" => to_string(module), "params" => params}
42 |> PublisherWorker.new(worker_args)
43 |> Pleroma.Repo.insert()
47 Relays an activity to all specified peers.
49 @callback publish(User.t(), Activity.t()) :: :ok | {:error, any()}
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)
65 Gathers links used by an outgoing federation module for WebFinger output.
67 @callback gather_webfinger_links(User.t()) :: list()
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)
78 Gathers nodeinfo protocol names supported by the federation module.
80 @callback gather_nodeinfo_protocol_names() :: list()
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()