Retire the Pleroma.Web.Federator.Publisher module

This commit is contained in:
Mark Felder 2023-12-28 14:53:09 -05:00
parent 1d816222e0
commit 3acfdb6f8a
4 changed files with 55 additions and 87 deletions

View File

@ -13,19 +13,56 @@ defmodule Pleroma.Web.ActivityPub.Publisher do
alias Pleroma.User
alias Pleroma.Web.ActivityPub.Relay
alias Pleroma.Web.ActivityPub.Transmogrifier
alias Pleroma.Workers.PublisherWorker
require Pleroma.Constants
import Pleroma.Web.ActivityPub.Visibility
@behaviour Pleroma.Web.Federator.Publisher
require Logger
@moduledoc """
ActivityPub outgoing federation module.
"""
@doc """
Enqueue publishing a single activity.
"""
@spec enqueue_one(Map.t(), Keyword.t()) :: {:ok, %Oban.Job{}}
def enqueue_one(%{} = params, worker_args \\ []) do
PublisherWorker.enqueue(
"publish_one",
%{"params" => params},
worker_args
)
end
@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
@doc """
Determine if an activity can be represented by running it through Transmogrifier.
"""
@ -138,7 +175,7 @@ defp recipients(actor, activity) do
[]
end
mentioned = Pleroma.Web.Federator.Publisher.remote_users(actor, activity)
mentioned = remote_users(actor, activity)
non_mentioned = (followers ++ fetchers) -- mentioned
[mentioned, non_mentioned]
@ -223,7 +260,7 @@ def publish(%User{} = actor, %{data: %{"bcc" => bcc}} = activity)
|> Map.put("cc", cc)
|> Jason.encode!()
Pleroma.Web.Federator.Publisher.enqueue_one(__MODULE__, %{
__MODULE__.enqueue_one(%{
inbox: inbox,
json: json,
actor_id: actor.id,
@ -262,8 +299,7 @@ def publish(%User{} = actor, %Activity{} = activity) do
inboxes
|> Instances.filter_reachable()
|> Enum.each(fn {inbox, unreachable_since} ->
Pleroma.Web.Federator.Publisher.enqueue_one(
__MODULE__,
__MODULE__.enqueue_one(
%{
inbox: inbox,
json: json,

View File

@ -1,64 +0,0 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.Federator.Publisher do
alias Pleroma.User
alias Pleroma.Workers.PublisherWorker
require Logger
@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(), Keyword.t()) :: {:ok, %Oban.Job{}}
def enqueue_one(module, %{} = params, worker_args \\ []) do
PublisherWorker.enqueue(
"publish_one",
%{"module" => to_string(module), "params" => params},
worker_args
)
end
@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
end

View File

@ -18,9 +18,9 @@ def perform(%Job{args: %{"op" => "publish", "activity_id" => activity_id}}) do
Federator.perform(:publish, activity)
end
def perform(%Job{args: %{"op" => "publish_one", "module" => module_name, "params" => params}}) do
def perform(%Job{args: %{"op" => "publish_one", "params" => params}}) do
params = Map.new(params, fn {k, v} -> {String.to_atom(k), v} end)
Federator.perform(:publish_one, String.to_atom(module_name), params)
Federator.perform(:publish_one, params)
end
@impl Oban.Worker

View File

@ -268,7 +268,7 @@ test "publish to url with with different ports" do
describe "publish/2" do
test_with_mock "doesn't publish a non-public activity to quarantined instances.",
Pleroma.Web.Federator.Publisher,
Pleroma.Web.ActivityPub.Publisher,
[:passthrough],
[] do
Config.put([:instance, :quarantined_instances], [{"domain.com", "some reason"}])
@ -295,7 +295,7 @@ test "publish to url with with different ports" do
assert res == :ok
assert not called(
Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
Publisher.enqueue_one(%{
inbox: "https://domain.com/users/nick1/inbox",
actor_id: actor.id,
id: note_activity.data["id"]
@ -304,7 +304,7 @@ test "publish to url with with different ports" do
end
test_with_mock "Publishes a non-public activity to non-quarantined instances.",
Pleroma.Web.Federator.Publisher,
Pleroma.Web.ActivityPub.Publisher,
[:passthrough],
[] do
Config.put([:instance, :quarantined_instances], [{"somedomain.com", "some reason"}])
@ -331,8 +331,7 @@ test "publish to url with with different ports" do
assert res == :ok
assert called(
Pleroma.Web.Federator.Publisher.enqueue_one(
Publisher,
Publisher.enqueue_one(
%{
inbox: "https://domain.com/users/nick1/inbox",
actor_id: actor.id,
@ -344,7 +343,7 @@ test "publish to url with with different ports" do
end
test_with_mock "Publishes to directly addressed actors with higher priority.",
Pleroma.Web.Federator.Publisher,
Pleroma.Web.ActivityPub.Publisher,
[:passthrough],
[] do
note_activity = insert(:direct_note_activity)
@ -356,8 +355,7 @@ test "publish to url with with different ports" do
assert res == :ok
assert called(
Pleroma.Web.Federator.Publisher.enqueue_one(
Publisher,
Publisher.enqueue_one(
%{
inbox: :_,
actor_id: actor.id,
@ -369,7 +367,7 @@ test "publish to url with with different ports" do
end
test_with_mock "publishes an activity with BCC to all relevant peers.",
Pleroma.Web.Federator.Publisher,
Pleroma.Web.ActivityPub.Publisher,
[:passthrough],
[] do
follower =
@ -393,7 +391,7 @@ test "publish to url with with different ports" do
assert res == :ok
assert called(
Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
Publisher.enqueue_one(%{
inbox: "https://domain.com/users/nick1/inbox",
actor_id: actor.id,
id: note_activity.data["id"]
@ -402,7 +400,7 @@ test "publish to url with with different ports" do
end
test_with_mock "publishes a delete activity to peers who signed fetch requests to the create acitvity/object.",
Pleroma.Web.Federator.Publisher,
Pleroma.Web.ActivityPub.Publisher,
[:passthrough],
[] do
fetcher =
@ -443,8 +441,7 @@ test "publish to url with with different ports" do
assert res == :ok
assert called(
Pleroma.Web.Federator.Publisher.enqueue_one(
Publisher,
Publisher.enqueue_one(
%{
inbox: "https://domain.com/users/nick1/inbox",
actor_id: actor.id,
@ -455,8 +452,7 @@ test "publish to url with with different ports" do
)
assert called(
Pleroma.Web.Federator.Publisher.enqueue_one(
Publisher,
Publisher.enqueue_one(
%{
inbox: "https://domain2.com/users/nick1/inbox",
actor_id: actor.id,