pipeline.ex 1.91 KB
Newer Older
1
# Pleroma: A lightweight social networking server
lain's avatar
lain committed
2
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3
4
5
6
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.ActivityPub.Pipeline do
  alias Pleroma.Activity
7
8
  alias Pleroma.Object
  alias Pleroma.Repo
9
10
11
12
13
  alias Pleroma.Web.ActivityPub.ActivityPub
  alias Pleroma.Web.ActivityPub.MRF
  alias Pleroma.Web.ActivityPub.ObjectValidator
  alias Pleroma.Web.ActivityPub.SideEffects
  alias Pleroma.Web.Federator
14
  alias Pleroma.Config
15

16
17
  @spec common_pipeline(map(), keyword()) ::
          {:ok, Activity.t() | Object.t(), keyword()} | {:error, any()}
18
  def common_pipeline(object, meta) do
19
20
21
22
23
24
25
26
27
28
    case Repo.transaction(fn -> do_common_pipeline(object, meta) end) do
      {:ok, value} ->
        value

      {:error, e} ->
        {:error, e}
    end
  end

  def do_common_pipeline(object, meta) do
29
30
31
    with {_, {:ok, validated_object, meta}} <-
           {:validate_object, ObjectValidator.validate(object, meta)},
         {_, {:ok, mrfd_object}} <- {:mrf_object, MRF.filter(validated_object)},
32
         {_, {:ok, activity, meta}} <-
33
           {:persist_object, ActivityPub.persist(mrfd_object, meta)},
34
         {_, {:ok, activity, meta}} <-
35
36
37
38
           {:execute_side_effects, SideEffects.handle(activity, meta)},
         {_, {:ok, _}} <- {:federation, maybe_federate(activity, meta)} do
      {:ok, activity, meta}
    else
39
      {:mrf_object, {:reject, _}} -> {:ok, nil, meta}
40
41
42
43
      e -> {:error, e}
    end
  end

44
45
46
  defp maybe_federate(%Object{}, _), do: {:ok, :not_federated}

  defp maybe_federate(%Activity{} = activity, meta) do
47
    with {:ok, local} <- Keyword.fetch(meta, :local) do
48
      do_not_federate = meta[:do_not_federate] || !Config.get([:instance, :federating])
49
50

      if !do_not_federate && local do
51
52
53
54
55
56
        Federator.publish(activity)
        {:ok, :federated}
      else
        {:ok, :not_federated}
      end
    else
57
      _e -> {:error, :badarg}
58
59
60
    end
  end
end