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, %{} = message) do
10 |> Enum.reduce({:ok, message}, fn
11 policy, {:ok, message} -> policy.filter(message)
16 def filter(%{} = object), do: get_policies() |> filter(object)
18 def pipeline_filter(%{} = message, meta) do
19 object = meta[:object_data]
20 ap_id = message["object"]
23 with {:ok, message} <- filter(Map.put(message, "object", object)) do
24 meta = Keyword.put(meta, :object_data, message["object"])
25 {:ok, Map.put(message, "object", ap_id), meta}
27 {err, message} -> {err, message, meta}
30 {err, message} = filter(message)
37 Pleroma.Config.get([:mrf, :policies], []) |> get_policies()
40 defp get_policies(policy) when is_atom(policy), do: [policy]
41 defp get_policies(policies) when is_list(policies), do: policies
42 defp get_policies(_), do: []
44 @spec subdomains_regex([String.t()]) :: [Regex.t()]
45 def subdomains_regex(domains) when is_list(domains) do
46 for domain <- domains, do: ~r(^#{String.replace(domain, "*.", "(.*\\.)*")}$)i
49 @spec subdomain_match?([Regex.t()], String.t()) :: boolean()
50 def subdomain_match?(domains, host) do
51 Enum.any?(domains, fn domain -> Regex.match?(domain, host) end)
54 @callback describe() :: {:ok | :error, Map.t()}
56 def describe(policies) do
57 {:ok, policy_configs} =
59 |> Enum.reduce({:ok, %{}}, fn
60 policy, {:ok, data} ->
61 {:ok, policy_data} = policy.describe()
62 {:ok, Map.merge(data, policy_data)}
70 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
72 exclusions = Pleroma.Config.get([:mrf, :transparency_exclusions])
76 mrf_policies: mrf_policies,
77 exclusions: length(exclusions) > 0
79 |> Map.merge(policy_configs)
84 def describe, do: get_policies() |> describe()