Merge branch 'bugfix/1543-streaming-boosts' into 'develop'
[akkoma] / lib / pleroma / web / federator / federator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.Federator do
6 alias Pleroma.Activity
7 alias Pleroma.Object.Containment
8 alias Pleroma.User
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
15
16 require Logger
17
18 @doc """
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.
23 """
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])
27
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
31 else
32 true
33 end
34 end
35
36 # Client API
37
38 def incoming_ap_doc(params) do
39 ReceiverWorker.enqueue("incoming_ap_doc", %{"params" => params})
40 end
41
42 def publish(%{id: "pleroma:fakeid"} = activity) do
43 perform(:publish, activity)
44 end
45
46 def publish(activity) do
47 PublisherWorker.enqueue("publish", %{"activity_id" => activity.id})
48 end
49
50 # Job Worker Callbacks
51
52 @spec perform(atom(), module(), any()) :: {:ok, any()} | {:error, any()}
53 def perform(:publish_one, module, params) do
54 apply(module, :publish_one, [params])
55 end
56
57 def perform(:publish, activity) do
58 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
59
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)
63 end
64 end
65
66 def perform(:incoming_ap_doc, params) do
67 Logger.debug("Handling incoming AP activity")
68
69 params = Utils.normalize_params(params)
70
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"]),
75 {_, :ok} <-
76 {:correct_origin?, Containment.contain_origin_from_id(params["actor"], params)},
77 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
78 {:ok, activity}
79 else
80 {:correct_origin?, _} ->
81 Logger.debug("Origin containment failure for #{params["id"]}")
82 {:error, :origin_containment_failed}
83
84 %Activity{} ->
85 Logger.debug("Already had #{params["id"]}")
86 {:error, :already_present}
87
88 e ->
89 # Just drop those for now
90 Logger.debug("Unhandled activity")
91 Logger.debug(Jason.encode!(params, pretty: true))
92 {:error, e}
93 end
94 end
95
96 def ap_enabled_actor(id) do
97 user = User.get_cached_by_ap_id(id)
98
99 if User.ap_enabled?(user) do
100 {:ok, user}
101 else
102 ActivityPub.make_user_from_ap_id(id)
103 end
104 end
105 end