extend reject MRF to check if originating instance is blocked
[akkoma] / lib / pleroma / config / deprecation_warnings.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.Config.DeprecationWarnings do
6 alias Pleroma.Config
7
8 require Logger
9 alias Pleroma.Config
10
11 @type config_namespace() :: atom() | [atom()]
12 @type config_map() :: {config_namespace(), config_namespace(), String.t()}
13
14 @mrf_config_map [
15 {[:instance, :rewrite_policy], [:mrf, :policies],
16 "\n* `config :pleroma, :instance, rewrite_policy` is now `config :pleroma, :mrf, policies`"},
17 {[:instance, :mrf_transparency], [:mrf, :transparency],
18 "\n* `config :pleroma, :instance, mrf_transparency` is now `config :pleroma, :mrf, transparency`"},
19 {[:instance, :mrf_transparency_exclusions], [:mrf, :transparency_exclusions],
20 "\n* `config :pleroma, :instance, mrf_transparency_exclusions` is now `config :pleroma, :mrf, transparency_exclusions`"},
21 {[:instance, :quarantined_instances], [:mrf_simple, :reject],
22 "\n* `config :pleroma, :instance, :quarantined_instances` is now covered by `:pleroma, :mrf_simple, :reject`"}
23 ]
24
25 def check_simple_policy_tuples do
26 has_strings =
27 Config.get([:mrf_simple])
28 |> Enum.any?(fn {_, v} -> is_list(v) and Enum.any?(v, &is_binary/1) end)
29
30 if has_strings do
31 Logger.warn("""
32 !!!DEPRECATION WARNING!!!
33 Your config is using strings in the SimplePolicy configuration instead of tuples. They should work for now, but you are advised to change to the new configuration to prevent possible issues later:
34
35 ```
36 config :pleroma, :mrf_simple,
37 media_removal: ["instance.tld"],
38 media_nsfw: ["instance.tld"],
39 federated_timeline_removal: ["instance.tld"],
40 report_removal: ["instance.tld"],
41 reject: ["instance.tld"],
42 followers_only: ["instance.tld"],
43 accept: ["instance.tld"],
44 avatar_removal: ["instance.tld"],
45 banner_removal: ["instance.tld"],
46 reject_deletes: ["instance.tld"]
47 ```
48
49 Is now
50
51
52 ```
53 config :pleroma, :mrf_simple,
54 media_removal: [{"instance.tld", "Reason for media removal"}],
55 media_nsfw: [{"instance.tld", "Reason for media nsfw"}],
56 federated_timeline_removal: [{"instance.tld", "Reason for federated timeline removal"}],
57 report_removal: [{"instance.tld", "Reason for report removal"}],
58 reject: [{"instance.tld", "Reason for reject"}],
59 followers_only: [{"instance.tld", "Reason for followers only"}],
60 accept: [{"instance.tld", "Reason for accept"}],
61 avatar_removal: [{"instance.tld", "Reason for avatar removal"}],
62 banner_removal: [{"instance.tld", "Reason for banner removal"}],
63 reject_deletes: [{"instance.tld", "Reason for reject deletes"}]
64 ```
65 """)
66
67 new_config =
68 Config.get([:mrf_simple])
69 |> Enum.filter(fn {k, v} -> not is_atom(v) end)
70 |> Enum.map(fn {k, v} ->
71 {k,
72 Enum.map(v, fn
73 {instance, reason} -> {instance, reason}
74 instance -> {instance, ""}
75 end)}
76 end)
77
78 Config.put([:mrf_simple], new_config)
79
80 :error
81 else
82 :ok
83 end
84 end
85
86 def check_quarantined_instances_tuples do
87 has_strings = Config.get([:instance, :quarantined_instances], []) |> Enum.any?(&is_binary/1)
88
89 if has_strings do
90 Logger.warn("""
91 !!!DEPRECATION WARNING!!!
92 Your config is using strings in the quarantined_instances configuration instead of tuples. They should work for now, but you are advised to change to the new configuration to prevent possible issues later:
93
94 ```
95 config :pleroma, :instance,
96 quarantined_instances: ["instance.tld"]
97 ```
98
99 Is now
100
101
102 ```
103 config :pleroma, :instance,
104 quarantined_instances: [{"instance.tld", "Reason for quarantine"}]
105 ```
106 """)
107
108 new_config =
109 Config.get([:instance, :quarantined_instances])
110 |> Enum.map(fn
111 {instance, reason} -> {instance, reason}
112 instance -> {instance, ""}
113 end)
114
115 Config.put([:instance, :quarantined_instances], new_config)
116
117 :error
118 else
119 :ok
120 end
121 end
122
123 def check_transparency_exclusions_tuples do
124 has_strings = Config.get([:mrf, :transparency_exclusions]) |> Enum.any?(&is_binary/1)
125
126 if has_strings do
127 Logger.warn("""
128 !!!DEPRECATION WARNING!!!
129 Your config is using strings in the transparency_exclusions configuration instead of tuples. They should work for now, but you are advised to change to the new configuration to prevent possible issues later:
130
131 ```
132 config :pleroma, :mrf,
133 transparency_exclusions: ["instance.tld"]
134 ```
135
136 Is now
137
138
139 ```
140 config :pleroma, :mrf,
141 transparency_exclusions: [{"instance.tld", "Reason to exlude transparency"}]
142 ```
143 """)
144
145 new_config =
146 Config.get([:mrf, :transparency_exclusions])
147 |> Enum.map(fn
148 {instance, reason} -> {instance, reason}
149 instance -> {instance, ""}
150 end)
151
152 Config.put([:mrf, :transparency_exclusions], new_config)
153
154 :error
155 else
156 :ok
157 end
158 end
159
160 def check_hellthread_threshold do
161 if Config.get([:mrf_hellthread, :threshold]) do
162 Logger.warn("""
163 !!!DEPRECATION WARNING!!!
164 You are using the old configuration mechanism for the hellthread filter. Please check config.md.
165 """)
166
167 :error
168 else
169 :ok
170 end
171 end
172
173 def warn do
174 [
175 check_hellthread_threshold(),
176 check_old_mrf_config(),
177 check_media_proxy_whitelist_config(),
178 check_welcome_message_config(),
179 check_activity_expiration_config(),
180 check_remote_ip_plug_name(),
181 check_uploders_s3_public_endpoint(),
182 check_quarantined_instances_tuples(),
183 check_transparency_exclusions_tuples(),
184 check_simple_policy_tuples()
185 ]
186 |> Enum.reduce(:ok, fn
187 :ok, :ok -> :ok
188 _, _ -> :error
189 end)
190 end
191
192 def check_welcome_message_config do
193 instance_config = Pleroma.Config.get([:instance])
194
195 use_old_config =
196 Keyword.has_key?(instance_config, :welcome_user_nickname) or
197 Keyword.has_key?(instance_config, :welcome_message)
198
199 if use_old_config do
200 Logger.error("""
201 !!!DEPRECATION WARNING!!!
202 Your config is using the old namespace for Welcome messages configuration. You need to convert to the new namespace. e.g.,
203 \n* `config :pleroma, :instance, welcome_user_nickname` and `config :pleroma, :instance, welcome_message` are now equal to:
204 \n* `config :pleroma, :welcome, direct_message: [enabled: true, sender_nickname: "NICKNAME", message: "Your welcome message"]`"
205 """)
206
207 :error
208 else
209 :ok
210 end
211 end
212
213 def check_old_mrf_config do
214 warning_preface = """
215 !!!DEPRECATION WARNING!!!
216 Your config is using old namespaces for MRF configuration. They should work for now, but you are advised to change to new namespaces to prevent possible issues later:
217 """
218
219 move_namespace_and_warn(@mrf_config_map, warning_preface)
220 end
221
222 @spec move_namespace_and_warn([config_map()], String.t()) :: :ok | nil
223 def move_namespace_and_warn(config_map, warning_preface) do
224 warning =
225 Enum.reduce(config_map, "", fn
226 {old, new, err_msg}, acc ->
227 old_config = Config.get(old)
228
229 if old_config do
230 Config.put(new, old_config)
231 acc <> err_msg
232 else
233 acc
234 end
235 end)
236
237 if warning == "" do
238 :ok
239 else
240 Logger.warn(warning_preface <> warning)
241 :error
242 end
243 end
244
245 @spec check_media_proxy_whitelist_config() :: :ok | nil
246 def check_media_proxy_whitelist_config do
247 whitelist = Config.get([:media_proxy, :whitelist])
248
249 if Enum.any?(whitelist, &(not String.starts_with?(&1, "http"))) do
250 Logger.warn("""
251 !!!DEPRECATION WARNING!!!
252 Your config is using old format (only domain) for MediaProxy whitelist option. Setting should work for now, but you are advised to change format to scheme with port to prevent possible issues later.
253 """)
254
255 :error
256 else
257 :ok
258 end
259 end
260
261 @spec check_activity_expiration_config() :: :ok | nil
262 def check_activity_expiration_config do
263 warning_preface = """
264 !!!DEPRECATION WARNING!!!
265 Your config is using old namespace for activity expiration configuration. Setting should work for now, but you are advised to change to new namespace to prevent possible issues later:
266 """
267
268 move_namespace_and_warn(
269 [
270 {Pleroma.ActivityExpiration, Pleroma.Workers.PurgeExpiredActivity,
271 "\n* `config :pleroma, Pleroma.ActivityExpiration` is now `config :pleroma, Pleroma.Workers.PurgeExpiredActivity`"}
272 ],
273 warning_preface
274 )
275 end
276
277 @spec check_remote_ip_plug_name() :: :ok | nil
278 def check_remote_ip_plug_name do
279 warning_preface = """
280 !!!DEPRECATION WARNING!!!
281 Your config is using old namespace for RemoteIp Plug. Setting should work for now, but you are advised to change to new namespace to prevent possible issues later:
282 """
283
284 move_namespace_and_warn(
285 [
286 {Pleroma.Plugs.RemoteIp, Pleroma.Web.Plugs.RemoteIp,
287 "\n* `config :pleroma, Pleroma.Plugs.RemoteIp` is now `config :pleroma, Pleroma.Web.Plugs.RemoteIp`"}
288 ],
289 warning_preface
290 )
291 end
292
293 @spec check_uploders_s3_public_endpoint() :: :ok | nil
294 def check_uploders_s3_public_endpoint do
295 s3_config = Pleroma.Config.get([Pleroma.Uploaders.S3])
296
297 use_old_config = Keyword.has_key?(s3_config, :public_endpoint)
298
299 if use_old_config do
300 Logger.error("""
301 !!!DEPRECATION WARNING!!!
302 Your config is using the old setting for controlling the URL of media uploaded to your S3 bucket.\n
303 Please make the following change at your earliest convenience.\n
304 \n* `config :pleroma, Pleroma.Uploaders.S3, public_endpoint` is now equal to:
305 \n* `config :pleroma, Pleroma.Upload, base_url`
306 """)
307
308 :error
309 else
310 :ok
311 end
312 end
313 end