1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2023 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.Policy}
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, :tuple},
37 key_placeholder: "instance",
38 value_placeholder: "reason",
40 "Exclude specific instance names from MRF transparency. The use of the exclusions feature will be disclosed in nodeinfo as a boolean value. You can also provide a reason for excluding these instance names. The instances and reasons won't be publicly disclosed.",
49 @default_description %{
54 @required_description_keys [:key, :related_policy]
56 def filter_one(policy, message) do
57 Code.ensure_loaded(policy)
59 should_plug_history? =
60 if function_exported?(policy, :history_awareness, 0) do
61 policy.history_awareness()
67 if not should_plug_history? do
68 policy.filter(message)
70 main_result = policy.filter(message)
72 with {_, {:ok, main_message}} <- {:main, main_result},
75 "formerRepresentations" => %{
76 "orderedItems" => [_ | _]
78 }} = {_, object} <- {:object, message["object"]},
79 {_, {:ok, new_history}} <-
81 Pleroma.Object.Updater.for_each_history_item(
82 object["formerRepresentations"],
85 with {:ok, filtered} <- policy.filter(Map.put(message, "object", item)) do
86 {:ok, filtered["object"]}
92 {:ok, put_in(main_message, ["object", "formerRepresentations"], new_history)}
94 {:main, _} -> main_result
95 {:object, _} -> main_result
101 def filter(policies, %{} = message) do
103 |> Enum.reduce({:ok, message}, fn
104 policy, {:ok, message} -> filter_one(policy, message)
109 def filter(%{} = object), do: get_policies() |> filter(object)
112 def pipeline_filter(%{} = message, meta) do
113 object = meta[:object_data]
114 ap_id = message["object"]
116 if object && ap_id do
117 with {:ok, message} <- filter(Map.put(message, "object", object)) do
118 meta = Keyword.put(meta, :object_data, message["object"])
119 {:ok, Map.put(message, "object", ap_id), meta}
121 {err, message} -> {err, message, meta}
124 {err, message} = filter(message)
131 Pleroma.Config.get([:mrf, :policies], [])
133 |> Enum.concat([Pleroma.Web.ActivityPub.MRF.HashtagPolicy])
136 defp get_policies(policy) when is_atom(policy), do: [policy]
137 defp get_policies(policies) when is_list(policies), do: policies
138 defp get_policies(_), do: []
140 @spec subdomains_regex([String.t()]) :: [Regex.t()]
141 def subdomains_regex(domains) when is_list(domains) do
142 for domain <- domains do
144 target = String.replace(domain, "*.", "(.*\\.)*")
148 Logger.error("MRF: Invalid subdomain Regex: #{domain}")
149 reraise e, __STACKTRACE__
154 @spec subdomain_match?([Regex.t()], String.t()) :: boolean()
155 def subdomain_match?(domains, host) do
156 Enum.any?(domains, fn domain -> Regex.match?(domain, host) end)
159 @spec instance_list_from_tuples([{String.t(), String.t()}]) :: [String.t()]
160 def instance_list_from_tuples(list) do
161 Enum.map(list, fn {instance, _} -> instance end)
164 def describe(policies) do
165 {:ok, policy_configs} =
167 |> Enum.reduce({:ok, %{}}, fn
168 policy, {:ok, data} ->
169 {:ok, policy_data} = policy.describe()
170 {:ok, Map.merge(data, policy_data)}
178 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
180 exclusions = Pleroma.Config.get([:mrf, :transparency_exclusions])
184 mrf_policies: mrf_policies,
185 exclusions: length(exclusions) > 0
187 |> Map.merge(policy_configs)
192 def describe, do: get_policies() |> describe()
194 def config_descriptions do
195 Pleroma.Web.ActivityPub.MRF.Policy
196 |> Pleroma.Docs.Generator.list_behaviour_implementations()
197 |> config_descriptions()
200 def config_descriptions(policies) do
201 Enum.reduce(policies, @mrf_config_descriptions, fn policy, acc ->
202 Code.ensure_loaded(policy)
204 if function_exported?(policy, :config_description, 0) do
207 |> Map.merge(policy.config_description)
208 |> Map.put(:group, :pleroma)
209 |> Map.put(:tab, :mrf)
210 |> Map.put(:type, :group)
212 if Enum.all?(@required_description_keys, &Map.has_key?(description, &1)) do
216 "#{policy} config description doesn't have one or all required keys #{inspect(@required_description_keys)}"
223 "#{policy} is excluded from config descriptions, because does not implement `config_description/0` method."