From 2eb5c453ad71417bebcc45714c0c318bc956c336 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Sat, 23 Dec 2023 23:07:35 -0600 Subject: [PATCH] Allow configuring the origin of the service actor --- config/config.exs | 3 ++- .../activity_pub/activity_pub_controller.ex | 12 +++++++++-- .../web/activity_pub/internal_fetch_actor.ex | 20 ++++++++++++++++--- .../activity_pub_controller_test.exs | 2 +- 4 files changed, 30 insertions(+), 7 deletions(-) diff --git a/config/config.exs b/config/config.exs index e1d45fd7c2..6ebfb4adcf 100644 --- a/config/config.exs +++ b/config/config.exs @@ -375,7 +375,8 @@ follow_handshake_timeout: 500, note_replies_output_limit: 5, sign_object_fetches: true, - authorized_fetch_mode: false + authorized_fetch_mode: false, + fetch_actor_origin: nil config :pleroma, :streamer, workers: 3, diff --git a/lib/pleroma/web/activity_pub/activity_pub_controller.ex b/lib/pleroma/web/activity_pub/activity_pub_controller.ex index ae8e939f40..9f1a723426 100644 --- a/lib/pleroma/web/activity_pub/activity_pub_controller.ex +++ b/lib/pleroma/web/activity_pub/activity_pub_controller.ex @@ -334,8 +334,16 @@ def relay(conn, _params) do |> represent_service_actor(conn) end - def internal_fetch(conn, _params) do - InternalFetchActor.get_actor() + def internal_fetch(%Plug.Conn{host: host} = conn, _params) do + with fetch_actor_origin when is_binary(fetch_actor_origin) <- + Pleroma.Config.get([:activitypub, :fetch_actor_origin]), + %URI{host: fetch_actor_host} <- URI.parse(fetch_actor_origin), + true <- host == fetch_actor_host do + fetch_actor_origin + else + _ -> Pleroma.Web.Endpoint.url() + end + |> InternalFetchActor.get_actor() |> represent_service_actor(conn) end diff --git a/lib/pleroma/web/activity_pub/internal_fetch_actor.ex b/lib/pleroma/web/activity_pub/internal_fetch_actor.ex index 0837238945..1c12859fd6 100644 --- a/lib/pleroma/web/activity_pub/internal_fetch_actor.ex +++ b/lib/pleroma/web/activity_pub/internal_fetch_actor.ex @@ -11,10 +11,24 @@ def init do # Wait for everything to settle. Process.sleep(1000 * 5) get_actor() + get_actor(Pleroma.Web.Endpoint.url()) end - def get_actor do - "#{Pleroma.Web.Endpoint.url()}/internal/fetch" - |> User.get_or_create_service_actor_by_ap_id("internal.fetch") + def get_actor(origin) do + %URI{host: host} = URI.parse(origin) + + nickname = + cond do + host == Pleroma.Web.Endpoint.host() -> "internal.fetch" + true -> "internal.fetch@#{host}" + end + + "#{origin}/internal/fetch" + |> User.get_or_create_service_actor_by_ap_id(nickname) + end + + def get_actor() do + (Pleroma.Config.get([:activitypub, :fetch_actor_origin]) || Pleroma.Web.Endpoint.url()) + |> get_actor() end end diff --git a/test/pleroma/web/activity_pub/activity_pub_controller_test.exs b/test/pleroma/web/activity_pub/activity_pub_controller_test.exs index 0dc61c2e56..ce5e1e19c7 100644 --- a/test/pleroma/web/activity_pub/activity_pub_controller_test.exs +++ b/test/pleroma/web/activity_pub/activity_pub_controller_test.exs @@ -75,7 +75,7 @@ test "it returns the internal fetch user", %{conn: conn} do |> get(activity_pub_path(conn, :internal_fetch)) |> json_response(200) - assert res["id"] =~ "/fetch" + assert res["id"] =~ "http://localhost:4001/internal/fetch" end test "on non-federating instance, it returns 404", %{conn: conn} do