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_ap_doc(params) do
34 ReceiverWorker.enqueue("incoming_ap_doc", %{"params" => params})
37 def publish(%{id: "pleroma:fakeid"} = activity) do
38 perform(:publish, activity)
41 def publish(activity) do
42 PublisherWorker.enqueue("publish", %{"activity_id" => activity.id})
45 # Job Worker Callbacks
47 @spec perform(atom(), module(), any()) :: {:ok, any()} | {:error, any()}
48 def perform(:publish_one, module, params) do
49 apply(module, :publish_one, [params])
52 def perform(:publish, activity) do
53 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
55 with %User{} = actor <- User.get_cached_by_ap_id(activity.data["actor"]),
56 {:ok, actor} <- User.ensure_keys_present(actor) do
57 Publisher.publish(actor, activity)
61 def perform(:incoming_ap_doc, params) do
62 Logger.info("Handling incoming AP activity")
64 params = Utils.normalize_params(params)
66 # NOTE: we use the actor ID to do the containment, this is fine because an
67 # actor shouldn't be acting on objects outside their own AP server.
68 with {:ok, _user} <- ap_enabled_actor(params["actor"]),
69 nil <- Activity.normalize(params["id"]),
70 :ok <- Containment.contain_origin_from_id(params["actor"], params),
71 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
75 Logger.info("Already had #{params["id"]}")
79 # Just drop those for now
80 Logger.info("Unhandled activity")
81 Logger.info(Jason.encode!(params, pretty: true))
86 def ap_enabled_actor(id) do
87 user = User.get_cached_by_ap_id(id)
89 if User.ap_enabled?(user) do
92 ActivityPub.make_user_from_ap_id(id)