2018-12-23 20:04:54 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2018-12-31 15:41:47 +00:00
|
|
|
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 20:04:54 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2017-04-26 16:33:10 +00:00
|
|
|
defmodule Pleroma.Web.Federator do
|
2019-08-13 17:20:26 +00: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 16:08:56 +00:00
|
|
|
alias Pleroma.Workers.PublisherWorker
|
|
|
|
alias Pleroma.Workers.ReceiverWorker
|
2019-02-06 19:20:02 +00:00
|
|
|
|
2017-04-26 16:33:10 +00:00
|
|
|
require Logger
|
|
|
|
|
2019-06-29 17:04:50 +00:00
|
|
|
@doc "Addresses [memory leaks on recursive replies fetching](https://git.pleroma.social/pleroma/pleroma/issues/161)"
|
|
|
|
# credo:disable-for-previous-line Credo.Check.Readability.MaxLineLength
|
2019-06-30 12:58:50 +00:00
|
|
|
def allowed_incoming_reply_depth?(depth) do
|
|
|
|
max_replies_depth = Pleroma.Config.get([:instance, :federation_incoming_replies_max_depth])
|
|
|
|
|
|
|
|
if max_replies_depth do
|
|
|
|
(depth || 1) <= max_replies_depth
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
|
|
|
end
|
2019-06-29 17:04:50 +00:00
|
|
|
|
2019-01-28 15:17:17 +00:00
|
|
|
# Client API
|
|
|
|
|
|
|
|
def incoming_ap_doc(params) do
|
2019-08-31 18:58:42 +00:00
|
|
|
ReceiverWorker.enqueue("incoming_ap_doc", %{"params" => params})
|
2019-01-28 15:17:17 +00:00
|
|
|
end
|
|
|
|
|
2019-08-09 17:08:01 +00:00
|
|
|
def publish(%{id: "pleroma:fakeid"} = activity) do
|
2019-08-13 17:20:26 +00:00
|
|
|
perform(:publish, activity)
|
2019-01-28 15:17:17 +00:00
|
|
|
end
|
|
|
|
|
2019-08-09 17:08:01 +00:00
|
|
|
def publish(activity) do
|
2019-08-31 18:58:42 +00:00
|
|
|
PublisherWorker.enqueue("publish", %{"activity_id" => activity.id})
|
2017-08-02 10:34:48 +00:00
|
|
|
end
|
|
|
|
|
2019-08-13 17:20:26 +00: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)
|
|
|
|
|
|
|
|
with %User{} = actor <- User.get_cached_by_ap_id(activity.data["actor"]),
|
|
|
|
{:ok, actor} <- User.ensure_keys_present(actor) do
|
|
|
|
Publisher.publish(actor, activity)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def perform(:incoming_ap_doc, params) do
|
|
|
|
Logger.info("Handling incoming AP activity")
|
|
|
|
|
|
|
|
params = Utils.normalize_params(params)
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
with {:ok, _user} <- ap_enabled_actor(params["actor"]),
|
|
|
|
nil <- Activity.normalize(params["id"]),
|
|
|
|
:ok <- Containment.contain_origin_from_id(params["actor"], params),
|
|
|
|
{:ok, activity} <- Transmogrifier.handle_incoming(params) do
|
|
|
|
{:ok, activity}
|
|
|
|
else
|
|
|
|
%Activity{} ->
|
|
|
|
Logger.info("Already had #{params["id"]}")
|
|
|
|
:error
|
|
|
|
|
|
|
|
_e ->
|
|
|
|
# Just drop those for now
|
|
|
|
Logger.info("Unhandled activity")
|
|
|
|
Logger.info(Jason.encode!(params, pretty: true))
|
|
|
|
:error
|
|
|
|
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 07:51:03 +00:00
|
|
|
else
|
2019-08-13 17:20:26 +00:00
|
|
|
ActivityPub.make_user_from_ap_id(id)
|
2018-02-21 07:51:03 +00:00
|
|
|
end
|
|
|
|
end
|
2017-04-26 16:33:10 +00:00
|
|
|
end
|