1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.Federator do
7 alias Pleroma.Object.Containment
9 alias Pleroma.Web.ActivityPub.Publisher
10 alias Pleroma.Web.ActivityPub.Transmogrifier
11 alias Pleroma.Web.ActivityPub.Utils
12 alias Pleroma.Workers.PublisherWorker
13 alias Pleroma.Workers.ReceiverWorker
17 @behaviour Pleroma.Web.Federator.Publishing
20 Returns `true` if the distance to target object does not exceed max configured value.
21 Serves to prevent fetching of very long threads, especially useful on smaller instances.
22 Addresses [memory leaks on recursive replies fetching](https://git.pleroma.social/pleroma/pleroma/issues/161).
23 Applies to fetching of both ancestor (reply-to) and child (reply) objects.
25 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
26 def allowed_thread_distance?(distance) do
27 max_distance = Pleroma.Config.get([:instance, :federation_incoming_replies_max_depth])
29 if max_distance && max_distance >= 0 do
30 # Default depth is 0 (an object has zero distance from itself in its thread)
31 (distance || 0) <= max_distance
38 def incoming_ap_doc(%{params: params, req_headers: req_headers}) do
39 ReceiverWorker.enqueue(
41 %{"req_headers" => req_headers, "params" => params, "timeout" => :timer.seconds(20)},
46 def incoming_ap_doc(%{"type" => "Delete"} = params) do
47 ReceiverWorker.enqueue("incoming_ap_doc", %{"params" => params}, priority: 3)
50 def incoming_ap_doc(params) do
51 ReceiverWorker.enqueue("incoming_ap_doc", %{"params" => params})
55 def publish(%{id: "pleroma:fakeid"} = activity) do
56 perform(:publish, activity)
60 def publish(%Pleroma.Activity{data: %{"type" => type}} = activity) do
61 PublisherWorker.enqueue("publish", %{"activity_id" => activity.id},
62 priority: publish_priority(type)
66 defp publish_priority("Delete"), do: 3
67 defp publish_priority(_), do: 0
69 # Job Worker Callbacks
71 @spec perform(atom(), any()) :: {:ok, any()} | {:error, any()}
72 def perform(:publish_one, params), do: Publisher.publish_one(params)
74 def perform(:publish, activity) do
75 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
77 %User{} = actor = User.get_cached_by_ap_id(activity.data["actor"])
78 Publisher.publish(actor, activity)
81 def perform(:incoming_ap_doc, params) do
82 Logger.debug("Handling incoming AP activity")
89 # NOTE: we use the actor ID to do the containment, this is fine because an
90 # actor shouldn't be acting on objects outside their own AP server.
91 with {_, {:ok, _user}} <- {:actor, User.get_or_fetch_by_ap_id(actor)},
92 nil <- Activity.normalize(params["id"]),
94 {:correct_origin?, Containment.contain_origin_from_id(actor, params)},
95 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
98 {:correct_origin?, _} ->
99 Logger.debug("Origin containment failure for #{params["id"]}")
100 {:error, :origin_containment_failed}
103 Logger.debug("Already had #{params["id"]}")
104 {:error, :already_present}
107 Logger.debug("Unhandled actor #{actor}, #{inspect(e)}")
110 {:error, {:validate_object, _}} = e ->
111 Logger.error("Incoming AP doc validation error: #{inspect(e)}")
112 Logger.debug(Jason.encode!(params, pretty: true))
116 # Just drop those for now
117 Logger.debug(fn -> "Unhandled activity\n" <> Jason.encode!(params, pretty: true) end)