1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF do
8 @behaviour Pleroma.Web.ActivityPub.MRF.PipelineFiltering
10 @mrf_config_descriptions [
17 description: "General MRF settings",
21 type: [:module, {:list, :module}],
23 "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.",
24 suggestions: {:list_behaviour_implementations, Pleroma.Web.ActivityPub.MRF}
28 label: "MRF transparency",
31 "Make the content of your Message Rewrite Facility settings public (via nodeinfo)"
34 key: :transparency_exclusions,
35 label: "MRF transparency exclusions",
36 type: {:list, :string},
38 "Exclude specific instance names from MRF transparency. The use of the exclusions feature will be disclosed in nodeinfo as a boolean value.",
47 @default_description %{
52 @required_description_keys [:key, :related_policy]
54 def filter(policies, %{} = message) do
56 |> Enum.reduce({:ok, message}, fn
57 policy, {:ok, message} -> policy.filter(message)
62 def filter(%{} = object), do: get_policies() |> filter(object)
65 def pipeline_filter(%{} = message, meta) do
66 object = meta[:object_data]
67 ap_id = message["object"]
70 with {:ok, message} <- filter(Map.put(message, "object", object)) do
71 meta = Keyword.put(meta, :object_data, message["object"])
72 {:ok, Map.put(message, "object", ap_id), meta}
74 {err, message} -> {err, message, meta}
77 {err, message} = filter(message)
84 Pleroma.Config.get([:mrf, :policies], [])
86 |> Enum.concat([Pleroma.Web.ActivityPub.MRF.HashtagPolicy])
89 defp get_policies(policy) when is_atom(policy), do: [policy]
90 defp get_policies(policies) when is_list(policies), do: policies
91 defp get_policies(_), do: []
93 @spec subdomains_regex([String.t()]) :: [Regex.t()]
94 def subdomains_regex(domains) when is_list(domains) do
95 for domain <- domains, do: ~r(^#{String.replace(domain, "*.", "(.*\\.)*")}$)i
98 @spec subdomain_match?([Regex.t()], String.t()) :: boolean()
99 def subdomain_match?(domains, host) do
100 Enum.any?(domains, fn domain -> Regex.match?(domain, host) end)
103 def describe(policies) do
104 {:ok, policy_configs} =
106 |> Enum.reduce({:ok, %{}}, fn
107 policy, {:ok, data} ->
108 {:ok, policy_data} = policy.describe()
109 {:ok, Map.merge(data, policy_data)}
117 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
119 exclusions = Pleroma.Config.get([:mrf, :transparency_exclusions])
123 mrf_policies: mrf_policies,
124 exclusions: length(exclusions) > 0
126 |> Map.merge(policy_configs)
131 def describe, do: get_policies() |> describe()
133 def config_descriptions do
134 Pleroma.Web.ActivityPub.MRF.Policy
135 |> Pleroma.Docs.Generator.list_behaviour_implementations()
136 |> config_descriptions()
139 def config_descriptions(policies) do
140 Enum.reduce(policies, @mrf_config_descriptions, fn policy, acc ->
141 if function_exported?(policy, :config_description, 0) do
144 |> Map.merge(policy.config_description)
145 |> Map.put(:group, :pleroma)
146 |> Map.put(:tab, :mrf)
147 |> Map.put(:type, :group)
149 if Enum.all?(@required_description_keys, &Map.has_key?(description, &1)) do
153 "#{policy} config description doesn't have one or all required keys #{
154 inspect(@required_description_keys)
162 "#{policy} is excluded from config descriptions, because does not implement `config_description/0` method."