1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.Web.OStatus
14 alias Pleroma.Workers.PublisherWorker
15 alias Pleroma.Workers.ReceiverWorker
19 @doc "Addresses [memory leaks on recursive replies fetching](https://git.pleroma.social/pleroma/pleroma/issues/161)"
20 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
21 def allowed_incoming_reply_depth?(depth) do
22 max_replies_depth = Pleroma.Config.get([:instance, :federation_incoming_replies_max_depth])
24 if max_replies_depth do
25 (depth || 1) <= max_replies_depth
33 def incoming_doc(doc) do
34 ReceiverWorker.enqueue("incoming_doc", %{"body" => doc})
37 def incoming_ap_doc(params) do
38 ReceiverWorker.enqueue("incoming_ap_doc", %{"params" => params})
41 def publish(%{id: "pleroma:fakeid"} = activity) do
42 perform(:publish, activity)
45 def publish(activity) do
46 PublisherWorker.enqueue("publish", %{"activity_id" => activity.id})
49 # Job Worker Callbacks
51 @spec perform(atom(), module(), any()) :: {:ok, any()} | {:error, any()}
52 def perform(:publish_one, module, params) do
53 apply(module, :publish_one, [params])
56 def perform(:publish, activity) do
57 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
59 with %User{} = actor <- User.get_cached_by_ap_id(activity.data["actor"]),
60 {:ok, actor} <- User.ensure_keys_present(actor) do
61 Publisher.publish(actor, activity)
65 def perform(:incoming_doc, doc) do
66 Logger.info("Got document, trying to parse")
67 OStatus.handle_incoming(doc)
70 def perform(:incoming_ap_doc, params) do
71 Logger.info("Handling incoming AP activity")
73 params = Utils.normalize_params(params)
75 # NOTE: we use the actor ID to do the containment, this is fine because an
76 # actor shouldn't be acting on objects outside their own AP server.
77 with {:ok, _user} <- ap_enabled_actor(params["actor"]),
78 nil <- Activity.normalize(params["id"]),
79 :ok <- Containment.contain_origin_from_id(params["actor"], params),
80 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
84 Logger.info("Already had #{params["id"]}")
88 # Just drop those for now
89 Logger.info("Unhandled activity")
90 Logger.info(Jason.encode!(params, pretty: true))
95 def ap_enabled_actor(id) do
96 user = User.get_cached_by_ap_id(id)
98 if User.ap_enabled?(user) do
101 ActivityPub.make_user_from_ap_id(id)