run mix format
[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 side_effects().handle_after_transaction(activity)
32 {:ok, activity, meta}
33
34 {:ok, value} ->
35 value
36
37 {:error, e} ->
38 {:error, e}
39
40 {:reject, e} ->
41 {:reject, e}
42 end
43 end
44
45 def do_common_pipeline(%{__struct__: _}, _meta), do: {:error, :is_struct}
46
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
53 {:ok, message, meta}
54 else
55 {:mrf, {:reject, message, _}} -> {:reject, message}
56 e -> {:error, e}
57 end
58 end
59
60 defp maybe_federate(%Object{}, _), do: {:ok, :not_federated}
61
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])
65
66 if !do_not_federate and local and not Visibility.is_local_public?(activity) do
67 activity =
68 if object = Keyword.get(meta, :object_data) do
69 %{activity | data: Map.put(activity.data, "object", object)}
70 else
71 activity
72 end
73
74 federator().publish(activity)
75 {:ok, :federated}
76 else
77 {:ok, :not_federated}
78 end
79 else
80 _e ->
81 {:error, :badarg}
82 end
83 end
84 end