fdb9e51765586173c70f3653754413aaf6bbe08c
[anni] / lib / pleroma / web / activity_pub / mrf / subchain_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.SubchainPolicy do
6   alias Pleroma.Config
7   alias Pleroma.Web.ActivityPub.MRF
8
9   require Logger
10
11   @behaviour Pleroma.Web.ActivityPub.MRF.Policy
12
13   defp lookup_subchain(actor) do
14     with matches <- Config.get([:mrf_subchain, :match_actor]),
15          {match, subchain} <- Enum.find(matches, fn {k, _v} -> String.match?(actor, k) end) do
16       {:ok, match, subchain}
17     else
18       _e -> {:error, :notfound}
19     end
20   end
21
22   @impl true
23   def filter(%{"actor" => actor} = message) do
24     with {:ok, match, subchain} <- lookup_subchain(actor) do
25       Logger.debug(
26         "[SubchainPolicy] Matched #{actor} against #{inspect(match)} with subchain #{inspect(subchain)}"
27       )
28
29       MRF.filter(subchain, message)
30     else
31       _e -> {:ok, message}
32     end
33   end
34
35   @impl true
36   def filter(message), do: {:ok, message}
37
38   @impl true
39   def describe, do: {:ok, %{}}
40
41   @impl true
42   def config_description do
43     %{
44       key: :mrf_subchain,
45       related_policy: "Pleroma.Web.ActivityPub.MRF.SubchainPolicy",
46       label: "MRF Subchain",
47       description:
48         "This policy processes messages through an alternate pipeline when a given message matches certain criteria." <>
49           " All criteria are configured as a map of regular expressions to lists of policy modules.",
50       children: [
51         %{
52           key: :match_actor,
53           type: {:map, {:list, :string}},
54           description: "Matches a series of regular expressions against the actor field",
55           suggestions: [
56             %{
57               ~r/https:\/\/example.com/s => [Pleroma.Web.ActivityPub.MRF.DropPolicy]
58             }
59           ]
60         }
61       ]
62     }
63   end
64 end