2019-05-11 20:41:34 -06:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 13:38:50 -07:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-05-11 20:41:34 -06:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.Federator.Publisher do
|
2019-05-11 21:55:17 -06:00
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Config
|
|
|
|
alias Pleroma.User
|
2019-08-31 10:08:56 -06:00
|
|
|
alias Pleroma.Workers.PublisherWorker
|
2019-05-11 21:43:53 -06:00
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
2019-05-11 20:41:34 -06:00
|
|
|
@moduledoc """
|
|
|
|
Defines the contract used by federation implementations to publish messages to
|
|
|
|
their peers.
|
|
|
|
"""
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Determine whether an activity can be relayed using the federation module.
|
|
|
|
"""
|
|
|
|
@callback is_representable?(Pleroma.Activity.t()) :: boolean()
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Relays an activity to a specified peer, determined by the parameters. The
|
|
|
|
parameters used are controlled by the federation module.
|
|
|
|
"""
|
|
|
|
@callback publish_one(Map.t()) :: {:ok, Map.t()} | {:error, any()}
|
|
|
|
|
|
|
|
@doc """
|
|
|
|
Enqueue publishing a single activity.
|
|
|
|
"""
|
|
|
|
@spec enqueue_one(module(), Map.t()) :: :ok
|
2019-08-01 08:28:00 -06:00
|
|
|
def enqueue_one(module, %{} = params) do
|
2019-08-31 12:58:42 -06:00
|
|
|
PublisherWorker.enqueue(
|
|
|
|
"publish_one",
|
|
|
|
%{"module" => to_string(module), "params" => params}
|
|
|
|
)
|
2019-05-11 21:43:53 -06:00
|
|
|
end
|
2019-05-11 21:55:17 -06:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Relays an activity to all specified peers.
|
|
|
|
"""
|
2019-05-17 01:25:20 -06:00
|
|
|
@callback publish(User.t(), Activity.t()) :: :ok | {:error, any()}
|
2019-05-11 21:55:17 -06:00
|
|
|
|
2019-05-17 01:25:20 -06:00
|
|
|
@spec publish(User.t(), Activity.t()) :: :ok
|
2019-05-11 21:55:17 -06:00
|
|
|
def publish(%User{} = user, %Activity{} = activity) do
|
|
|
|
Config.get([:instance, :federation_publisher_modules])
|
|
|
|
|> Enum.each(fn module ->
|
2019-05-11 22:17:17 -06:00
|
|
|
if module.is_representable?(activity) do
|
2019-12-11 03:46:07 -07:00
|
|
|
Logger.debug("Publishing #{activity.data["id"]} using #{inspect(module)}")
|
2019-05-11 22:17:17 -06:00
|
|
|
module.publish(user, activity)
|
|
|
|
end
|
2019-05-11 21:55:17 -06:00
|
|
|
end)
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
2019-05-12 13:05:03 -06:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gathers links used by an outgoing federation module for WebFinger output.
|
|
|
|
"""
|
2019-05-17 01:25:20 -06:00
|
|
|
@callback gather_webfinger_links(User.t()) :: list()
|
2019-05-12 13:05:03 -06:00
|
|
|
|
2019-05-17 01:25:20 -06:00
|
|
|
@spec gather_webfinger_links(User.t()) :: list()
|
2019-05-12 13:05:03 -06:00
|
|
|
def gather_webfinger_links(%User{} = user) do
|
|
|
|
Config.get([:instance, :federation_publisher_modules])
|
|
|
|
|> Enum.reduce([], fn module, links ->
|
|
|
|
links ++ module.gather_webfinger_links(user)
|
|
|
|
end)
|
|
|
|
end
|
2019-05-12 13:15:29 -06:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gathers nodeinfo protocol names supported by the federation module.
|
|
|
|
"""
|
|
|
|
@callback gather_nodeinfo_protocol_names() :: list()
|
|
|
|
|
|
|
|
@spec gather_nodeinfo_protocol_names() :: list()
|
|
|
|
def gather_nodeinfo_protocol_names do
|
|
|
|
Config.get([:instance, :federation_publisher_modules])
|
|
|
|
|> Enum.reduce([], fn module, links ->
|
|
|
|
links ++ module.gather_nodeinfo_protocol_names()
|
|
|
|
end)
|
|
|
|
end
|
2019-10-17 17:05:45 -06:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gathers a set of remote users given an IR envelope.
|
|
|
|
"""
|
|
|
|
def remote_users(%User{id: user_id}, %{data: %{"to" => to} = data}) do
|
|
|
|
cc = Map.get(data, "cc", [])
|
|
|
|
|
|
|
|
bcc =
|
|
|
|
data
|
|
|
|
|> Map.get("bcc", [])
|
|
|
|
|> Enum.reduce([], fn ap_id, bcc ->
|
|
|
|
case Pleroma.List.get_by_ap_id(ap_id) do
|
|
|
|
%Pleroma.List{user_id: ^user_id} = list ->
|
|
|
|
{:ok, following} = Pleroma.List.get_following(list)
|
|
|
|
bcc ++ Enum.map(following, & &1.ap_id)
|
|
|
|
|
|
|
|
_ ->
|
|
|
|
bcc
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
[to, cc, bcc]
|
|
|
|
|> Enum.concat()
|
|
|
|
|> Enum.map(&User.get_cached_by_ap_id/1)
|
|
|
|
|> Enum.filter(fn user -> user && !user.local end)
|
|
|
|
end
|
2019-05-11 20:41:34 -06:00
|
|
|
end
|