1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
19 Returns `true` if the distance to target object does not exceed max configured value.
20 Serves to prevent fetching of very long threads, especially useful on smaller instances.
21 Addresses [memory leaks on recursive replies fetching](https://git.pleroma.social/pleroma/pleroma/issues/161).
22 Applies to fetching of both ancestor (reply-to) and child (reply) objects.
24 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
25 def allowed_thread_distance?(distance) do
26 max_distance = Pleroma.Config.get([:instance, :federation_incoming_replies_max_depth])
28 if max_distance && max_distance >= 0 do
29 # Default depth is 0 (an object has zero distance from itself in its thread)
30 (distance || 0) <= max_distance
38 def incoming_ap_doc(params) do
39 ReceiverWorker.enqueue("incoming_ap_doc", %{"params" => params})
42 def publish(%{id: "pleroma:fakeid"} = activity) do
43 perform(:publish, activity)
46 def publish(activity) do
47 PublisherWorker.enqueue("publish", %{"activity_id" => activity.id})
50 # Job Worker Callbacks
52 @spec perform(atom(), module(), any()) :: {:ok, any()} | {:error, any()}
53 def perform(:publish_one, module, params) do
54 apply(module, :publish_one, [params])
57 def perform(:publish, activity) do
58 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
60 with %User{} = actor <- User.get_cached_by_ap_id(activity.data["actor"]),
61 {:ok, actor} <- User.ensure_keys_present(actor) do
62 Publisher.publish(actor, activity)
66 def perform(:incoming_ap_doc, params) do
67 Logger.debug("Handling incoming AP activity")
69 params = Utils.normalize_params(params)
71 # NOTE: we use the actor ID to do the containment, this is fine because an
72 # actor shouldn't be acting on objects outside their own AP server.
73 with {:ok, _user} <- ap_enabled_actor(params["actor"]),
74 nil <- Activity.normalize(params["id"]),
76 {:correct_origin?, Containment.contain_origin_from_id(params["actor"], params)},
77 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
80 {:correct_origin?, _} ->
81 Logger.debug("Origin containment failure for #{params["id"]}")
82 {:error, :origin_containment_failed}
85 Logger.debug("Already had #{params["id"]}")
86 {:error, :already_present}
89 # Just drop those for now
90 Logger.debug("Unhandled activity")
91 Logger.debug(Jason.encode!(params, pretty: true))
96 def ap_enabled_actor(id) do
97 user = User.get_cached_by_ap_id(id)
99 if User.ap_enabled?(user) do
102 ActivityPub.make_user_from_ap_id(id)