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")
74 # NOTE: we use the actor ID to do the containment, this is fine because an
75 # actor shouldn't be acting on objects outside their own AP server.
76 with {_, {:ok, _user}} <- {:actor, ap_enabled_actor(actor)},
77 nil <- Activity.normalize(params["id"]),
79 {:correct_origin?, Containment.contain_origin_from_id(actor, params)},
80 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
83 {:correct_origin?, _} ->
84 Logger.debug("Origin containment failure for #{params["id"]}")
85 {:error, :origin_containment_failed}
88 Logger.debug("Already had #{params["id"]}")
89 {:error, :already_present}
92 Logger.debug("Unhandled actor #{actor}, #{inspect(e)}")
96 # Just drop those for now
97 Logger.debug(fn -> "Unhandled activity\n" <> Jason.encode!(params, pretty: true) end)
102 def ap_enabled_actor(id) do
103 user = User.get_cached_by_ap_id(id)
105 if User.ap_enabled?(user) do
108 ActivityPub.make_user_from_ap_id(id)