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 @callback filter(Map.t()) :: {:ok | :reject, Map.t()}
55 @callback describe() :: {:ok | :error, Map.t()}
56 @callback config_description() :: %{
57 optional(:children) => [map()],
59 related_policy: String.t(),
61 description: String.t()
63 @optional_callbacks config_description: 0
65 def filter(policies, %{} = message) do
67 |> Enum.reduce({:ok, message}, fn
68 policy, {:ok, message} -> policy.filter(message)
73 def filter(%{} = object), do: get_policies() |> filter(object)
76 def pipeline_filter(%{} = message, meta) do
77 object = meta[:object_data]
78 ap_id = message["object"]
81 with {:ok, message} <- filter(Map.put(message, "object", object)) do
82 meta = Keyword.put(meta, :object_data, message["object"])
83 {:ok, Map.put(message, "object", ap_id), meta}
85 {err, message} -> {err, message, meta}
88 {err, message} = filter(message)
95 Pleroma.Config.get([:mrf, :policies], [])
97 |> Enum.concat([Pleroma.Web.ActivityPub.MRF.HashtagPolicy])
100 defp get_policies(policy) when is_atom(policy), do: [policy]
101 defp get_policies(policies) when is_list(policies), do: policies
102 defp get_policies(_), do: []
104 @spec subdomains_regex([String.t()]) :: [Regex.t()]
105 def subdomains_regex(domains) when is_list(domains) do
106 for domain <- domains, do: ~r(^#{String.replace(domain, "*.", "(.*\\.)*")}$)i
109 @spec subdomain_match?([Regex.t()], String.t()) :: boolean()
110 def subdomain_match?(domains, host) do
111 Enum.any?(domains, fn domain -> Regex.match?(domain, host) end)
114 def describe(policies) do
115 {:ok, policy_configs} =
117 |> Enum.reduce({:ok, %{}}, fn
118 policy, {:ok, data} ->
119 {:ok, policy_data} = policy.describe()
120 {:ok, Map.merge(data, policy_data)}
128 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
130 exclusions = Pleroma.Config.get([:mrf, :transparency_exclusions])
134 mrf_policies: mrf_policies,
135 exclusions: length(exclusions) > 0
137 |> Map.merge(policy_configs)
142 def describe, do: get_policies() |> describe()
144 def config_descriptions do
145 Pleroma.Web.ActivityPub.MRF
146 |> Pleroma.Docs.Generator.list_behaviour_implementations()
147 |> config_descriptions()
150 def config_descriptions(policies) do
151 Enum.reduce(policies, @mrf_config_descriptions, fn policy, acc ->
152 if function_exported?(policy, :config_description, 0) do
155 |> Map.merge(policy.config_description)
156 |> Map.put(:group, :pleroma)
157 |> Map.put(:tab, :mrf)
158 |> Map.put(:type, :group)
160 if Enum.all?(@required_description_keys, &Map.has_key?(description, &1)) do
164 "#{policy} config description doesn't have one or all required keys #{
165 inspect(@required_description_keys)
173 "#{policy} is excluded from config descriptions, because does not implement `config_description/0` method."