Merge branch 'instance-docs' into 'develop'
[akkoma] / lib / pleroma / config / deprecation_warnings.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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_hellthread_threshold do
24 if Config.get([:mrf_hellthread, :threshold]) do
25 Logger.warn("""
26 !!!DEPRECATION WARNING!!!
27 You are using the old configuration mechanism for the hellthread filter. Please check config.md.
28 """)
29
30 :error
31 else
32 :ok
33 end
34 end
35
36 def mrf_user_allowlist do
37 config = Config.get(:mrf_user_allowlist)
38
39 if config && Enum.any?(config, fn {k, _} -> is_atom(k) end) do
40 rewritten =
41 Enum.reduce(Config.get(:mrf_user_allowlist), Map.new(), fn {k, v}, acc ->
42 Map.put(acc, to_string(k), v)
43 end)
44
45 Config.put(:mrf_user_allowlist, rewritten)
46
47 Logger.error("""
48 !!!DEPRECATION WARNING!!!
49 As of Pleroma 2.0.7, the `mrf_user_allowlist` setting changed of format.
50 Pleroma 2.1 will remove support for the old format. Please change your configuration to match this:
51
52 config :pleroma, :mrf_user_allowlist, #{inspect(rewritten, pretty: true)}
53 """)
54
55 :error
56 else
57 :ok
58 end
59 end
60
61 def warn do
62 with :ok <- check_hellthread_threshold(),
63 :ok <- mrf_user_allowlist(),
64 :ok <- check_old_mrf_config(),
65 :ok <- check_media_proxy_whitelist_config(),
66 :ok <- check_welcome_message_config(),
67 :ok <- check_gun_pool_options(),
68 :ok <- check_activity_expiration_config() do
69 :ok
70 else
71 _ ->
72 :error
73 end
74 end
75
76 def check_welcome_message_config do
77 instance_config = Pleroma.Config.get([:instance])
78
79 use_old_config =
80 Keyword.has_key?(instance_config, :welcome_user_nickname) or
81 Keyword.has_key?(instance_config, :welcome_message)
82
83 if use_old_config do
84 Logger.error("""
85 !!!DEPRECATION WARNING!!!
86 Your config is using the old namespace for Welcome messages configuration. You need to change to the new namespace:
87 \n* `config :pleroma, :instance, welcome_user_nickname` is now `config :pleroma, :welcome, :direct_message, :sender_nickname`
88 \n* `config :pleroma, :instance, welcome_message` is now `config :pleroma, :welcome, :direct_message, :message`
89 """)
90
91 :error
92 else
93 :ok
94 end
95 end
96
97 def check_old_mrf_config do
98 warning_preface = """
99 !!!DEPRECATION WARNING!!!
100 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:
101 """
102
103 move_namespace_and_warn(@mrf_config_map, warning_preface)
104 end
105
106 @spec move_namespace_and_warn([config_map()], String.t()) :: :ok | nil
107 def move_namespace_and_warn(config_map, warning_preface) do
108 warning =
109 Enum.reduce(config_map, "", fn
110 {old, new, err_msg}, acc ->
111 old_config = Config.get(old)
112
113 if old_config do
114 Config.put(new, old_config)
115 acc <> err_msg
116 else
117 acc
118 end
119 end)
120
121 if warning == "" do
122 :ok
123 else
124 Logger.warn(warning_preface <> warning)
125 :error
126 end
127 end
128
129 @spec check_media_proxy_whitelist_config() :: :ok | nil
130 def check_media_proxy_whitelist_config do
131 whitelist = Config.get([:media_proxy, :whitelist])
132
133 if Enum.any?(whitelist, &(not String.starts_with?(&1, "http"))) do
134 Logger.warn("""
135 !!!DEPRECATION WARNING!!!
136 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.
137 """)
138
139 :error
140 else
141 :ok
142 end
143 end
144
145 def check_gun_pool_options do
146 pool_config = Config.get(:connections_pool)
147
148 if timeout = pool_config[:await_up_timeout] do
149 Logger.warn("""
150 !!!DEPRECATION WARNING!!!
151 Your config is using old setting name `await_up_timeout` instead of `connect_timeout`. Setting should work for now, but you are advised to change format to scheme with port to prevent possible issues later.
152 """)
153
154 Config.put(:connections_pool, Keyword.put_new(pool_config, :connect_timeout, timeout))
155 end
156
157 pools_configs = Config.get(:pools)
158
159 warning_preface = """
160 !!!DEPRECATION WARNING!!!
161 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.
162 """
163
164 updated_config =
165 Enum.reduce(pools_configs, [], fn {pool_name, config}, acc ->
166 if timeout = config[:timeout] do
167 Keyword.put(acc, pool_name, Keyword.put_new(config, :recv_timeout, timeout))
168 else
169 acc
170 end
171 end)
172
173 if updated_config != [] do
174 pool_warnings =
175 updated_config
176 |> Keyword.keys()
177 |> Enum.map(fn pool_name ->
178 "\n* `:timeout` options in #{pool_name} pool is now `:recv_timeout`"
179 end)
180
181 Logger.warn(Enum.join([warning_preface | pool_warnings]))
182
183 Config.put(:pools, updated_config)
184 :error
185 else
186 :ok
187 end
188 end
189
190 @spec check_activity_expiration_config() :: :ok | nil
191 def check_activity_expiration_config do
192 warning_preface = """
193 !!!DEPRECATION WARNING!!!
194 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:
195 """
196
197 move_namespace_and_warn(
198 [
199 {Pleroma.ActivityExpiration, Pleroma.Workers.PurgeExpiredActivity,
200 "\n* `config :pleroma, Pleroma.ActivityExpiration` is now `config :pleroma, Pleroma.Workers.PurgeExpiredActivity`"}
201 ],
202 warning_preface
203 )
204 end
205 end