2018-12-23 13:04:54 -07:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-12 23:49:20 -07:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 13:04:54 -07:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-04-26 10:33:10 -06:00
|
|
|
defmodule Pleroma.Web.Federator do
|
2019-08-13 11:20:26 -06:00
|
|
|
alias Pleroma.Activity
|
|
|
|
alias Pleroma.Object.Containment
|
|
|
|
alias Pleroma.User
|
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
|
|
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
|
|
|
alias Pleroma.Web.ActivityPub.Utils
|
|
|
|
alias Pleroma.Web.Federator.Publisher
|
2019-08-31 10:08:56 -06:00
|
|
|
alias Pleroma.Workers.PublisherWorker
|
|
|
|
alias Pleroma.Workers.ReceiverWorker
|
2019-02-06 12:20:02 -07:00
|
|
|
|
2017-04-26 10:33:10 -06:00
|
|
|
require Logger
|
|
|
|
|
2020-12-16 09:51:48 -07:00
|
|
|
@behaviour Pleroma.Web.Federator.Publishing
|
|
|
|
|
2020-02-15 10:41:38 -07:00
|
|
|
@doc """
|
|
|
|
Returns `true` if the distance to target object does not exceed max configured value.
|
|
|
|
Serves to prevent fetching of very long threads, especially useful on smaller instances.
|
|
|
|
Addresses [memory leaks on recursive replies fetching](https://git.pleroma.social/pleroma/pleroma/issues/161).
|
|
|
|
Applies to fetching of both ancestor (reply-to) and child (reply) objects.
|
|
|
|
"""
|
2019-06-29 11:04:50 -06:00
|
|
|
# credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
|
2020-02-15 10:41:38 -07:00
|
|
|
def allowed_thread_distance?(distance) do
|
|
|
|
max_distance = Pleroma.Config.get([:instance, :federation_incoming_replies_max_depth])
|
2019-06-30 06:58:50 -06:00
|
|
|
|
2020-02-15 10:41:38 -07:00
|
|
|
if max_distance && max_distance >= 0 do
|
|
|
|
# Default depth is 0 (an object has zero distance from itself in its thread)
|
|
|
|
(distance || 0) <= max_distance
|
2019-06-30 06:58:50 -06:00
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2019-06-29 11:04:50 -06:00
|
|
|
|
2019-01-28 08:17:17 -07:00
|
|
|
# Client API
|
|
|
|
|
|
|
|
def incoming_ap_doc(params) do
|
2019-08-31 12:58:42 -06:00
|
|
|
ReceiverWorker.enqueue("incoming_ap_doc", %{"params" => params})
|
2019-01-28 08:17:17 -07:00
|
|
|
end
|
|
|
|
|
2020-12-16 09:51:48 -07:00
|
|
|
@impl true
|
2019-08-09 11:08:01 -06:00
|
|
|
def publish(%{id: "pleroma:fakeid"} = activity) do
|
2019-08-13 11:20:26 -06:00
|
|
|
perform(:publish, activity)
|
2019-01-28 08:17:17 -07:00
|
|
|
end
|
|
|
|
|
2020-12-16 09:51:48 -07:00
|
|
|
@impl true
|
2022-06-25 11:41:26 -06:00
|
|
|
def publish(%{data: %{"object" => object}} = activity) when is_binary(object) do
|
|
|
|
PublisherWorker.enqueue("publish", %{"activity_id" => activity.id, "object_data" => nil})
|
|
|
|
end
|
|
|
|
|
|
|
|
@impl true
|
|
|
|
def publish(%{data: %{"object" => object}} = activity) when is_map(object) or is_list(object) do
|
2022-10-11 07:30:08 -06:00
|
|
|
PublisherWorker.enqueue(
|
|
|
|
"publish",
|
|
|
|
%{
|
|
|
|
"activity_id" => activity.id,
|
|
|
|
"object_data" => Jason.encode!(object)
|
|
|
|
},
|
|
|
|
priority: publish_priority(activity)
|
|
|
|
)
|
2017-08-02 04:34:48 -06:00
|
|
|
end
|
|
|
|
|
2022-10-11 07:30:08 -06:00
|
|
|
defp publish_priority(%{type: "Delete"}), do: 3
|
|
|
|
defp publish_priority(_), do: 0
|
|
|
|
|
2019-08-13 11:20:26 -06:00
|
|
|
# Job Worker Callbacks
|
|
|
|
|
|
|
|
@spec perform(atom(), module(), any()) :: {:ok, any()} | {:error, any()}
|
|
|
|
def perform(:publish_one, module, params) do
|
|
|
|
apply(module, :publish_one, [params])
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform(:publish, activity) do
|
|
|
|
Logger.debug(fn -> "Running publish for #{activity.data["id"]}" end)
|
|
|
|
|
2022-08-26 10:30:43 -06:00
|
|
|
%User{} = actor = User.get_cached_by_ap_id(activity.data["actor"])
|
|
|
|
Publisher.publish(actor, activity)
|
2019-08-13 11:20:26 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
def perform(:incoming_ap_doc, params) do
|
2019-12-11 03:46:07 -07:00
|
|
|
Logger.debug("Handling incoming AP activity")
|
2019-08-13 11:20:26 -06:00
|
|
|
|
2020-09-10 15:13:51 -06:00
|
|
|
actor =
|
|
|
|
params
|
|
|
|
|> Map.get("actor")
|
|
|
|
|> Utils.get_ap_id()
|
2019-08-13 11:20:26 -06:00
|
|
|
|
|
|
|
# NOTE: we use the actor ID to do the containment, this is fine because an
|
|
|
|
# actor shouldn't be acting on objects outside their own AP server.
|
2020-09-10 15:13:51 -06:00
|
|
|
with {_, {:ok, _user}} <- {:actor, ap_enabled_actor(actor)},
|
2019-08-13 11:20:26 -06:00
|
|
|
nil <- Activity.normalize(params["id"]),
|
2020-04-22 05:28:52 -06:00
|
|
|
{_, :ok} <-
|
2020-09-10 15:13:51 -06:00
|
|
|
{:correct_origin?, Containment.contain_origin_from_id(actor, params)},
|
2019-08-13 11:20:26 -06:00
|
|
|
{:ok, activity} <- Transmogrifier.handle_incoming(params) do
|
|
|
|
{:ok, activity}
|
|
|
|
else
|
2020-04-22 05:28:52 -06:00
|
|
|
{:correct_origin?, _} ->
|
|
|
|
Logger.debug("Origin containment failure for #{params["id"]}")
|
|
|
|
{:error, :origin_containment_failed}
|
|
|
|
|
2019-08-13 11:20:26 -06:00
|
|
|
%Activity{} ->
|
2019-12-11 03:46:07 -07:00
|
|
|
Logger.debug("Already had #{params["id"]}")
|
2020-04-22 05:28:52 -06:00
|
|
|
{:error, :already_present}
|
2019-08-13 11:20:26 -06:00
|
|
|
|
2020-09-10 15:13:51 -06:00
|
|
|
{:actor, e} ->
|
|
|
|
Logger.debug("Unhandled actor #{actor}, #{inspect(e)}")
|
|
|
|
{:error, e}
|
|
|
|
|
2020-09-10 03:11:10 -06:00
|
|
|
{:error, {:validate_object, _}} = e ->
|
|
|
|
Logger.error("Incoming AP doc validation error: #{inspect(e)}")
|
|
|
|
Logger.debug(Jason.encode!(params, pretty: true))
|
|
|
|
e
|
|
|
|
|
2020-04-22 05:28:52 -06:00
|
|
|
e ->
|
2019-08-13 11:20:26 -06:00
|
|
|
# Just drop those for now
|
2020-09-11 04:46:16 -06:00
|
|
|
Logger.debug(fn -> "Unhandled activity\n" <> Jason.encode!(params, pretty: true) end)
|
2020-04-22 05:28:52 -06:00
|
|
|
{:error, e}
|
2019-08-13 11:20:26 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def ap_enabled_actor(id) do
|
|
|
|
user = User.get_cached_by_ap_id(id)
|
|
|
|
|
|
|
|
if User.ap_enabled?(user) do
|
|
|
|
{:ok, user}
|
2018-02-21 00:51:03 -07:00
|
|
|
else
|
2019-08-13 11:20:26 -06:00
|
|
|
ActivityPub.make_user_from_ap_id(id)
|
2018-02-21 00:51:03 -07:00
|
|
|
end
|
|
|
|
end
|
2017-04-26 10:33:10 -06:00
|
|
|
end
|