turn inlineQuotePolicy on by default
[akkoma] / lib / pleroma / web / activity_pub / mrf.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF do
6 require Logger
7
8 @behaviour Pleroma.Web.ActivityPub.MRF.PipelineFiltering
9
10 @mrf_config_descriptions [
11 %{
12 group: :pleroma,
13 key: :mrf,
14 tab: :mrf,
15 label: "MRF",
16 type: :group,
17 description: "General MRF settings",
18 children: [
19 %{
20 key: :policies,
21 type: [:module, {:list, :module}],
22 description:
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}
25 },
26 %{
27 key: :transparency,
28 label: "MRF transparency",
29 type: :boolean,
30 description:
31 "Make the content of your Message Rewrite Facility settings public (via nodeinfo)"
32 },
33 %{
34 key: :transparency_exclusions,
35 label: "MRF transparency exclusions",
36 type: {:list, :tuple},
37 key_placeholder: "instance",
38 value_placeholder: "reason",
39 description:
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.",
41 suggestions: [
42 "exclusion.com"
43 ]
44 },
45 %{
46 key: :transparency_obfuscate_domains,
47 label: "MRF domain obfuscation",
48 type: {:list, :string},
49 description:
50 "Obfuscate domains in MRF transparency. This is useful if the domain you're blocking contains words you don't want displayed, but still want to disclose the MRF settings.",
51 suggestions: [
52 "badword.com"
53 ]
54 }
55 ]
56 }
57 ]
58
59 @default_description %{
60 label: "",
61 description: ""
62 }
63
64 @required_description_keys [:key, :related_policy]
65
66 def filter(policies, %{} = message) do
67 policies
68 |> Enum.reduce({:ok, message}, fn
69 policy, {:ok, message} -> policy.filter(message)
70 _, error -> error
71 end)
72 end
73
74 def filter(%{} = object), do: get_policies() |> filter(object)
75
76 @impl true
77 def pipeline_filter(%{} = message, meta) do
78 object = meta[:object_data]
79 ap_id = message["object"]
80
81 if object && ap_id do
82 with {:ok, message} <- filter(Map.put(message, "object", object)) do
83 meta = Keyword.put(meta, :object_data, message["object"])
84 {:ok, Map.put(message, "object", ap_id), meta}
85 else
86 {err, message} -> {err, message, meta}
87 end
88 else
89 {err, message} = filter(message)
90
91 {err, message, meta}
92 end
93 end
94
95 def get_policies do
96 Pleroma.Config.get([:mrf, :policies], [])
97 |> get_policies()
98 |> Enum.concat([
99 Pleroma.Web.ActivityPub.MRF.HashtagPolicy,
100 Pleroma.Web.ActivityPub.MRF.InlineQuotePolicy
101 ])
102 |> Enum.uniq()
103 end
104
105 defp get_policies(policy) when is_atom(policy), do: [policy]
106 defp get_policies(policies) when is_list(policies), do: policies
107 defp get_policies(_), do: []
108
109 @spec subdomains_regex([String.t()]) :: [Regex.t()]
110 def subdomains_regex(domains) when is_list(domains) do
111 for domain <- domains, do: ~r(^#{String.replace(domain, "*.", "(.*\\.)*")}$)i
112 end
113
114 @spec subdomain_match?([Regex.t()], String.t()) :: boolean()
115 def subdomain_match?(domains, host) do
116 Enum.any?(domains, fn domain -> Regex.match?(domain, host) end)
117 end
118
119 @spec instance_list_from_tuples([{String.t(), String.t()}]) :: [String.t()]
120 def instance_list_from_tuples(list) do
121 Enum.map(list, fn {instance, _} -> instance end)
122 end
123
124 def describe(policies) do
125 {:ok, policy_configs} =
126 policies
127 |> Enum.reduce({:ok, %{}}, fn
128 policy, {:ok, data} ->
129 {:ok, policy_data} = policy.describe()
130 {:ok, Map.merge(data, policy_data)}
131
132 _, error ->
133 error
134 end)
135
136 mrf_policies =
137 get_policies()
138 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
139
140 exclusions = Pleroma.Config.get([:mrf, :transparency_exclusions])
141
142 base =
143 %{
144 mrf_policies: mrf_policies,
145 exclusions: length(exclusions) > 0
146 }
147 |> Map.merge(policy_configs)
148
149 {:ok, base}
150 end
151
152 def describe, do: get_policies() |> describe()
153
154 def config_descriptions do
155 Pleroma.Web.ActivityPub.MRF.Policy
156 |> Pleroma.Docs.Generator.list_behaviour_implementations()
157 |> config_descriptions()
158 end
159
160 def config_descriptions(policies) do
161 Enum.reduce(policies, @mrf_config_descriptions, fn policy, acc ->
162 if function_exported?(policy, :config_description, 0) do
163 description =
164 @default_description
165 |> Map.merge(policy.config_description)
166 |> Map.put(:group, :pleroma)
167 |> Map.put(:tab, :mrf)
168 |> Map.put(:type, :group)
169
170 if Enum.all?(@required_description_keys, &Map.has_key?(description, &1)) do
171 [description | acc]
172 else
173 Logger.warn(
174 "#{policy} config description doesn't have one or all required keys #{inspect(@required_description_keys)}"
175 )
176
177 acc
178 end
179 else
180 Logger.debug(
181 "#{policy} is excluded from config descriptions, because does not implement `config_description/0` method."
182 )
183
184 acc
185 end
186 end)
187 end
188 end