2019-05-12 04:41:34 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.Federator.Publisher do
|
2019-05-12 05:55:17 +02:00
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Config
|
|
|
|
alias Pleroma.User
|
2019-05-12 05:43:53 +02:00
|
|
|
alias Pleroma.Web.Federator.RetryQueue
|
|
|
|
|
|
|
|
require Logger
|
|
|
|
|
2019-05-12 04:41:34 +02: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-05-12 05:43:53 +02:00
|
|
|
def enqueue_one(module, %{} = params),
|
|
|
|
do: PleromaJobQueue.enqueue(:federation_outgoing, __MODULE__, [:publish_one, module, params])
|
|
|
|
|
|
|
|
@spec perform(atom(), module(), any()) :: {:ok, any()} | {:error, any()}
|
|
|
|
def perform(:publish_one, module, params) do
|
|
|
|
case apply(module, :publish_one, [params]) do
|
|
|
|
{:ok, _} ->
|
|
|
|
:ok
|
|
|
|
|
2019-05-12 06:17:17 +02:00
|
|
|
{:error, _e} ->
|
2019-05-12 05:43:53 +02:00
|
|
|
RetryQueue.enqueue(params, module)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform(type, _, _) do
|
|
|
|
Logger.debug("Unknown task: #{type}")
|
|
|
|
{:error, "Don't know what to do with this"}
|
|
|
|
end
|
2019-05-12 05:55:17 +02:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Relays an activity to all specified peers.
|
|
|
|
"""
|
|
|
|
@callback publish(Pleroma.User.t(), Pleroma.Activity.t()) :: :ok | {:error, any()}
|
|
|
|
|
|
|
|
@spec publish(Pleroma.User.t(), Pleroma.Activity.t()) :: :ok
|
|
|
|
def publish(%User{} = user, %Activity{} = activity) do
|
|
|
|
Config.get([:instance, :federation_publisher_modules])
|
|
|
|
|> Enum.each(fn module ->
|
2019-05-12 06:17:17 +02:00
|
|
|
if module.is_representable?(activity) do
|
|
|
|
Logger.info("Publishing #{activity.data["id"]} using #{inspect(module)}")
|
|
|
|
module.publish(user, activity)
|
|
|
|
end
|
2019-05-12 05:55:17 +02:00
|
|
|
end)
|
|
|
|
|
|
|
|
:ok
|
|
|
|
end
|
2019-05-12 21:05:03 +02:00
|
|
|
|
|
|
|
@doc """
|
|
|
|
Gathers links used by an outgoing federation module for WebFinger output.
|
|
|
|
"""
|
|
|
|
@callback gather_webfinger_links(Pleroma.User.t()) :: list()
|
|
|
|
|
|
|
|
@spec gather_webfinger_links(Pleroma.User.t()) :: list()
|
|
|
|
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 21:15:29 +02: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-05-12 04:41:34 +02:00
|
|
|
end
|