1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Federator.Publisher do
9 alias Pleroma.Workers.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
37 PublisherWorker.enqueue(
39 %{"module" => to_string(module), "params" => params}
44 Relays an activity to all specified peers.
46 @callback publish(User.t(), Activity.t()) :: :ok | {:error, any()}
48 @spec publish(User.t(), Activity.t()) :: :ok
49 def publish(%User{} = user, %Activity{} = activity) do
50 Config.get([:instance, :federation_publisher_modules])
51 |> Enum.each(fn module ->
52 if module.is_representable?(activity) do
53 Logger.debug("Publishing #{activity.data["id"]} using #{inspect(module)}")
54 module.publish(user, activity)
62 Gathers links used by an outgoing federation module for WebFinger output.
64 @callback gather_webfinger_links(User.t()) :: list()
66 @spec gather_webfinger_links(User.t()) :: list()
67 def gather_webfinger_links(%User{} = user) do
68 Config.get([:instance, :federation_publisher_modules])
69 |> Enum.reduce([], fn module, links ->
70 links ++ module.gather_webfinger_links(user)
75 Gathers nodeinfo protocol names supported by the federation module.
77 @callback gather_nodeinfo_protocol_names() :: list()
79 @spec gather_nodeinfo_protocol_names() :: list()
80 def gather_nodeinfo_protocol_names do
81 Config.get([:instance, :federation_publisher_modules])
82 |> Enum.reduce([], fn module, links ->
83 links ++ module.gather_nodeinfo_protocol_names()
88 Gathers a set of remote users given an IR envelope.
90 def remote_users(%User{id: user_id}, %{data: %{"to" => to} = data}) do
91 cc = Map.get(data, "cc", [])
96 |> Enum.reduce([], fn ap_id, bcc ->
97 case Pleroma.List.get_by_ap_id(ap_id) do
98 %Pleroma.List{user_id: ^user_id} = list ->
99 {:ok, following} = Pleroma.List.get_following(list)
100 bcc ++ Enum.map(following, & &1.ap_id)
109 |> Enum.map(&User.get_cached_by_ap_id/1)
110 |> Enum.filter(fn user -> user && !user.local end)