Merge branch 'develop' into issue/2099
[akkoma] / lib / pleroma / web / activity_pub / mrf / subchain_policy.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.MRF.SubchainPolicy do
6 alias Pleroma.Config
7 alias Pleroma.Web.ActivityPub.MRF
8
9 require Logger
10
11 @behaviour Pleroma.Web.ActivityPub.MRF
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 end