74084558d49fb8935d054864eaae4945559ec8f2
[akkoma] / lib / pleroma / web / federator / publisher.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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.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 IO.puts("ENQUEUE")
35 IO.inspect(params)
36
37 PublisherWorker.enqueue(
38 "publish_one",
39 %{"module" => to_string(module), "params" => params}
40 )
41 end
42
43 @doc """
44 Relays an activity to all specified peers.
45 """
46 @callback publish(User.t(), Activity.t()) :: :ok | {:error, any()}
47
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)
55 end
56 end)
57
58 :ok
59 end
60
61 @doc """
62 Gathers links used by an outgoing federation module for WebFinger output.
63 """
64 @callback gather_webfinger_links(User.t()) :: list()
65
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)
71 end)
72 end
73
74 @doc """
75 Gathers nodeinfo protocol names supported by the federation module.
76 """
77 @callback gather_nodeinfo_protocol_names() :: list()
78
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()
84 end)
85 end
86
87 @doc """
88 Gathers a set of remote users given an IR envelope.
89 """
90 def remote_users(%User{id: user_id}, %{data: %{"to" => to} = data}) do
91 cc = Map.get(data, "cc", [])
92
93 bcc =
94 data
95 |> Map.get("bcc", [])
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)
101
102 _ ->
103 bcc
104 end
105 end)
106
107 [to, cc, bcc]
108 |> Enum.concat()
109 |> Enum.map(&User.get_cached_by_ap_id/1)
110 |> Enum.filter(fn user -> user && !user.local end)
111 end
112 end