1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Workers.ReceiverWorker do
6 alias Pleroma.Signature
8 alias Pleroma.Web.Federator
10 use Pleroma.Workers.WorkerHelper, queue: "federator_incoming"
15 args: %{"op" => "incoming_ap_doc", "req_headers" => req_headers, "params" => params}
17 # Oban's serialization converts our tuple headers to lists.
18 # Revert it for the signature validation.
19 req_headers = Enum.into(req_headers, [], &List.to_tuple(&1))
21 conn_data = %{params: params, req_headers: req_headers}
23 with {:ok, %User{} = _actor} <- User.get_or_fetch_by_ap_id(conn_data.params["actor"]),
24 {:ok, _public_key} <- Signature.refetch_public_key(conn_data),
25 {:signature, true} <- {:signature, HTTPSignatures.validate_conn(conn_data)},
26 {:ok, res} <- Federator.perform(:incoming_ap_doc, params) do
29 e -> process_errors(e)
33 def perform(%Job{args: %{"op" => "incoming_ap_doc", "params" => params}}) do
34 with {:ok, res} <- Federator.perform(:incoming_ap_doc, params) do
37 e -> process_errors(e)
42 def timeout(%_{args: %{"timeout" => timeout}}), do: timeout
44 def timeout(_job), do: :timer.seconds(5)
46 defp process_errors(errors) do
48 {:error, :origin_containment_failed} -> {:cancel, :origin_containment_failed}
49 {:error, :already_present} -> {:cancel, :already_present}
50 {:error, {:validate_object, reason}} -> {:cancel, reason}
51 {:error, {:error, {:validate, reason}}} -> {:cancel, reason}
52 {:error, {:reject, reason}} -> {:cancel, reason}
53 {:signature, false} -> {:cancel, :invalid_signature}
54 {:error, {:error, reason = "Object has been deleted"}} -> {:cancel, reason}