Merge branch 'feature/mastofe-admin-scope' into 'develop'
[akkoma] / lib / pleroma / web / federator / publisher.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 PublisherWorker.enqueue(
35 "publish_one",
36 %{"module" => to_string(module), "params" => params}
37 )
38 end
39
40 @doc """
41 Relays an activity to all specified peers.
42 """
43 @callback publish(User.t(), Activity.t()) :: :ok | {:error, any()}
44
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)
52 end
53 end)
54
55 :ok
56 end
57
58 @doc """
59 Gathers links used by an outgoing federation module for WebFinger output.
60 """
61 @callback gather_webfinger_links(User.t()) :: list()
62
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)
68 end)
69 end
70
71 @doc """
72 Gathers nodeinfo protocol names supported by the federation module.
73 """
74 @callback gather_nodeinfo_protocol_names() :: list()
75
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()
81 end)
82 end
83
84 @doc """
85 Gathers a set of remote users given an IR envelope.
86 """
87 def remote_users(%User{id: user_id}, %{data: %{"to" => to} = data}) do
88 cc = Map.get(data, "cc", [])
89
90 bcc =
91 data
92 |> Map.get("bcc", [])
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)
98
99 _ ->
100 bcc
101 end
102 end)
103
104 [to, cc, bcc]
105 |> Enum.concat()
106 |> Enum.map(&User.get_cached_by_ap_id/1)
107 |> Enum.filter(fn user -> user && !user.local end)
108 end
109 end