Merge branch 'hide-blocked-from-interaction' into 'develop'
[akkoma] / lib / pleroma / config / transfer_task.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Config.TransferTask do
6 use Task
7 alias Pleroma.Web.AdminAPI.Config
8
9 def start_link do
10 load_and_update_env()
11 if Pleroma.Config.get(:env) == :test, do: Ecto.Adapters.SQL.Sandbox.checkin(Pleroma.Repo)
12 :ignore
13 end
14
15 def load_and_update_env do
16 if Pleroma.Config.get([:instance, :dynamic_configuration]) and
17 Ecto.Adapters.SQL.table_exists?(Pleroma.Repo, "config") do
18 for_restart =
19 Pleroma.Repo.all(Config)
20 |> Enum.map(&update_env(&1))
21
22 # We need to restart applications for loaded settings take effect
23 for_restart
24 |> Enum.reject(&(&1 in [:pleroma, :ok]))
25 |> Enum.each(fn app ->
26 Application.stop(app)
27 :ok = Application.start(app)
28 end)
29 end
30 end
31
32 defp update_env(setting) do
33 try do
34 key =
35 if String.starts_with?(setting.key, "Pleroma.") do
36 "Elixir." <> setting.key
37 else
38 String.trim_leading(setting.key, ":")
39 end
40
41 group = String.to_existing_atom(setting.group)
42
43 Application.put_env(
44 group,
45 String.to_existing_atom(key),
46 Config.from_binary(setting.value)
47 )
48
49 group
50 rescue
51 e ->
52 require Logger
53
54 Logger.warn(
55 "updating env causes error, key: #{inspect(setting.key)}, error: #{inspect(e)}"
56 )
57 end
58 end
59 end