1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.Pipeline do
11 alias Pleroma.Web.ActivityPub.ActivityPub
12 alias Pleroma.Web.ActivityPub.MRF
13 alias Pleroma.Web.ActivityPub.ObjectValidator
14 alias Pleroma.Web.ActivityPub.SideEffects
15 alias Pleroma.Web.ActivityPub.Visibility
16 alias Pleroma.Web.Federator
18 defp side_effects, do: Config.get([:pipeline, :side_effects], SideEffects)
19 defp federator, do: Config.get([:pipeline, :federator], Federator)
20 defp object_validator, do: Config.get([:pipeline, :object_validator], ObjectValidator)
21 defp mrf, do: Config.get([:pipeline, :mrf], MRF)
22 defp activity_pub, do: Config.get([:pipeline, :activity_pub], ActivityPub)
23 defp config, do: Config.get([:pipeline, :config], Config)
25 @spec common_pipeline(map(), keyword()) ::
26 {:ok, Activity.t() | Object.t(), keyword()} | {:error, any()}
27 def common_pipeline(object, meta) do
28 case Repo.transaction(fn -> do_common_pipeline(object, meta) end, Utils.query_timeout()) do
29 {:ok, {:ok, activity, meta}} ->
30 side_effects().handle_after_transaction(meta)
31 side_effects().handle_after_transaction(activity)
45 def do_common_pipeline(%{__struct__: _}, _meta), do: {:error, :is_struct}
47 def do_common_pipeline(message, meta) do
48 with {_, {:ok, message, meta}} <- {:validate, object_validator().validate(message, meta)},
49 {_, {:ok, message, meta}} <- {:mrf, mrf().pipeline_filter(message, meta)},
50 {_, {:ok, message, meta}} <- {:persist, activity_pub().persist(message, meta)},
51 {_, {:ok, message, meta}} <- {:side_effects, side_effects().handle(message, meta)},
52 {_, {:ok, _}} <- {:federation, maybe_federate(message, meta)} do
55 {:mrf, {:reject, message, _}} -> {:reject, message}
60 defp maybe_federate(%Object{}, _), do: {:ok, :not_federated}
62 defp maybe_federate(%Activity{} = activity, meta) do
63 with {:ok, local} <- Keyword.fetch(meta, :local) do
64 do_not_federate = meta[:do_not_federate] || !config().get([:instance, :federating])
66 if !do_not_federate and local and not Visibility.is_local_public?(activity) do
68 if object = Keyword.get(meta, :object_data) do
69 %{activity | data: Map.put(activity.data, "object", object)}
74 federator().publish(activity)