Merge branch 'develop' into feature/gen-magic
[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 warn do
37 with :ok <- check_hellthread_threshold(),
38 :ok <- check_old_mrf_config(),
39 :ok <- check_media_proxy_whitelist_config(),
40 :ok <- check_welcome_message_config(),
41 :ok <- check_gun_pool_options(),
42 :ok <- check_activity_expiration_config() do
43 :ok
44 else
45 _ ->
46 :error
47 end
48 end
49
50 def check_welcome_message_config do
51 instance_config = Pleroma.Config.get([:instance])
52
53 use_old_config =
54 Keyword.has_key?(instance_config, :welcome_user_nickname) or
55 Keyword.has_key?(instance_config, :welcome_message)
56
57 if use_old_config do
58 Logger.error("""
59 !!!DEPRECATION WARNING!!!
60 Your config is using the old namespace for Welcome messages configuration. You need to convert to the new namespace. e.g.,
61 \n* `config :pleroma, :instance, welcome_user_nickname` and `config :pleroma, :instance, welcome_message` are now equal to:
62 \n* `config :pleroma, :welcome, direct_message: [enabled: true, sender_nickname: "NICKNAME", message: "Your welcome message"]`"
63 """)
64
65 :error
66 else
67 :ok
68 end
69 end
70
71 def check_old_mrf_config do
72 warning_preface = """
73 !!!DEPRECATION WARNING!!!
74 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:
75 """
76
77 move_namespace_and_warn(@mrf_config_map, warning_preface)
78 end
79
80 @spec move_namespace_and_warn([config_map()], String.t()) :: :ok | nil
81 def move_namespace_and_warn(config_map, warning_preface) do
82 warning =
83 Enum.reduce(config_map, "", fn
84 {old, new, err_msg}, acc ->
85 old_config = Config.get(old)
86
87 if old_config do
88 Config.put(new, old_config)
89 acc <> err_msg
90 else
91 acc
92 end
93 end)
94
95 if warning == "" do
96 :ok
97 else
98 Logger.warn(warning_preface <> warning)
99 :error
100 end
101 end
102
103 @spec check_media_proxy_whitelist_config() :: :ok | nil
104 def check_media_proxy_whitelist_config do
105 whitelist = Config.get([:media_proxy, :whitelist])
106
107 if Enum.any?(whitelist, &(not String.starts_with?(&1, "http"))) do
108 Logger.warn("""
109 !!!DEPRECATION WARNING!!!
110 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.
111 """)
112
113 :error
114 else
115 :ok
116 end
117 end
118
119 def check_gun_pool_options do
120 pool_config = Config.get(:connections_pool)
121
122 if timeout = pool_config[:await_up_timeout] do
123 Logger.warn("""
124 !!!DEPRECATION WARNING!!!
125 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.
126 """)
127
128 Config.put(:connections_pool, Keyword.put_new(pool_config, :connect_timeout, timeout))
129 end
130
131 pools_configs = Config.get(:pools)
132
133 warning_preface = """
134 !!!DEPRECATION WARNING!!!
135 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.
136 """
137
138 updated_config =
139 Enum.reduce(pools_configs, [], fn {pool_name, config}, acc ->
140 if timeout = config[:timeout] do
141 Keyword.put(acc, pool_name, Keyword.put_new(config, :recv_timeout, timeout))
142 else
143 acc
144 end
145 end)
146
147 if updated_config != [] do
148 pool_warnings =
149 updated_config
150 |> Keyword.keys()
151 |> Enum.map(fn pool_name ->
152 "\n* `:timeout` options in #{pool_name} pool is now `:recv_timeout`"
153 end)
154
155 Logger.warn(Enum.join([warning_preface | pool_warnings]))
156
157 Config.put(:pools, updated_config)
158 :error
159 else
160 :ok
161 end
162 end
163
164 @spec check_activity_expiration_config() :: :ok | nil
165 def check_activity_expiration_config do
166 warning_preface = """
167 !!!DEPRECATION WARNING!!!
168 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:
169 """
170
171 move_namespace_and_warn(
172 [
173 {Pleroma.ActivityExpiration, Pleroma.Workers.PurgeExpiredActivity,
174 "\n* `config :pleroma, Pleroma.ActivityExpiration` is now `config :pleroma, Pleroma.Workers.PurgeExpiredActivity`"}
175 ],
176 warning_preface
177 )
178 end
179 end