2019-10-23 11:52:27 +02:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-26 15:37:42 +01:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-10-23 11:52:27 +02:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Web.ActivityPub.Pipeline do
|
|
|
|
alias Pleroma.Activity
|
2020-05-21 13:31:52 +02:00
|
|
|
alias Pleroma.Config
|
2020-05-04 14:34:19 +02:00
|
|
|
alias Pleroma.Object
|
|
|
|
alias Pleroma.Repo
|
2019-10-23 11:52:27 +02:00
|
|
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
|
|
|
alias Pleroma.Web.ActivityPub.MRF
|
|
|
|
alias Pleroma.Web.ActivityPub.ObjectValidator
|
|
|
|
alias Pleroma.Web.ActivityPub.SideEffects
|
2020-11-11 15:47:57 +01:00
|
|
|
alias Pleroma.Web.ActivityPub.Visibility
|
2019-10-23 11:52:27 +02:00
|
|
|
alias Pleroma.Web.Federator
|
|
|
|
|
2020-12-16 17:51:48 +01:00
|
|
|
@side_effects Config.get([:pipeline, :side_effects], SideEffects)
|
|
|
|
@federator Config.get([:pipeline, :federator], Federator)
|
|
|
|
@object_validator Config.get([:pipeline, :object_validator], ObjectValidator)
|
|
|
|
@mrf Config.get([:pipeline, :mrf], MRF)
|
|
|
|
@activity_pub Config.get([:pipeline, :activity_pub], ActivityPub)
|
|
|
|
@config Config.get([:pipeline, :config], Config)
|
|
|
|
|
2020-05-04 14:34:19 +02:00
|
|
|
@spec common_pipeline(map(), keyword()) ::
|
|
|
|
{:ok, Activity.t() | Object.t(), keyword()} | {:error, any()}
|
2019-10-23 11:52:27 +02:00
|
|
|
def common_pipeline(object, meta) do
|
2020-05-04 14:34:19 +02:00
|
|
|
case Repo.transaction(fn -> do_common_pipeline(object, meta) end) do
|
2020-06-05 16:47:02 +02:00
|
|
|
{:ok, {:ok, activity, meta}} ->
|
2020-12-16 17:51:48 +01:00
|
|
|
@side_effects.handle_after_transaction(meta)
|
2020-06-05 16:47:02 +02:00
|
|
|
{:ok, activity, meta}
|
|
|
|
|
2020-05-04 14:34:19 +02:00
|
|
|
{:ok, value} ->
|
|
|
|
value
|
|
|
|
|
|
|
|
{:error, e} ->
|
|
|
|
{:error, e}
|
2020-09-12 12:05:36 +02:00
|
|
|
|
|
|
|
{:reject, e} ->
|
|
|
|
{:reject, e}
|
2020-05-04 14:34:19 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def do_common_pipeline(object, meta) do
|
2019-10-23 11:52:27 +02:00
|
|
|
with {_, {:ok, validated_object, meta}} <-
|
2020-12-16 17:51:48 +01:00
|
|
|
{:validate_object, @object_validator.validate(object, meta)},
|
2020-09-12 12:05:36 +02:00
|
|
|
{_, {:ok, mrfd_object, meta}} <-
|
2020-12-16 17:51:48 +01:00
|
|
|
{:mrf_object, @mrf.pipeline_filter(validated_object, meta)},
|
2020-05-04 14:34:19 +02:00
|
|
|
{_, {:ok, activity, meta}} <-
|
2020-12-16 17:51:48 +01:00
|
|
|
{:persist_object, @activity_pub.persist(mrfd_object, meta)},
|
2020-05-04 14:34:19 +02:00
|
|
|
{_, {:ok, activity, meta}} <-
|
2020-12-16 17:51:48 +01:00
|
|
|
{:execute_side_effects, @side_effects.handle(activity, meta)},
|
2019-10-23 11:52:27 +02:00
|
|
|
{_, {:ok, _}} <- {:federation, maybe_federate(activity, meta)} do
|
|
|
|
{:ok, activity, meta}
|
|
|
|
else
|
2020-09-12 12:05:36 +02:00
|
|
|
{:mrf_object, {:reject, message, _}} -> {:reject, message}
|
2019-10-23 11:52:27 +02:00
|
|
|
e -> {:error, e}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-04 14:34:19 +02:00
|
|
|
defp maybe_federate(%Object{}, _), do: {:ok, :not_federated}
|
|
|
|
|
|
|
|
defp maybe_federate(%Activity{} = activity, meta) do
|
2019-10-23 11:52:27 +02:00
|
|
|
with {:ok, local} <- Keyword.fetch(meta, :local) do
|
2020-12-16 17:51:48 +01:00
|
|
|
do_not_federate = meta[:do_not_federate] || !@config.get([:instance, :federating])
|
2020-04-30 21:23:18 +02:00
|
|
|
|
2020-11-11 15:47:57 +01:00
|
|
|
if !do_not_federate and local and not Visibility.is_local_public?(activity) do
|
2020-07-24 14:06:41 +02:00
|
|
|
activity =
|
2020-07-27 16:39:50 +02:00
|
|
|
if object = Keyword.get(meta, :object_data) do
|
2020-07-24 14:06:41 +02:00
|
|
|
%{activity | data: Map.put(activity.data, "object", object)}
|
|
|
|
else
|
|
|
|
activity
|
|
|
|
end
|
|
|
|
|
2020-12-16 17:51:48 +01:00
|
|
|
@federator.publish(activity)
|
2019-10-23 11:52:27 +02:00
|
|
|
{:ok, :federated}
|
|
|
|
else
|
|
|
|
{:ok, :not_federated}
|
|
|
|
end
|
|
|
|
else
|
2020-03-24 19:24:09 +01:00
|
|
|
_e -> {:error, :badarg}
|
2019-10-23 11:52:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|