770044de26bbe38566053df5dd99886c3c28bd9b
[akkoma] / lib / pleroma / web / federator.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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 @behaviour Pleroma.Web.Federator.Publishing
19
20 @doc """
21 Returns `true` if the distance to target object does not exceed max configured value.
22 Serves to prevent fetching of very long threads, especially useful on smaller instances.
23 Addresses [memory leaks on recursive replies fetching](https://git.pleroma.social/pleroma/pleroma/issues/161).
24 Applies to fetching of both ancestor (reply-to) and child (reply) objects.
25 """
26 # credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
27 def allowed_thread_distance?(distance) do
28 max_distance = Pleroma.Config.get([:instance, :federation_incoming_replies_max_depth])
29
30 if max_distance && max_distance >= 0 do
31 # Default depth is 0 (an object has zero distance from itself in its thread)
32 (distance || 0) <= max_distance
33 else
34 true
35 end
36 end
37
38 # Client API
39
40 def incoming_ap_doc(params) do
41 ReceiverWorker.enqueue("incoming_ap_doc", %{"params" => params})
42 end
43
44 @impl true
45 def publish(%{id: "pleroma:fakeid"} = activity) do
46 perform(:publish, activity)
47 end
48
49 @impl true
50 def publish(%{data: %{"object" => object}} = activity) when is_binary(object) do
51 PublisherWorker.enqueue("publish", %{"activity_id" => activity.id, "object_data" => nil})
52 end
53
54 @impl true
55 def publish(%{data: %{"object" => object}} = activity) when is_map(object) or is_list(object) do
56 PublisherWorker.enqueue(
57 "publish",
58 %{
59 "activity_id" => activity.id,
60 "object_data" => Jason.encode!(object)
61 },
62 priority: publish_priority(activity)
63 )
64 end
65
66 defp publish_priority(%{type: "Delete"}), do: 3
67 defp publish_priority(_), do: 0
68
69 # Job Worker Callbacks
70
71 @spec perform(atom(), module(), any()) :: {:ok, any()} | {:error, any()}
72 def perform(:publish_one, module, params) do
73 apply(module, :publish_one, [params])
74 end
75
76 def perform(:publish, activity) do
77 Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
78
79 %User{} = actor = User.get_cached_by_ap_id(activity.data["actor"])
80 Publisher.publish(actor, activity)
81 end
82
83 def perform(:incoming_ap_doc, params) do
84 Logger.debug("Handling incoming AP activity")
85
86 actor =
87 params
88 |> Map.get("actor")
89 |> Utils.get_ap_id()
90
91 # NOTE: we use the actor ID to do the containment, this is fine because an
92 # actor shouldn't be acting on objects outside their own AP server.
93 with {_, {:ok, _user}} <- {:actor, ap_enabled_actor(actor)},
94 nil <- Activity.normalize(params["id"]),
95 {_, :ok} <-
96 {:correct_origin?, Containment.contain_origin_from_id(actor, params)},
97 {:ok, activity} <- Transmogrifier.handle_incoming(params) do
98 {:ok, activity}
99 else
100 {:correct_origin?, _} ->
101 Logger.debug("Origin containment failure for #{params["id"]}")
102 {:error, :origin_containment_failed}
103
104 %Activity{} ->
105 Logger.debug("Already had #{params["id"]}")
106 {:error, :already_present}
107
108 {:actor, e} ->
109 Logger.debug("Unhandled actor #{actor}, #{inspect(e)}")
110 {:error, e}
111
112 {:error, {:validate_object, _}} = e ->
113 Logger.error("Incoming AP doc validation error: #{inspect(e)}")
114 Logger.debug(Jason.encode!(params, pretty: true))
115 e
116
117 e ->
118 # Just drop those for now
119 Logger.debug(fn -> "Unhandled activity\n" <> Jason.encode!(params, pretty: true) end)
120 {:error, e}
121 end
122 end
123
124 def ap_enabled_actor(id) do
125 user = User.get_cached_by_ap_id(id)
126
127 if User.ap_enabled?(user) do
128 {:ok, user}
129 else
130 ActivityPub.make_user_from_ap_id(id)
131 end
132 end
133 end