Merge branch 'develop' into refactor/deactivated_user_field
[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.Web.ActivityPub.ActivityPub
11 alias Pleroma.Web.ActivityPub.MRF
12 alias Pleroma.Web.ActivityPub.ObjectValidator
13 alias Pleroma.Web.ActivityPub.SideEffects
14 alias Pleroma.Web.ActivityPub.Visibility
15 alias Pleroma.Web.Federator
16
17 @side_effects Config.get([:pipeline, :side_effects], SideEffects)
18 @federator Config.get([:pipeline, :federator], Federator)
19 @object_validator Config.get([:pipeline, :object_validator], ObjectValidator)
20 @mrf Config.get([:pipeline, :mrf], MRF)
21 @activity_pub Config.get([:pipeline, :activity_pub], ActivityPub)
22 @config Config.get([:pipeline, :config], Config)
23
24 @spec common_pipeline(map(), keyword()) ::
25 {:ok, Activity.t() | Object.t(), keyword()} | {:error, any()}
26 def common_pipeline(object, meta) do
27 case Repo.transaction(fn -> do_common_pipeline(object, meta) end) do
28 {:ok, {:ok, activity, meta}} ->
29 @side_effects.handle_after_transaction(meta)
30 {:ok, activity, meta}
31
32 {:ok, value} ->
33 value
34
35 {:error, e} ->
36 {:error, e}
37
38 {:reject, e} ->
39 {:reject, e}
40 end
41 end
42
43 def do_common_pipeline(object, meta) do
44 with {_, {:ok, validated_object, meta}} <-
45 {:validate_object, @object_validator.validate(object, meta)},
46 {_, {:ok, mrfd_object, meta}} <-
47 {:mrf_object, @mrf.pipeline_filter(validated_object, meta)},
48 {_, {:ok, activity, meta}} <-
49 {:persist_object, @activity_pub.persist(mrfd_object, meta)},
50 {_, {:ok, activity, meta}} <-
51 {:execute_side_effects, @side_effects.handle(activity, meta)},
52 {_, {:ok, _}} <- {:federation, maybe_federate(activity, meta)} do
53 {:ok, activity, meta}
54 else
55 {:mrf_object, {: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 -> {:error, :badarg}
81 end
82 end
83 end