f84d7cc7169a6ba1b27a1becb31a1a80ba7b04ae
[akkoma] / lib / pleroma / web / activity_pub / mrf / subchain_policy.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.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 #{
27 inspect(subchain)
28 }"
29 )
30
31 MRF.filter(subchain, message)
32 else
33 _e -> {:ok, message}
34 end
35 end
36
37 @impl true
38 def filter(message), do: {:ok, message}
39
40 @impl true
41 def describe, do: {:ok, %{}}
42
43 @impl true
44 def config_description do
45 %{
46 key: :mrf_subchain,
47 related_policy: "Pleroma.Web.ActivityPub.MRF.SubchainPolicy",
48 label: "MRF Subchain",
49 description:
50 "This policy processes messages through an alternate pipeline when a given message matches certain criteria." <>
51 " All criteria are configured as a map of regular expressions to lists of policy modules.",
52 children: [
53 %{
54 key: :match_actor,
55 type: {:map, {:list, :string}},
56 description: "Matches a series of regular expressions against the actor field",
57 suggestions: [
58 %{
59 ~r/https:\/\/example.com/s => [Pleroma.Web.ActivityPub.MRF.DropPolicy]
60 }
61 ]
62 }
63 ]
64 }
65 end
66 end