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