Merge branch 'fix/no-email-no-fail' into 'develop'
[akkoma] / lib / pleroma / config / config_db.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.ConfigDB do
6 use Ecto.Schema
7
8 import Ecto.Changeset
9 import Ecto.Query
10 import Pleroma.Web.Gettext
11
12 alias __MODULE__
13 alias Pleroma.Repo
14
15 @type t :: %__MODULE__{}
16
17 @full_key_update [
18 {:pleroma, :ecto_repos},
19 {:quack, :meta},
20 {:mime, :types},
21 {:cors_plug, [:max_age, :methods, :expose, :headers]},
22 {:auto_linker, :opts},
23 {:swarm, :node_blacklist},
24 {:logger, :backends}
25 ]
26
27 @full_subkey_update [
28 {:pleroma, :assets, :mascots},
29 {:pleroma, :emoji, :groups},
30 {:pleroma, :workers, :retries},
31 {:pleroma, :mrf_subchain, :match_actor},
32 {:pleroma, :mrf_keyword, :replace}
33 ]
34
35 @regex ~r/^~r(?'delimiter'[\/|"'([{<]{1})(?'pattern'.+)[\/|"')\]}>]{1}(?'modifier'[uismxfU]*)/u
36
37 @delimiters ["/", "|", "\"", "'", {"(", ")"}, {"[", "]"}, {"{", "}"}, {"<", ">"}]
38
39 schema "config" do
40 field(:key, :string)
41 field(:group, :string)
42 field(:value, :binary)
43 field(:db, {:array, :string}, virtual: true, default: [])
44
45 timestamps()
46 end
47
48 @spec get_all_as_keyword() :: keyword()
49 def get_all_as_keyword do
50 ConfigDB
51 |> select([c], {c.group, c.key, c.value})
52 |> Repo.all()
53 |> Enum.reduce([], fn {group, key, value}, acc ->
54 group = ConfigDB.from_string(group)
55 key = ConfigDB.from_string(key)
56 value = from_binary(value)
57
58 Keyword.update(acc, group, [{key, value}], &Keyword.merge(&1, [{key, value}]))
59 end)
60 end
61
62 @spec get_by_params(map()) :: ConfigDB.t() | nil
63 def get_by_params(params), do: Repo.get_by(ConfigDB, params)
64
65 @spec changeset(ConfigDB.t(), map()) :: Changeset.t()
66 def changeset(config, params \\ %{}) do
67 params = Map.put(params, :value, transform(params[:value]))
68
69 config
70 |> cast(params, [:key, :group, :value])
71 |> validate_required([:key, :group, :value])
72 |> unique_constraint(:key, name: :config_group_key_index)
73 end
74
75 @spec create(map()) :: {:ok, ConfigDB.t()} | {:error, Changeset.t()}
76 def create(params) do
77 %ConfigDB{}
78 |> changeset(params)
79 |> Repo.insert()
80 end
81
82 @spec update(ConfigDB.t(), map()) :: {:ok, ConfigDB.t()} | {:error, Changeset.t()}
83 def update(%ConfigDB{} = config, %{value: value}) do
84 config
85 |> changeset(%{value: value})
86 |> Repo.update()
87 end
88
89 @spec get_db_keys(ConfigDB.t()) :: [String.t()]
90 def get_db_keys(%ConfigDB{} = config) do
91 config.value
92 |> ConfigDB.from_binary()
93 |> get_db_keys(config.key)
94 end
95
96 @spec get_db_keys(keyword(), any()) :: [String.t()]
97 def get_db_keys(value, key) do
98 if Keyword.keyword?(value) do
99 value |> Keyword.keys() |> Enum.map(&convert(&1))
100 else
101 [convert(key)]
102 end
103 end
104
105 @spec merge_group(atom(), atom(), keyword(), keyword()) :: keyword()
106 def merge_group(group, key, old_value, new_value) do
107 new_keys = to_map_set(new_value)
108
109 intersect_keys =
110 old_value |> to_map_set() |> MapSet.intersection(new_keys) |> MapSet.to_list()
111
112 merged_value = ConfigDB.merge(old_value, new_value)
113
114 @full_subkey_update
115 |> Enum.map(fn
116 {g, k, subkey} when g == group and k == key ->
117 if subkey in intersect_keys, do: subkey, else: []
118
119 _ ->
120 []
121 end)
122 |> List.flatten()
123 |> Enum.reduce(merged_value, fn subkey, acc ->
124 Keyword.put(acc, subkey, new_value[subkey])
125 end)
126 end
127
128 defp to_map_set(keyword) do
129 keyword
130 |> Keyword.keys()
131 |> MapSet.new()
132 end
133
134 @spec sub_key_full_update?(atom(), atom(), [Keyword.key()]) :: boolean()
135 def sub_key_full_update?(group, key, subkeys) do
136 Enum.any?(@full_subkey_update, fn {g, k, subkey} ->
137 g == group and k == key and subkey in subkeys
138 end)
139 end
140
141 @spec merge(keyword(), keyword()) :: keyword()
142 def merge(config1, config2) when is_list(config1) and is_list(config2) do
143 Keyword.merge(config1, config2, fn _, app1, app2 ->
144 if Keyword.keyword?(app1) and Keyword.keyword?(app2) do
145 Keyword.merge(app1, app2, &deep_merge/3)
146 else
147 app2
148 end
149 end)
150 end
151
152 defp deep_merge(_key, value1, value2) do
153 if Keyword.keyword?(value1) and Keyword.keyword?(value2) do
154 Keyword.merge(value1, value2, &deep_merge/3)
155 else
156 value2
157 end
158 end
159
160 @spec update_or_create(map()) :: {:ok, ConfigDB.t()} | {:error, Changeset.t()}
161 def update_or_create(params) do
162 search_opts = Map.take(params, [:group, :key])
163
164 with %ConfigDB{} = config <- ConfigDB.get_by_params(search_opts),
165 {:partial_update, true, config} <-
166 {:partial_update, can_be_partially_updated?(config), config},
167 old_value <- from_binary(config.value),
168 transformed_value <- do_transform(params[:value]),
169 {:can_be_merged, true, config} <- {:can_be_merged, is_list(transformed_value), config},
170 new_value <-
171 merge_group(
172 ConfigDB.from_string(config.group),
173 ConfigDB.from_string(config.key),
174 old_value,
175 transformed_value
176 ) do
177 ConfigDB.update(config, %{value: new_value})
178 else
179 {reason, false, config} when reason in [:partial_update, :can_be_merged] ->
180 ConfigDB.update(config, params)
181
182 nil ->
183 ConfigDB.create(params)
184 end
185 end
186
187 defp can_be_partially_updated?(%ConfigDB{} = config), do: not only_full_update?(config)
188
189 defp only_full_update?(%ConfigDB{} = config) do
190 config_group = ConfigDB.from_string(config.group)
191 config_key = ConfigDB.from_string(config.key)
192
193 Enum.any?(@full_key_update, fn
194 {group, key} when is_list(key) ->
195 config_group == group and config_key in key
196
197 {group, key} ->
198 config_group == group and config_key == key
199 end)
200 end
201
202 @spec delete(map()) :: {:ok, ConfigDB.t()} | {:error, Changeset.t()}
203 def delete(params) do
204 search_opts = Map.delete(params, :subkeys)
205
206 with %ConfigDB{} = config <- ConfigDB.get_by_params(search_opts),
207 {config, sub_keys} when is_list(sub_keys) <- {config, params[:subkeys]},
208 old_value <- from_binary(config.value),
209 keys <- Enum.map(sub_keys, &do_transform_string(&1)),
210 {:partial_remove, config, new_value} when new_value != [] <-
211 {:partial_remove, config, Keyword.drop(old_value, keys)} do
212 ConfigDB.update(config, %{value: new_value})
213 else
214 {:partial_remove, config, []} ->
215 Repo.delete(config)
216
217 {config, nil} ->
218 Repo.delete(config)
219
220 nil ->
221 err =
222 dgettext("errors", "Config with params %{params} not found", params: inspect(params))
223
224 {:error, err}
225 end
226 end
227
228 @spec from_binary(binary()) :: term()
229 def from_binary(binary), do: :erlang.binary_to_term(binary)
230
231 @spec from_binary_with_convert(binary()) :: any()
232 def from_binary_with_convert(binary) do
233 binary
234 |> from_binary()
235 |> do_convert()
236 end
237
238 @spec from_string(String.t()) :: atom() | no_return()
239 def from_string(string), do: do_transform_string(string)
240
241 @spec convert(any()) :: any()
242 def convert(entity), do: do_convert(entity)
243
244 defp do_convert(entity) when is_list(entity) do
245 for v <- entity, into: [], do: do_convert(v)
246 end
247
248 defp do_convert(%Regex{} = entity), do: inspect(entity)
249
250 defp do_convert(entity) when is_map(entity) do
251 for {k, v} <- entity, into: %{}, do: {do_convert(k), do_convert(v)}
252 end
253
254 defp do_convert({:proxy_url, {type, :localhost, port}}) do
255 %{"tuple" => [":proxy_url", %{"tuple" => [do_convert(type), "localhost", port]}]}
256 end
257
258 defp do_convert({:proxy_url, {type, host, port}}) when is_tuple(host) do
259 ip =
260 host
261 |> :inet_parse.ntoa()
262 |> to_string()
263
264 %{
265 "tuple" => [
266 ":proxy_url",
267 %{"tuple" => [do_convert(type), ip, port]}
268 ]
269 }
270 end
271
272 defp do_convert({:proxy_url, {type, host, port}}) do
273 %{
274 "tuple" => [
275 ":proxy_url",
276 %{"tuple" => [do_convert(type), to_string(host), port]}
277 ]
278 }
279 end
280
281 defp do_convert({:partial_chain, entity}), do: %{"tuple" => [":partial_chain", inspect(entity)]}
282
283 defp do_convert(entity) when is_tuple(entity) do
284 value =
285 entity
286 |> Tuple.to_list()
287 |> do_convert()
288
289 %{"tuple" => value}
290 end
291
292 defp do_convert(entity) when is_boolean(entity) or is_number(entity) or is_nil(entity) do
293 entity
294 end
295
296 defp do_convert(entity)
297 when is_atom(entity) and entity in [:"tlsv1.1", :"tlsv1.2", :"tlsv1.3"] do
298 ":#{entity}"
299 end
300
301 defp do_convert(entity) when is_atom(entity), do: inspect(entity)
302
303 defp do_convert(entity) when is_binary(entity), do: entity
304
305 @spec transform(any()) :: binary() | no_return()
306 def transform(entity) when is_binary(entity) or is_map(entity) or is_list(entity) do
307 entity
308 |> do_transform()
309 |> to_binary()
310 end
311
312 def transform(entity), do: to_binary(entity)
313
314 @spec transform_with_out_binary(any()) :: any()
315 def transform_with_out_binary(entity), do: do_transform(entity)
316
317 @spec to_binary(any()) :: binary()
318 def to_binary(entity), do: :erlang.term_to_binary(entity)
319
320 defp do_transform(%Regex{} = entity), do: entity
321
322 defp do_transform(%{"tuple" => [":proxy_url", %{"tuple" => [type, host, port]}]}) do
323 {:proxy_url, {do_transform_string(type), parse_host(host), port}}
324 end
325
326 defp do_transform(%{"tuple" => [":partial_chain", entity]}) do
327 {partial_chain, []} =
328 entity
329 |> String.replace(~r/[^\w|^{:,[|^,|^[|^\]^}|^\/|^\.|^"]^\s/, "")
330 |> Code.eval_string()
331
332 {:partial_chain, partial_chain}
333 end
334
335 defp do_transform(%{"tuple" => entity}) do
336 Enum.reduce(entity, {}, fn val, acc -> Tuple.append(acc, do_transform(val)) end)
337 end
338
339 defp do_transform(entity) when is_map(entity) do
340 for {k, v} <- entity, into: %{}, do: {do_transform(k), do_transform(v)}
341 end
342
343 defp do_transform(entity) when is_list(entity) do
344 for v <- entity, into: [], do: do_transform(v)
345 end
346
347 defp do_transform(entity) when is_binary(entity) do
348 entity
349 |> String.trim()
350 |> do_transform_string()
351 end
352
353 defp do_transform(entity), do: entity
354
355 defp parse_host("localhost"), do: :localhost
356
357 defp parse_host(host) do
358 charlist = to_charlist(host)
359
360 case :inet.parse_address(charlist) do
361 {:error, :einval} ->
362 charlist
363
364 {:ok, ip} ->
365 ip
366 end
367 end
368
369 defp find_valid_delimiter([], _string, _) do
370 raise(ArgumentError, message: "valid delimiter for Regex expression not found")
371 end
372
373 defp find_valid_delimiter([{leading, closing} = delimiter | others], pattern, regex_delimiter)
374 when is_tuple(delimiter) do
375 if String.contains?(pattern, closing) do
376 find_valid_delimiter(others, pattern, regex_delimiter)
377 else
378 {:ok, {leading, closing}}
379 end
380 end
381
382 defp find_valid_delimiter([delimiter | others], pattern, regex_delimiter) do
383 if String.contains?(pattern, delimiter) do
384 find_valid_delimiter(others, pattern, regex_delimiter)
385 else
386 {:ok, {delimiter, delimiter}}
387 end
388 end
389
390 defp do_transform_string("~r" <> _pattern = regex) do
391 with %{"modifier" => modifier, "pattern" => pattern, "delimiter" => regex_delimiter} <-
392 Regex.named_captures(@regex, regex),
393 {:ok, {leading, closing}} <- find_valid_delimiter(@delimiters, pattern, regex_delimiter),
394 {result, _} <- Code.eval_string("~r#{leading}#{pattern}#{closing}#{modifier}") do
395 result
396 end
397 end
398
399 defp do_transform_string(":" <> atom), do: String.to_atom(atom)
400
401 defp do_transform_string(value) do
402 if is_module_name?(value) do
403 String.to_existing_atom("Elixir." <> value)
404 else
405 value
406 end
407 end
408
409 @spec is_module_name?(String.t()) :: boolean()
410 def is_module_name?(string) do
411 Regex.match?(~r/^(Pleroma|Phoenix|Tesla|Quack|Ueberauth|Swoosh)\./, string) or
412 string in ["Oban", "Ueberauth", "ExSyslogger"]
413 end
414 end