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-2020 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.Federator
15
16 @spec common_pipeline(map(), keyword()) ::
17 {:ok, Activity.t() | Object.t(), keyword()} | {:error, any()}
18 def common_pipeline(object, meta) do
19 case Repo.transaction(fn -> do_common_pipeline(object, meta) end) do
20 {:ok, {:ok, activity, meta}} ->
21 SideEffects.handle_after_transaction(meta)
22 {:ok, activity, meta}
23
24 {:ok, value} ->
25 value
26
27 {:error, e} ->
28 {:error, e}
29 end
30 end
31
32 def do_common_pipeline(object, meta) do
33 with {_, {:ok, validated_object, meta}} <-
34 {:validate_object, ObjectValidator.validate(object, meta)},
35 {_, {:ok, mrfd_object}} <- {:mrf_object, MRF.filter(validated_object)},
36 {_, {:ok, activity, meta}} <-
37 {:persist_object, ActivityPub.persist(mrfd_object, meta)},
38 {_, {:ok, activity, meta}} <-
39 {:execute_side_effects, SideEffects.handle(activity, meta)},
40 {_, {:ok, _}} <- {:federation, maybe_federate(activity, meta)} do
41 {:ok, activity, meta}
42 else
43 {:mrf_object, {:reject, _}} -> {:ok, nil, meta}
44 e -> {:error, e}
45 end
46 end
47
48 defp maybe_federate(%Object{}, _), do: {:ok, :not_federated}
49
50 defp maybe_federate(%Activity{} = activity, meta) do
51 with {:ok, local} <- Keyword.fetch(meta, :local) do
52 do_not_federate = meta[:do_not_federate] || !Config.get([:instance, :federating])
53
54 if !do_not_federate && local do
55 activity =
56 if object = Keyword.get(meta, :object_data) do
57 %{activity | data: Map.put(activity.data, "object", object)}
58 else
59 activity
60 end
61
62 Federator.publish(activity)
63 {:ok, :federated}
64 else
65 {:ok, :not_federated}
66 end
67 else
68 _e -> {:error, :badarg}
69 end
70 end
71 end