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