Merge branch 'issue/2069' into 'develop'
[akkoma] / lib / pleroma / web / 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 actor =
70 params
71 |> Map.get("actor")
72 |> Utils.get_ap_id()
73
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"]),
78 {_, :ok} <-
79 {:correct_origin?, Containment.contain_origin_from_id(actor, params)},
80 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
81 {:ok, activity}
82 else
83 {:correct_origin?, _} ->
84 Logger.debug("Origin containment failure for #{params["id"]}")
85 {:error, :origin_containment_failed}
86
87 %Activity{} ->
88 Logger.debug("Already had #{params["id"]}")
89 {:error, :already_present}
90
91 {:actor, e} ->
92 Logger.debug("Unhandled actor #{actor}, #{inspect(e)}")
93 {:error, e}
94
95 e ->
96 # Just drop those for now
97 Logger.debug(fn -> "Unhandled activity\n" <> Jason.encode!(params, pretty: true) end)
98 {:error, e}
99 end
100 end
101
102 def ap_enabled_actor(id) do
103 user = User.get_cached_by_ap_id(id)
104
105 if User.ap_enabled?(user) do
106 {:ok, user}
107 else
108 ActivityPub.make_user_from_ap_id(id)
109 end
110 end
111 end