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
8 @mrf_config_descriptions [
15 description: "General MRF settings",
19 type: [:module, {:list, :module}],
21 "A list of MRF policies enabled. Module names are shortened (removed leading `Pleroma.Web.ActivityPub.MRF.` part), but on adding custom module you need to use full name.",
22 suggestions: {:list_behaviour_implementations, Pleroma.Web.ActivityPub.MRF}
26 label: "MRF transparency",
29 "Make the content of your Message Rewrite Facility settings public (via nodeinfo)"
32 key: :transparency_exclusions,
33 label: "MRF transparency exclusions",
34 type: {:list, :string},
36 "Exclude specific instance names from MRF transparency. The use of the exclusions feature will be disclosed in nodeinfo as a boolean value.",
45 @default_description %{
50 @required_description_keys [:key, :related_policy]
52 @callback filter(Map.t()) :: {:ok | :reject, Map.t()}
53 @callback describe() :: {:ok | :error, Map.t()}
54 @callback config_description() :: %{
55 optional(:children) => [map()],
57 related_policy: String.t(),
59 description: String.t()
61 @optional_callbacks config_description: 0
63 def filter(policies, %{} = message) do
65 |> Enum.reduce({:ok, message}, fn
66 policy, {:ok, message} -> policy.filter(message)
71 def filter(%{} = object), do: get_policies() |> filter(object)
73 def pipeline_filter(%{} = message, meta) do
74 object = meta[:object_data]
75 ap_id = message["object"]
78 with {:ok, message} <- filter(Map.put(message, "object", object)) do
79 meta = Keyword.put(meta, :object_data, message["object"])
80 {:ok, Map.put(message, "object", ap_id), meta}
82 {err, message} -> {err, message, meta}
85 {err, message} = filter(message)
92 Pleroma.Config.get([:mrf, :policies], []) |> get_policies()
95 defp get_policies(policy) when is_atom(policy), do: [policy]
96 defp get_policies(policies) when is_list(policies), do: policies
97 defp get_policies(_), do: []
99 @spec subdomains_regex([String.t()]) :: [Regex.t()]
100 def subdomains_regex(domains) when is_list(domains) do
101 for domain <- domains, do: ~r(^#{String.replace(domain, "*.", "(.*\\.)*")}$)i
104 @spec subdomain_match?([Regex.t()], String.t()) :: boolean()
105 def subdomain_match?(domains, host) do
106 Enum.any?(domains, fn domain -> Regex.match?(domain, host) end)
109 def describe(policies) do
110 {:ok, policy_configs} =
112 |> Enum.reduce({:ok, %{}}, fn
113 policy, {:ok, data} ->
114 {:ok, policy_data} = policy.describe()
115 {:ok, Map.merge(data, policy_data)}
123 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
125 exclusions = Pleroma.Config.get([:mrf, :transparency_exclusions])
129 mrf_policies: mrf_policies,
130 exclusions: length(exclusions) > 0
132 |> Map.merge(policy_configs)
137 def describe, do: get_policies() |> describe()
139 def config_descriptions do
140 Pleroma.Web.ActivityPub.MRF
141 |> Pleroma.Docs.Generator.list_behaviour_implementations()
142 |> config_descriptions()
145 def config_descriptions(policies) do
146 Enum.reduce(policies, @mrf_config_descriptions, fn policy, acc ->
147 if function_exported?(policy, :config_description, 0) do
150 |> Map.merge(policy.config_description)
151 |> Map.put(:group, :pleroma)
152 |> Map.put(:tab, :mrf)
153 |> Map.put(:type, :group)
155 if Enum.all?(@required_description_keys, &Map.has_key?(description, &1)) do
159 "#{policy} config description doesn't have one or all required keys #{
160 inspect(@required_description_keys)
168 "#{policy} is excluded from config descriptions, because does not implement `config_description/0` method."