pipeline.ex 2.78 KB
Newer Older
1
# Pleroma: A lightweight social networking server
Haelwenn's avatar
Haelwenn committed
2
# Copyright © 2017-2021 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
lain's avatar
lain committed
7
  alias Pleroma.Config
8
9
  alias Pleroma.Object
  alias Pleroma.Repo
feld's avatar
feld committed
10
  alias Pleroma.Utils
11
12
13
14
  alias Pleroma.Web.ActivityPub.ActivityPub
  alias Pleroma.Web.ActivityPub.MRF
  alias Pleroma.Web.ActivityPub.ObjectValidator
  alias Pleroma.Web.ActivityPub.SideEffects
minibikini's avatar
minibikini committed
15
  alias Pleroma.Web.ActivityPub.Visibility
16
17
  alias Pleroma.Web.Federator

lain's avatar
lain committed
18
19
20
21
22
23
24
  @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)

25
26
  @spec common_pipeline(map(), keyword()) ::
          {:ok, Activity.t() | Object.t(), keyword()} | {:error, any()}
27
  def common_pipeline(object, meta) do
feld's avatar
feld committed
28
    case Repo.transaction(fn -> do_common_pipeline(object, meta) end, Utils.query_timeout()) do
29
      {:ok, {:ok, activity, meta}} ->
lain's avatar
lain committed
30
        @side_effects.handle_after_transaction(meta)
31
32
        {:ok, activity, meta}

33
34
35
36
37
      {:ok, value} ->
        value

      {:error, e} ->
        {:error, e}
38
39
40

      {:reject, e} ->
        {:reject, e}
41
42
43
    end
  end

44
45
  def do_common_pipeline(%{__struct__: _}, _meta), do: {:error, :is_struct}

Haelwenn's avatar
Haelwenn committed
46
47
48
49
50
51
52
  def do_common_pipeline(message, meta) do
    with {_, {:ok, message, meta}} <- {:validate, @object_validator.validate(message, meta)},
         {_, {:ok, message, meta}} <- {:mrf, @mrf.pipeline_filter(message, meta)},
         {_, {:ok, message, meta}} <- {:persist, @activity_pub.persist(message, meta)},
         {_, {:ok, message, meta}} <- {:side_effects, @side_effects.handle(message, meta)},
         {_, {:ok, _}} <- {:federation, maybe_federate(message, meta)} do
      {:ok, message, meta}
53
    else
Haelwenn's avatar
Haelwenn committed
54
      {:mrf, {:reject, message, _}} -> {:reject, message}
55
56
57
58
      e -> {:error, e}
    end
  end

59
60
61
  defp maybe_federate(%Object{}, _), do: {:ok, :not_federated}

  defp maybe_federate(%Activity{} = activity, meta) do
62
    with {:ok, local} <- Keyword.fetch(meta, :local) do
lain's avatar
lain committed
63
      do_not_federate = meta[:do_not_federate] || !@config.get([:instance, :federating])
64

minibikini's avatar
minibikini committed
65
      if !do_not_federate and local and not Visibility.is_local_public?(activity) do
66
        activity =
67
          if object = Keyword.get(meta, :object_data) do
68
69
70
71
72
            %{activity | data: Map.put(activity.data, "object", object)}
          else
            activity
          end

lain's avatar
lain committed
73
        @federator.publish(activity)
74
75
76
77
78
        {:ok, :federated}
      else
        {:ok, :not_federated}
      end
    else
79
      _e -> {:error, :badarg}
80
81
82
    end
  end
end