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
13 Defines the contract used by federation implementations to publish messages to
18 Determine whether an activity can be relayed using the federation module.
20 @callback is_representable?(Pleroma.Activity.t()) :: boolean()
23 Relays an activity to a specified peer, determined by the parameters. The
24 parameters used are controlled by the federation module.
26 @callback publish_one(Map.t()) :: {:ok, Map.t()} | {:error, any()}
29 Enqueue publishing a single activity.
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()
39 Relays an activity to all specified peers.
41 @callback publish(User.t(), Activity.t()) :: :ok | {:error, any()}
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)
57 Gathers links used by an outgoing federation module for WebFinger output.
59 @callback gather_webfinger_links(User.t()) :: list()
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)
70 Gathers nodeinfo protocol names supported by the federation module.
72 @callback gather_nodeinfo_protocol_names() :: list()
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()