1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF do
6 @callback filter(Map.t()) :: {:ok | :reject, Map.t()}
8 def filter(policies, %{} = object) do
10 |> Enum.reduce({:ok, object}, fn
11 policy, {:ok, object} -> policy.filter(object)
16 def filter(%{} = object), do: get_policies() |> filter(object)
19 Pleroma.Config.get([:instance, :rewrite_policy], []) |> get_policies()
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: []
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
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)
36 @callback describe() :: {:ok | :error, Map.t()}
38 def describe(policies) do
39 {:ok, policy_configs} =
41 |> Enum.reduce({:ok, %{}}, fn
42 policy, {:ok, data} ->
43 {:ok, policy_data} = policy.describe()
44 {:ok, Map.merge(data, policy_data)}
52 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
54 exclusions = Pleroma.Config.get([:instance, :mrf_transparency_exclusions])
58 mrf_policies: mrf_policies,
59 exclusions: length(exclusions) > 0
61 |> Map.merge(policy_configs)
66 def describe, do: get_policies() |> describe()