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 @side_effects Config.get([:pipeline, :side_effects], SideEffects)
19 @federator Config.get([:pipeline, :federator], Federator)
20 @object_validator Config.get([:pipeline, :object_validator], ObjectValidator)
21 @mrf Config.get([:pipeline, :mrf], MRF)
22 @activity_pub Config.get([:pipeline, :activity_pub], ActivityPub)
23 @config 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)
44 def do_common_pipeline(%{__struct__: _}, _meta), do: {:error, :is_struct}
46 def do_common_pipeline(message, meta) do
47 with {_, {:ok, message, meta}} <- {:validate, @object_validator.validate(message, meta)},
48 {_, {:ok, message, meta}} <- {:mrf, @mrf.pipeline_filter(message, meta)},
49 {_, {:ok, message, meta}} <- {:persist, @activity_pub.persist(message, meta)},
50 {_, {:ok, message, meta}} <- {:side_effects, @side_effects.handle(message, meta)},
51 {_, {:ok, _}} <- {:federation, maybe_federate(message, meta)} do
54 {:mrf, {:reject, message, _}} -> {:reject, message}
59 defp maybe_federate(%Object{}, _), do: {:ok, :not_federated}
61 defp maybe_federate(%Activity{} = activity, meta) do
62 with {:ok, local} <- Keyword.fetch(meta, :local) do
63 do_not_federate = meta[:do_not_federate] || !@config.get([:instance, :federating])
65 if !do_not_federate and local and not Visibility.is_local_public?(activity) do
67 if object = Keyword.get(meta, :object_data) do
68 %{activity | data: Map.put(activity.data, "object", object)}
73 @federator.publish(activity)
79 _e -> {:error, :badarg}