1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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 should_plug_history? =
58 if function_exported?(policy, :history_awareness, 0) do
59 policy.history_awareness()
65 if not should_plug_history? do
66 policy.filter(message)
68 main_result = policy.filter(message)
70 with {_, {:ok, main_message}} <- {:main, main_result},
73 "formerRepresentations" => %{
74 "orderedItems" => [_ | _]
76 }} = {_, object} <- {:object, message["object"]},
77 {_, {:ok, new_history}} <-
79 Pleroma.Object.Updater.for_each_history_item(
80 object["formerRepresentations"],
83 with {:ok, filtered} <- policy.filter(Map.put(message, "object", item)) do
84 {:ok, filtered["object"]}
90 {:ok, put_in(main_message, ["object", "formerRepresentations"], new_history)}
92 {:main, _} -> main_result
93 {:object, _} -> main_result
99 def filter(policies, %{} = message) do
101 |> Enum.reduce({:ok, message}, fn
102 policy, {:ok, message} -> filter_one(policy, message)
107 def filter(%{} = object), do: get_policies() |> filter(object)
110 def pipeline_filter(%{} = message, meta) do
111 object = meta[:object_data]
112 ap_id = message["object"]
114 if object && ap_id do
115 with {:ok, message} <- filter(Map.put(message, "object", object)) do
116 meta = Keyword.put(meta, :object_data, message["object"])
117 {:ok, Map.put(message, "object", ap_id), meta}
119 {err, message} -> {err, message, meta}
122 {err, message} = filter(message)
129 Pleroma.Config.get([:mrf, :policies], [])
131 |> Enum.concat([Pleroma.Web.ActivityPub.MRF.HashtagPolicy])
134 defp get_policies(policy) when is_atom(policy), do: [policy]
135 defp get_policies(policies) when is_list(policies), do: policies
136 defp get_policies(_), do: []
138 @spec subdomains_regex([String.t()]) :: [Regex.t()]
139 def subdomains_regex(domains) when is_list(domains) do
140 for domain <- domains, do: ~r(^#{String.replace(domain, "*.", "(.*\\.)*")}$)i
143 @spec subdomain_match?([Regex.t()], String.t()) :: boolean()
144 def subdomain_match?(domains, host) do
145 Enum.any?(domains, fn domain -> Regex.match?(domain, host) end)
148 @spec instance_list_from_tuples([{String.t(), String.t()}]) :: [String.t()]
149 def instance_list_from_tuples(list) do
150 Enum.map(list, fn {instance, _} -> instance end)
153 def describe(policies) do
154 {:ok, policy_configs} =
156 |> Enum.reduce({:ok, %{}}, fn
157 policy, {:ok, data} ->
158 {:ok, policy_data} = policy.describe()
159 {:ok, Map.merge(data, policy_data)}
167 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
169 exclusions = Pleroma.Config.get([:mrf, :transparency_exclusions])
173 mrf_policies: mrf_policies,
174 exclusions: length(exclusions) > 0
176 |> Map.merge(policy_configs)
181 def describe, do: get_policies() |> describe()
183 def config_descriptions do
184 Pleroma.Web.ActivityPub.MRF.Policy
185 |> Pleroma.Docs.Generator.list_behaviour_implementations()
186 |> config_descriptions()
189 def config_descriptions(policies) do
190 Enum.reduce(policies, @mrf_config_descriptions, fn policy, acc ->
191 if function_exported?(policy, :config_description, 0) do
194 |> Map.merge(policy.config_description)
195 |> Map.put(:group, :pleroma)
196 |> Map.put(:tab, :mrf)
197 |> Map.put(:type, :group)
199 if Enum.all?(@required_description_keys, &Map.has_key?(description, &1)) do
203 "#{policy} config description doesn't have one or all required keys #{inspect(@required_description_keys)}"
210 "#{policy} is excluded from config descriptions, because does not implement `config_description/0` method."