b53b15d9560ffa154c565535a63ba495277b1644
[anni] / lib / pleroma / config / deprecation_warnings.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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   ]
22
23   def check_exiftool_filter do
24     filters = Config.get([Pleroma.Upload]) |> Keyword.get(:filters, [])
25
26     if Pleroma.Upload.Filter.Exiftool in filters do
27       Logger.warn("""
28       !!!DEPRECATION WARNING!!!
29       Your config is using Exiftool as a filter instead of Exiftool.StripLocation. This should work for now, but you are advised to change to the new configuration to prevent possible issues later:
30
31       ```
32       config :pleroma, Pleroma.Upload,
33         filters: [Pleroma.Upload.Filter.Exiftool]
34       ```
35
36       Is now
37
38
39       ```
40       config :pleroma, Pleroma.Upload,
41         filters: [Pleroma.Upload.Filter.Exiftool.StripLocation]
42       ```
43       """)
44
45       new_config =
46         filters
47         |> Enum.map(fn
48           Pleroma.Upload.Filter.Exiftool -> Pleroma.Upload.Filter.Exiftool.StripLocation
49           filter -> filter
50         end)
51
52       Config.put([Pleroma.Upload, :filters], new_config)
53
54       :error
55     else
56       :ok
57     end
58   end
59
60   def check_simple_policy_tuples do
61     has_strings =
62       Config.get([:mrf_simple])
63       |> Enum.any?(fn {_, v} -> Enum.any?(v, &is_binary/1) end)
64
65     if has_strings do
66       Logger.warn("""
67       !!!DEPRECATION WARNING!!!
68       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:
69
70       ```
71       config :pleroma, :mrf_simple,
72         media_removal: ["instance.tld"],
73         media_nsfw: ["instance.tld"],
74         federated_timeline_removal: ["instance.tld"],
75         report_removal: ["instance.tld"],
76         reject: ["instance.tld"],
77         followers_only: ["instance.tld"],
78         accept: ["instance.tld"],
79         avatar_removal: ["instance.tld"],
80         banner_removal: ["instance.tld"],
81         reject_deletes: ["instance.tld"]
82       ```
83
84       Is now
85
86
87       ```
88       config :pleroma, :mrf_simple,
89         media_removal: [{"instance.tld", "Reason for media removal"}],
90         media_nsfw: [{"instance.tld", "Reason for media nsfw"}],
91         federated_timeline_removal: [{"instance.tld", "Reason for federated timeline removal"}],
92         report_removal: [{"instance.tld", "Reason for report removal"}],
93         reject: [{"instance.tld", "Reason for reject"}],
94         followers_only: [{"instance.tld", "Reason for followers only"}],
95         accept: [{"instance.tld", "Reason for accept"}],
96         avatar_removal: [{"instance.tld", "Reason for avatar removal"}],
97         banner_removal: [{"instance.tld", "Reason for banner removal"}],
98         reject_deletes: [{"instance.tld", "Reason for reject deletes"}]
99       ```
100       """)
101
102       new_config =
103         Config.get([:mrf_simple])
104         |> Enum.map(fn {k, v} ->
105           {k,
106            Enum.map(v, fn
107              {instance, reason} -> {instance, reason}
108              instance -> {instance, ""}
109            end)}
110         end)
111
112       Config.put([:mrf_simple], new_config)
113
114       :error
115     else
116       :ok
117     end
118   end
119
120   def check_quarantined_instances_tuples do
121     has_strings = Config.get([:instance, :quarantined_instances]) |> Enum.any?(&is_binary/1)
122
123     if has_strings do
124       Logger.warn("""
125       !!!DEPRECATION WARNING!!!
126       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:
127
128       ```
129       config :pleroma, :instance,
130         quarantined_instances: ["instance.tld"]
131       ```
132
133       Is now
134
135
136       ```
137       config :pleroma, :instance,
138         quarantined_instances: [{"instance.tld", "Reason for quarantine"}]
139       ```
140       """)
141
142       new_config =
143         Config.get([:instance, :quarantined_instances])
144         |> Enum.map(fn
145           {instance, reason} -> {instance, reason}
146           instance -> {instance, ""}
147         end)
148
149       Config.put([:instance, :quarantined_instances], new_config)
150
151       :error
152     else
153       :ok
154     end
155   end
156
157   def check_transparency_exclusions_tuples do
158     has_strings = Config.get([:mrf, :transparency_exclusions]) |> Enum.any?(&is_binary/1)
159
160     if has_strings do
161       Logger.warn("""
162       !!!DEPRECATION WARNING!!!
163       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:
164
165       ```
166       config :pleroma, :mrf,
167         transparency_exclusions: ["instance.tld"]
168       ```
169
170       Is now
171
172
173       ```
174       config :pleroma, :mrf,
175         transparency_exclusions: [{"instance.tld", "Reason to exlude transparency"}]
176       ```
177       """)
178
179       new_config =
180         Config.get([:mrf, :transparency_exclusions])
181         |> Enum.map(fn
182           {instance, reason} -> {instance, reason}
183           instance -> {instance, ""}
184         end)
185
186       Config.put([:mrf, :transparency_exclusions], new_config)
187
188       :error
189     else
190       :ok
191     end
192   end
193
194   def check_hellthread_threshold do
195     if Config.get([:mrf_hellthread, :threshold]) do
196       Logger.warn("""
197       !!!DEPRECATION WARNING!!!
198       You are using the old configuration mechanism for the hellthread filter. Please check config.md.
199       """)
200
201       :error
202     else
203       :ok
204     end
205   end
206
207   def warn do
208     [
209       check_hellthread_threshold(),
210       check_old_mrf_config(),
211       check_media_proxy_whitelist_config(),
212       check_welcome_message_config(),
213       check_gun_pool_options(),
214       check_activity_expiration_config(),
215       check_remote_ip_plug_name(),
216       check_uploders_s3_public_endpoint(),
217       check_old_chat_shoutbox(),
218       check_quarantined_instances_tuples(),
219       check_transparency_exclusions_tuples(),
220       check_simple_policy_tuples(),
221       check_exiftool_filter()
222     ]
223     |> Enum.reduce(:ok, fn
224       :ok, :ok -> :ok
225       _, _ -> :error
226     end)
227   end
228
229   def check_welcome_message_config do
230     instance_config = Pleroma.Config.get([:instance])
231
232     use_old_config =
233       Keyword.has_key?(instance_config, :welcome_user_nickname) or
234         Keyword.has_key?(instance_config, :welcome_message)
235
236     if use_old_config do
237       Logger.error("""
238       !!!DEPRECATION WARNING!!!
239       Your config is using the old namespace for Welcome messages configuration. You need to convert to the new namespace. e.g.,
240       \n* `config :pleroma, :instance, welcome_user_nickname` and `config :pleroma, :instance, welcome_message` are now equal to:
241       \n* `config :pleroma, :welcome, direct_message: [enabled: true, sender_nickname: "NICKNAME", message: "Your welcome message"]`"
242       """)
243
244       :error
245     else
246       :ok
247     end
248   end
249
250   def check_old_mrf_config do
251     warning_preface = """
252     !!!DEPRECATION WARNING!!!
253     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:
254     """
255
256     move_namespace_and_warn(@mrf_config_map, warning_preface)
257   end
258
259   @spec move_namespace_and_warn([config_map()], String.t()) :: :ok | nil
260   def move_namespace_and_warn(config_map, warning_preface) do
261     warning =
262       Enum.reduce(config_map, "", fn
263         {old, new, err_msg}, acc ->
264           old_config = Config.get(old)
265
266           if old_config do
267             Config.put(new, old_config)
268             acc <> err_msg
269           else
270             acc
271           end
272       end)
273
274     if warning == "" do
275       :ok
276     else
277       Logger.warn(warning_preface <> warning)
278       :error
279     end
280   end
281
282   @spec check_media_proxy_whitelist_config() :: :ok | nil
283   def check_media_proxy_whitelist_config do
284     whitelist = Config.get([:media_proxy, :whitelist])
285
286     if Enum.any?(whitelist, &(not String.starts_with?(&1, "http"))) do
287       Logger.warn("""
288       !!!DEPRECATION WARNING!!!
289       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.
290       """)
291
292       :error
293     else
294       :ok
295     end
296   end
297
298   def check_gun_pool_options do
299     pool_config = Config.get(:connections_pool)
300
301     if timeout = pool_config[:await_up_timeout] do
302       Logger.warn("""
303       !!!DEPRECATION WARNING!!!
304       Your config is using old setting `config :pleroma, :connections_pool, await_up_timeout`. Please change to `config :pleroma, :connections_pool, connect_timeout` to ensure compatibility with future releases.
305       """)
306
307       Config.put(:connections_pool, Keyword.put_new(pool_config, :connect_timeout, timeout))
308     end
309
310     pools_configs = Config.get(:pools)
311
312     warning_preface = """
313     !!!DEPRECATION WARNING!!!
314     Your config is using old setting name `timeout` instead of `recv_timeout` in pool settings. The setting will not take effect until updated.
315     """
316
317     updated_config =
318       Enum.reduce(pools_configs, [], fn {pool_name, config}, acc ->
319         if timeout = config[:timeout] do
320           Keyword.put(acc, pool_name, Keyword.put_new(config, :recv_timeout, timeout))
321         else
322           acc
323         end
324       end)
325
326     if updated_config != [] do
327       pool_warnings =
328         updated_config
329         |> Keyword.keys()
330         |> Enum.map(fn pool_name ->
331           "\n* `:timeout` options in #{pool_name} pool is now `:recv_timeout`"
332         end)
333
334       Logger.warn(Enum.join([warning_preface | pool_warnings]))
335
336       Config.put(:pools, updated_config)
337       :error
338     else
339       :ok
340     end
341   end
342
343   @spec check_activity_expiration_config() :: :ok | nil
344   def check_activity_expiration_config do
345     warning_preface = """
346     !!!DEPRECATION WARNING!!!
347       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:
348     """
349
350     move_namespace_and_warn(
351       [
352         {Pleroma.ActivityExpiration, Pleroma.Workers.PurgeExpiredActivity,
353          "\n* `config :pleroma, Pleroma.ActivityExpiration` is now `config :pleroma, Pleroma.Workers.PurgeExpiredActivity`"}
354       ],
355       warning_preface
356     )
357   end
358
359   @spec check_remote_ip_plug_name() :: :ok | nil
360   def check_remote_ip_plug_name do
361     warning_preface = """
362     !!!DEPRECATION WARNING!!!
363     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:
364     """
365
366     move_namespace_and_warn(
367       [
368         {Pleroma.Plugs.RemoteIp, Pleroma.Web.Plugs.RemoteIp,
369          "\n* `config :pleroma, Pleroma.Plugs.RemoteIp` is now `config :pleroma, Pleroma.Web.Plugs.RemoteIp`"}
370       ],
371       warning_preface
372     )
373   end
374
375   @spec check_uploders_s3_public_endpoint() :: :ok | nil
376   def check_uploders_s3_public_endpoint do
377     s3_config = Pleroma.Config.get([Pleroma.Uploaders.S3])
378
379     use_old_config = Keyword.has_key?(s3_config, :public_endpoint)
380
381     if use_old_config do
382       Logger.error("""
383       !!!DEPRECATION WARNING!!!
384       Your config is using the old setting for controlling the URL of media uploaded to your S3 bucket.\n
385       Please make the following change at your earliest convenience.\n
386       \n* `config :pleroma, Pleroma.Uploaders.S3, public_endpoint` is now equal to:
387       \n* `config :pleroma, Pleroma.Upload, base_url`
388       """)
389
390       :error
391     else
392       :ok
393     end
394   end
395
396   @spec check_old_chat_shoutbox() :: :ok | nil
397   def check_old_chat_shoutbox do
398     instance_config = Pleroma.Config.get([:instance])
399     chat_config = Pleroma.Config.get([:chat]) || []
400
401     use_old_config =
402       Keyword.has_key?(instance_config, :chat_limit) or
403         Keyword.has_key?(chat_config, :enabled)
404
405     if use_old_config do
406       Logger.error("""
407       !!!DEPRECATION WARNING!!!
408       Your config is using the old namespace for the Shoutbox configuration. You need to convert to the new namespace. e.g.,
409       \n* `config :pleroma, :chat, enabled` and `config :pleroma, :instance, chat_limit` are now equal to:
410       \n* `config :pleroma, :shout, enabled` and `config :pleroma, :shout, limit`
411       """)
412
413       :error
414     else
415       :ok
416     end
417   end
418 end