2019-08-09 17:08:01 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2023-01-02 20:38:50 +00:00
|
|
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
2019-08-09 17:08:01 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-08-31 16:08:56 +00:00
|
|
|
defmodule Pleroma.Workers.ReceiverWorker do
|
2023-12-08 03:27:19 +00:00
|
|
|
alias Pleroma.Signature
|
|
|
|
alias Pleroma.User
|
2019-08-13 17:20:26 +00:00
|
|
|
alias Pleroma.Web.Federator
|
2019-08-09 17:08:01 +00:00
|
|
|
|
2019-08-31 18:58:42 +00:00
|
|
|
use Pleroma.Workers.WorkerHelper, queue: "federator_incoming"
|
|
|
|
|
2019-08-09 17:08:01 +00:00
|
|
|
@impl Oban.Worker
|
2023-12-08 03:27:19 +00:00
|
|
|
|
|
|
|
def perform(%Job{
|
|
|
|
args: %{"op" => "incoming_ap_doc", "req_headers" => req_headers, "params" => params}
|
|
|
|
}) do
|
2023-12-09 02:51:36 +00:00
|
|
|
# Oban's serialization converts our tuple headers to lists.
|
|
|
|
# Revert it for the signature validation.
|
|
|
|
req_headers = Enum.into(req_headers, [], &List.to_tuple(&1))
|
|
|
|
|
2023-12-08 03:27:19 +00:00
|
|
|
conn_data = %{params: params, req_headers: req_headers}
|
|
|
|
|
|
|
|
with {:ok, %User{} = _actor} <- User.get_or_fetch_by_ap_id(conn_data.params["actor"]),
|
|
|
|
{:ok, _public_key} <- Signature.refetch_public_key(conn_data),
|
|
|
|
{:signature, true} <- {:signature, HTTPSignatures.validate_conn(conn_data)},
|
|
|
|
{:ok, res} <- Federator.perform(:incoming_ap_doc, params) do
|
|
|
|
{:ok, res}
|
|
|
|
else
|
|
|
|
e -> process_errors(e)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-23 12:09:01 +00:00
|
|
|
def perform(%Job{args: %{"op" => "incoming_ap_doc", "params" => params}}) do
|
2022-08-06 04:31:36 +00:00
|
|
|
with {:ok, res} <- Federator.perform(:incoming_ap_doc, params) do
|
|
|
|
{:ok, res}
|
|
|
|
else
|
2023-12-08 03:27:19 +00:00
|
|
|
e -> process_errors(e)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl Oban.Worker
|
|
|
|
def timeout(%_{args: %{"timeout" => timeout}}), do: timeout
|
|
|
|
|
|
|
|
def timeout(_job), do: :timer.seconds(5)
|
|
|
|
|
|
|
|
defp process_errors(errors) do
|
|
|
|
case errors do
|
2022-08-08 12:41:33 +00:00
|
|
|
{:error, :origin_containment_failed} -> {:cancel, :origin_containment_failed}
|
2023-03-01 05:40:44 +00:00
|
|
|
{:error, :already_present} -> {:cancel, :already_present}
|
|
|
|
{:error, {:validate_object, reason}} -> {:cancel, reason}
|
|
|
|
{:error, {:error, {:validate, reason}}} -> {:cancel, reason}
|
2022-08-06 04:31:36 +00:00
|
|
|
{:error, {:reject, reason}} -> {:cancel, reason}
|
2023-12-10 17:55:41 +00:00
|
|
|
{:signature, false} -> {:cancel, :invalid_signature}
|
2023-12-09 22:43:54 +00:00
|
|
|
{:error, {:error, reason = "Object has been deleted"}} -> {:cancel, reason}
|
2022-08-06 04:31:36 +00:00
|
|
|
e -> e
|
|
|
|
end
|
2019-08-09 17:08:01 +00:00
|
|
|
end
|
|
|
|
end
|