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.ActivityPub
10 alias Pleroma.Web.ActivityPub.Transmogrifier
11 alias Pleroma.Web.ActivityPub.Utils
12 alias Pleroma.Web.Federator.Publisher
13 alias Pleroma.Workers.PublisherWorker
14 alias Pleroma.Workers.ReceiverWorker
18 @behaviour Pleroma.Web.Federator.Publishing
21 Returns `true` if the distance to target object does not exceed max configured value.
22 Serves to prevent fetching of very long threads, especially useful on smaller instances.
23 Addresses [memory leaks on recursive replies fetching](https://git.pleroma.social/pleroma/pleroma/issues/161).
24 Applies to fetching of both ancestor (reply-to) and child (reply) objects.
26 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
27 def allowed_thread_distance?(distance) do
28 max_distance = Pleroma.Config.get([:instance, :federation_incoming_replies_max_depth])
30 if max_distance && max_distance >= 0 do
31 # Default depth is 0 (an object has zero distance from itself in its thread)
32 (distance || 0) <= max_distance
40 def incoming_ap_doc(params) do
41 ReceiverWorker.enqueue("incoming_ap_doc", %{"params" => params})
45 def publish(%{id: "pleroma:fakeid"} = activity) do
46 perform(:publish, activity)
50 def publish(%Pleroma.Activity{data: %{"type" => type}} = activity) do
51 PublisherWorker.enqueue("publish", %{"activity_id" => activity.id},
52 priority: publish_priority(type)
56 defp publish_priority("Delete"), do: 3
57 defp publish_priority(_), do: 0
59 # Job Worker Callbacks
61 @spec perform(atom(), module(), any()) :: {:ok, any()} | {:error, any()}
62 def perform(:publish_one, module, params) do
63 apply(module, :publish_one, [params])
66 def perform(:publish, activity) do
67 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
69 %User{} = actor = User.get_cached_by_ap_id(activity.data["actor"])
70 Publisher.publish(actor, activity)
73 def perform(:incoming_ap_doc, params) do
74 Logger.debug("Handling incoming AP activity")
81 # NOTE: we use the actor ID to do the containment, this is fine because an
82 # actor shouldn't be acting on objects outside their own AP server.
83 with {_, {:ok, _user}} <- {:actor, ap_enabled_actor(actor)},
84 nil <- Activity.normalize(params["id"]),
86 {:correct_origin?, Containment.contain_origin_from_id(actor, params)},
87 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
90 {:correct_origin?, _} ->
91 Logger.debug("Origin containment failure for #{params["id"]}")
92 {:error, :origin_containment_failed}
95 Logger.debug("Already had #{params["id"]}")
96 {:error, :already_present}
99 Logger.debug("Unhandled actor #{actor}, #{inspect(e)}")
102 {:error, {:validate_object, _}} = e ->
103 Logger.error("Incoming AP doc validation error: #{inspect(e)}")
104 Logger.debug(Jason.encode!(params, pretty: true))
108 # Just drop those for now
109 Logger.debug(fn -> "Unhandled activity\n" <> Jason.encode!(params, pretty: true) end)
114 def ap_enabled_actor(id) do
115 user = User.get_cached_by_ap_id(id)
117 if User.ap_enabled?(user) do
120 ActivityPub.make_user_from_ap_id(id)