Merge pull request 'Always prefer shared inbox' (#881) from Oneric/akkoma:shared-inbox into develop

Reviewed-on: https://akkoma.dev/AkkomaGang/akkoma/pulls/881
This commit is contained in:
floatingghost 2025-05-09 16:54:19 +00:00
commit fffac8bc7c
3 changed files with 99 additions and 185 deletions

View file

@ -47,7 +47,9 @@ def is_representable?(%Activity{} = activity) do
* `actor`: the actor which is signing the message * `actor`: the actor which is signing the message
* `id`: the ActivityStreams URI of the message * `id`: the ActivityStreams URI of the message
""" """
def publish_one(%{inbox: inbox, json: json, actor: %User{} = actor, id: id} = params) do def publish_one(
%{"inbox" => inbox, "json" => json, "actor" => %User{} = actor, "id" => id} = params
) do
Logger.debug("Federating #{id} to #{inbox}") Logger.debug("Federating #{id} to #{inbox}")
uri = %{path: path} = URI.parse(inbox) uri = %{path: path} = URI.parse(inbox)
digest = "SHA-256=" <> (:crypto.hash(:sha256, json) |> Base.encode64()) digest = "SHA-256=" <> (:crypto.hash(:sha256, json) |> Base.encode64())
@ -74,24 +76,24 @@ def publish_one(%{inbox: inbox, json: json, actor: %User{} = actor, id: id} = pa
{"digest", digest} {"digest", digest}
] ]
) do ) do
if not Map.has_key?(params, :unreachable_since) || params[:unreachable_since] do if not Map.has_key?(params, "unreachable_since") || params["unreachable_since"] do
Instances.set_reachable(inbox) Instances.set_reachable(inbox)
end end
result result
else else
{_post_result, response} -> {_post_result, response} ->
unless params[:unreachable_since], do: Instances.set_unreachable(inbox) unless params["unreachable_since"], do: Instances.set_unreachable(inbox)
{:error, response} {:error, response}
end end
end end
def publish_one(%{actor_id: actor_id} = params) do def publish_one(%{"actor_id" => actor_id} = params) do
actor = User.get_cached_by_id(actor_id) actor = User.get_cached_by_id(actor_id)
params params
|> Map.delete(:actor_id) |> Map.delete("actor_id")
|> Map.put(:actor, actor) |> Map.put("actor", actor)
|> publish_one() |> publish_one()
end end
@ -170,42 +172,8 @@ defp get_cc_ap_ids(ap_id, recipients) do
|> Enum.map(& &1.ap_id) |> Enum.map(& &1.ap_id)
end end
defp maybe_use_sharedinbox(%User{shared_inbox: nil, inbox: inbox}), do: inbox defp try_sharedinbox(%User{shared_inbox: nil, inbox: inbox}), do: inbox
defp maybe_use_sharedinbox(%User{shared_inbox: shared_inbox}), do: shared_inbox defp try_sharedinbox(%User{shared_inbox: shared_inbox}), do: shared_inbox
@doc """
Determine a user inbox to use based on heuristics. These heuristics
are based on an approximation of the ``sharedInbox`` rules in the
[ActivityPub specification][ap-sharedinbox].
Please do not edit this function (or its children) without reading
the spec, as editing the code is likely to introduce some breakage
without some familiarity.
[ap-sharedinbox]: https://www.w3.org/TR/activitypub/#shared-inbox-delivery
"""
def determine_inbox(
%Activity{data: activity_data},
%User{inbox: inbox} = user
) do
to = activity_data["to"] || []
cc = activity_data["cc"] || []
type = activity_data["type"]
cond do
type == "Delete" ->
maybe_use_sharedinbox(user)
Pleroma.Constants.as_public() in to || Pleroma.Constants.as_public() in cc ->
maybe_use_sharedinbox(user)
length(to) + length(cc) > 1 ->
maybe_use_sharedinbox(user)
true ->
inbox
end
end
@doc """ @doc """
Publishes an activity with BCC to all relevant peers. Publishes an activity with BCC to all relevant peers.
@ -237,11 +205,11 @@ def publish(%User{} = actor, %{data: %{"bcc" => bcc}} = activity)
|> Jason.encode!() |> Jason.encode!()
Pleroma.Web.Federator.Publisher.enqueue_one(__MODULE__, %{ Pleroma.Web.Federator.Publisher.enqueue_one(__MODULE__, %{
inbox: inbox, "inbox" => inbox,
json: json, "json" => json,
actor_id: actor.id, "actor_id" => actor.id,
id: activity.data["id"], "id" => activity.data["id"],
unreachable_since: unreachable_since "unreachable_since" => unreachable_since
}) })
end) end)
end) end)
@ -261,7 +229,7 @@ def publish(%User{} = actor, %Activity{} = activity) do
recipients(actor, activity) recipients(actor, activity)
|> Enum.map(fn %User{} = user -> |> Enum.map(fn %User{} = user ->
determine_inbox(activity, user) try_sharedinbox(user)
end) end)
|> Enum.uniq() |> Enum.uniq()
|> Enum.filter(fn inbox -> should_federate?(inbox) end) |> Enum.filter(fn inbox -> should_federate?(inbox) end)
@ -270,11 +238,11 @@ def publish(%User{} = actor, %Activity{} = activity) do
Pleroma.Web.Federator.Publisher.enqueue_one( Pleroma.Web.Federator.Publisher.enqueue_one(
__MODULE__, __MODULE__,
%{ %{
inbox: inbox, "inbox" => inbox,
json: json, "json" => json,
actor_id: actor.id, "actor_id" => actor.id,
id: activity.data["id"], "id" => activity.data["id"],
unreachable_since: unreachable_since "unreachable_since" => unreachable_since
} }
) )
end) end)

View file

@ -30,7 +30,6 @@ def perform(%Job{
end end
def perform(%Job{args: %{"op" => "publish_one", "module" => module_name, "params" => params}}) do def perform(%Job{args: %{"op" => "publish_one", "module" => module_name, "params" => params}}) do
params = Map.new(params, fn {k, v} -> {String.to_atom(k), v} end) Federator.perform(:publish_one, String.to_existing_atom(module_name), params)
Federator.perform(:publish_one, String.to_atom(module_name), params)
end end
end end

View file

@ -11,7 +11,6 @@ defmodule Pleroma.Web.ActivityPub.PublisherTest do
import Tesla.Mock import Tesla.Mock
import Mock import Mock
alias Pleroma.Activity
alias Pleroma.Instances alias Pleroma.Instances
alias Pleroma.Object alias Pleroma.Object
alias Pleroma.Web.ActivityPub.Publisher alias Pleroma.Web.ActivityPub.Publisher
@ -51,82 +50,6 @@ test "it returns links" do
end end
end end
describe "determine_inbox/2" do
test "it returns sharedInbox for messages involving as:Public in to" do
user = insert(:user, %{shared_inbox: "http://example.com/inbox"})
activity = %Activity{
data: %{"to" => [@as_public], "cc" => [user.follower_address]}
}
assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
end
test "it returns sharedInbox for messages involving as:Public in cc" do
user = insert(:user, %{shared_inbox: "http://example.com/inbox"})
activity = %Activity{
data: %{"cc" => [@as_public], "to" => [user.follower_address]}
}
assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
end
test "it returns sharedInbox for messages involving multiple recipients in to" do
user = insert(:user, %{shared_inbox: "http://example.com/inbox"})
user_two = insert(:user)
user_three = insert(:user)
activity = %Activity{
data: %{"cc" => [], "to" => [user.ap_id, user_two.ap_id, user_three.ap_id]}
}
assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
end
test "it returns sharedInbox for messages involving multiple recipients in cc" do
user = insert(:user, %{shared_inbox: "http://example.com/inbox"})
user_two = insert(:user)
user_three = insert(:user)
activity = %Activity{
data: %{"to" => [], "cc" => [user.ap_id, user_two.ap_id, user_three.ap_id]}
}
assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
end
test "it returns sharedInbox for messages involving multiple recipients in total" do
user =
insert(:user, %{
shared_inbox: "http://example.com/inbox",
inbox: "http://example.com/personal-inbox"
})
user_two = insert(:user)
activity = %Activity{
data: %{"to" => [user_two.ap_id], "cc" => [user.ap_id]}
}
assert Publisher.determine_inbox(activity, user) == "http://example.com/inbox"
end
test "it returns inbox for messages involving single recipients in total" do
user =
insert(:user, %{
shared_inbox: "http://example.com/inbox",
inbox: "http://example.com/personal-inbox"
})
activity = %Activity{
data: %{"to" => [user.ap_id], "cc" => []}
}
assert Publisher.determine_inbox(activity, user) == "http://example.com/personal-inbox"
end
end
describe "publish_one/1" do describe "publish_one/1" do
test "publish to url with with different ports" do test "publish to url with with different ports" do
inbox80 = "http://42.site/users/nick1/inbox" inbox80 = "http://42.site/users/nick1/inbox"
@ -146,20 +69,20 @@ test "publish to url with with different ports" do
assert {:ok, %{body: "port 42"}} = assert {:ok, %{body: "port 42"}} =
Publisher.publish_one(%{ Publisher.publish_one(%{
inbox: inbox42, "inbox" => inbox42,
json: "{}", "json" => "{}",
actor: actor, "actor" => actor,
id: 1, "id" => 1,
unreachable_since: true "unreachable_since" => true
}) })
assert {:ok, %{body: "port 80"}} = assert {:ok, %{body: "port 80"}} =
Publisher.publish_one(%{ Publisher.publish_one(%{
inbox: inbox80, "inbox" => inbox80,
json: "{}", "json" => "{}",
actor: actor, "actor" => actor,
id: 1, "id" => 1,
unreachable_since: true "unreachable_since" => true
}) })
end end
@ -173,7 +96,14 @@ test "publish to url with with different ports" do
inbox = "http://200.site/users/nick1/inbox" inbox = "http://200.site/users/nick1/inbox"
assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1}) assert {:ok, _} =
Publisher.publish_one(%{
"inbox" => inbox,
"json" => "{}",
"actor" => actor,
"id" => 1
})
assert called(Instances.set_reachable(inbox)) assert called(Instances.set_reachable(inbox))
end end
@ -189,11 +119,11 @@ test "publish to url with with different ports" do
assert {:ok, _} = assert {:ok, _} =
Publisher.publish_one(%{ Publisher.publish_one(%{
inbox: inbox, "inbox" => inbox,
json: "{}", "json" => "{}",
actor: actor, "actor" => actor,
id: 1, "id" => 1,
unreachable_since: NaiveDateTime.utc_now() "unreachable_since" => NaiveDateTime.utc_now()
}) })
assert called(Instances.set_reachable(inbox)) assert called(Instances.set_reachable(inbox))
@ -211,11 +141,11 @@ test "publish to url with with different ports" do
assert {:ok, _} = assert {:ok, _} =
Publisher.publish_one(%{ Publisher.publish_one(%{
inbox: inbox, "inbox" => inbox,
json: "{}", "json" => "{}",
actor: actor, "actor" => actor,
id: 1, "id" => 1,
unreachable_since: nil "unreachable_since" => nil
}) })
refute called(Instances.set_reachable(inbox)) refute called(Instances.set_reachable(inbox))
@ -231,7 +161,13 @@ test "publish to url with with different ports" do
inbox = "http://404.site/users/nick1/inbox" inbox = "http://404.site/users/nick1/inbox"
assert {:error, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1}) assert {:error, _} =
Publisher.publish_one(%{
"inbox" => inbox,
"json" => "{}",
"actor" => actor,
"id" => 1
})
assert called(Instances.set_unreachable(inbox)) assert called(Instances.set_unreachable(inbox))
end end
@ -248,7 +184,12 @@ test "publish to url with with different ports" do
assert capture_log(fn -> assert capture_log(fn ->
assert {:error, _} = assert {:error, _} =
Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1}) Publisher.publish_one(%{
"inbox" => inbox,
"json" => "{}",
"actor" => actor,
"id" => 1
})
end) =~ "connrefused" end) =~ "connrefused"
assert called(Instances.set_unreachable(inbox)) assert called(Instances.set_unreachable(inbox))
@ -264,7 +205,13 @@ test "publish to url with with different ports" do
inbox = "http://200.site/users/nick1/inbox" inbox = "http://200.site/users/nick1/inbox"
assert {:ok, _} = Publisher.publish_one(%{inbox: inbox, json: "{}", actor: actor, id: 1}) assert {:ok, _} =
Publisher.publish_one(%{
"inbox" => inbox,
"json" => "{}",
"actor" => actor,
"id" => 1
})
refute called(Instances.set_unreachable(inbox)) refute called(Instances.set_unreachable(inbox))
end end
@ -282,11 +229,11 @@ test "publish to url with with different ports" do
assert capture_log(fn -> assert capture_log(fn ->
assert {:error, _} = assert {:error, _} =
Publisher.publish_one(%{ Publisher.publish_one(%{
inbox: inbox, "inbox" => inbox,
json: "{}", "json" => "{}",
actor: actor, "actor" => actor,
id: 1, "id" => 1,
unreachable_since: NaiveDateTime.utc_now() "unreachable_since" => NaiveDateTime.utc_now()
}) })
end) =~ "connrefused" end) =~ "connrefused"
@ -344,33 +291,33 @@ test "publish to url with with different ports" do
assert not called( assert not called(
Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{ Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
inbox: "https://domain.com/users/nick1/inbox", "inbox" => "https://domain.com/users/nick1/inbox",
actor_id: actor.id, "actor_id" => actor.id,
id: note_activity.data["id"] "id" => note_activity.data["id"]
}) })
) )
assert not called( assert not called(
Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{ Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
inbox: "https://domain.com/users/nick1/inbox", "inbox" => "https://domain.com/users/nick1/inbox",
actor_id: actor.id, "actor_id" => actor.id,
id: public_note_activity.data["id"] "id" => public_note_activity.data["id"]
}) })
) )
assert not called( assert not called(
Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{ Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
inbox: "https://rejected.com/users/nick2/inbox", "inbox" => "https://rejected.com/users/nick2/inbox",
actor_id: actor.id, "actor_id" => actor.id,
id: note_activity.data["id"] "id" => note_activity.data["id"]
}) })
) )
assert not called( assert not called(
Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{ Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
inbox: "https://rejected.com/users/nick2/inbox", "inbox" => "https://rejected.com/users/nick2/inbox",
actor_id: actor.id, "actor_id" => actor.id,
id: public_note_activity.data["id"] "id" => public_note_activity.data["id"]
}) })
) )
end end
@ -406,9 +353,9 @@ test "publish to url with with different ports" do
assert called( assert called(
Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{ Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
inbox: "https://domain.com/users/nick1/inbox", "inbox" => "https://domain.com/users/nick1/inbox",
actor_id: actor.id, "actor_id" => actor.id,
id: note_activity.data["id"] "id" => note_activity.data["id"]
}) })
) )
end end
@ -441,9 +388,9 @@ test "publish to url with with different ports" do
assert called( assert called(
Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{ Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
inbox: "https://domain.com/users/nick1/inbox", "inbox" => "https://domain.com/users/nick1/inbox",
actor_id: actor.id, "actor_id" => actor.id,
id: note_activity.data["id"] "id" => note_activity.data["id"]
}) })
) )
end end
@ -493,17 +440,17 @@ test "publish to url with with different ports" do
assert called( assert called(
Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{ Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
inbox: "https://domain.com/users/nick1/inbox", "inbox" => "https://domain.com/users/nick1/inbox",
actor_id: actor.id, "actor_id" => actor.id,
id: delete.data["id"] "id" => delete.data["id"]
}) })
) )
assert called( assert called(
Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{ Pleroma.Web.Federator.Publisher.enqueue_one(Publisher, %{
inbox: "https://domain2.com/users/nick1/inbox", "inbox" => "https://domain2.com/users/nick1/inbox",
actor_id: actor.id, "actor_id" => actor.id,
id: delete.data["id"] "id" => delete.data["id"]
}) })
) )
end end