Apply suggestion to config/description.exs
[akkoma] / lib / pleroma / web / activity_pub / mrf.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 do
6 @callback filter(Map.t()) :: {:ok | :reject, Map.t()}
7
8 def filter(policies, %{} = object) do
9 policies
10 |> Enum.reduce({:ok, object}, fn
11 policy, {:ok, object} -> policy.filter(object)
12 _, error -> error
13 end)
14 end
15
16 def filter(%{} = object), do: get_policies() |> filter(object)
17
18 def get_policies do
19 Pleroma.Config.get([:instance, :rewrite_policy], []) |> get_policies()
20 end
21
22 defp get_policies(policy) when is_atom(policy), do: [policy]
23 defp get_policies(policies) when is_list(policies), do: policies
24 defp get_policies(_), do: []
25
26 @spec subdomains_regex([String.t()]) :: [Regex.t()]
27 def subdomains_regex(domains) when is_list(domains) do
28 for domain <- domains, do: ~r(^#{String.replace(domain, "*.", "(.*\\.)*")}$)i
29 end
30
31 @spec subdomain_match?([Regex.t()], String.t()) :: boolean()
32 def subdomain_match?(domains, host) do
33 Enum.any?(domains, fn domain -> Regex.match?(domain, host) end)
34 end
35
36 @callback describe() :: {:ok | :error, Map.t()}
37
38 def describe(policies) do
39 {:ok, policy_configs} =
40 policies
41 |> Enum.reduce({:ok, %{}}, fn
42 policy, {:ok, data} ->
43 {:ok, policy_data} = policy.describe()
44 {:ok, Map.merge(data, policy_data)}
45
46 _, error ->
47 error
48 end)
49
50 mrf_policies =
51 get_policies()
52 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
53
54 exclusions = Pleroma.Config.get([:instance, :mrf_transparency_exclusions])
55
56 base =
57 %{
58 mrf_policies: mrf_policies,
59 exclusions: length(exclusions) > 0
60 }
61 |> Map.merge(policy_configs)
62
63 {:ok, base}
64 end
65
66 def describe, do: get_policies() |> describe()
67 end