Merge branch 'develop' of https://git.pleroma.social/pleroma/pleroma into develop
[akkoma] / lib / pleroma / web / activity_pub / pipeline.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.Pipeline do
6 alias Pleroma.Activity
7 alias Pleroma.Config
8 alias Pleroma.Object
9 alias Pleroma.Repo
10 alias Pleroma.Utils
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
17
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)
24
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 {:ok, activity, meta}
32
33 {:ok, value} ->
34 value
35
36 {:error, e} ->
37 {:error, e}
38
39 {:reject, e} ->
40 {:reject, e}
41 end
42 end
43
44 def do_common_pipeline(%{__struct__: _}, _meta), do: {:error, :is_struct}
45
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
52 {:ok, message, meta}
53 else
54 {:mrf, {:reject, message, _}} -> {:reject, message}
55 e -> {:error, e}
56 end
57 end
58
59 defp maybe_federate(%Object{}, _), do: {:ok, :not_federated}
60
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])
64
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 %{activity | data: Map.put(activity.data, "object", object)}
69 else
70 activity
71 end
72
73 federator().publish(activity)
74 {:ok, :federated}
75 else
76 {:ok, :not_federated}
77 end
78 else
79 _e -> {:error, :badarg}
80 end
81 end
82 end