1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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
34 PublisherWorker.enqueue(
36 %{"module" => to_string(module), "params" => params}
41 Relays an activity to all specified peers.
43 @callback publish(User.t(), Activity.t()) :: :ok | {:error, any()}
45 @spec publish(User.t(), Activity.t()) :: :ok
46 def publish(%User{} = user, %Activity{} = activity) do
47 Config.get([:instance, :federation_publisher_modules])
48 |> Enum.each(fn module ->
49 if module.is_representable?(activity) do
50 Logger.debug("Publishing #{activity.data["id"]} using #{inspect(module)}")
51 module.publish(user, activity)
59 Gathers links used by an outgoing federation module for WebFinger output.
61 @callback gather_webfinger_links(User.t()) :: list()
63 @spec gather_webfinger_links(User.t()) :: list()
64 def gather_webfinger_links(%User{} = user) do
65 Config.get([:instance, :federation_publisher_modules])
66 |> Enum.reduce([], fn module, links ->
67 links ++ module.gather_webfinger_links(user)
72 Gathers nodeinfo protocol names supported by the federation module.
74 @callback gather_nodeinfo_protocol_names() :: list()
76 @spec gather_nodeinfo_protocol_names() :: list()
77 def gather_nodeinfo_protocol_names do
78 Config.get([:instance, :federation_publisher_modules])
79 |> Enum.reduce([], fn module, links ->
80 links ++ module.gather_nodeinfo_protocol_names()
85 Gathers a set of remote users given an IR envelope.
87 def remote_users(%User{id: user_id}, %{data: %{"to" => to} = data}) do
88 cc = Map.get(data, "cc", [])
93 |> Enum.reduce([], fn ap_id, bcc ->
94 case Pleroma.List.get_by_ap_id(ap_id) do
95 %Pleroma.List{user_id: ^user_id} = list ->
96 {:ok, following} = Pleroma.List.get_following(list)
97 bcc ++ Enum.map(following, & &1.ap_id)
106 |> Enum.map(&User.get_cached_by_ap_id/1)
107 |> Enum.filter(fn user -> user && !user.local end)