Merge branch 'develop' into issue/1023
[akkoma] / lib / pleroma / config.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 do
6 defmodule Error do
7 defexception [:message]
8 end
9
10 def get(key), do: get(key, nil)
11
12 def get([key], default), do: get(key, default)
13
14 def get([parent_key | keys], default) do
15 case :pleroma
16 |> Application.get_env(parent_key)
17 |> get_in(keys) do
18 nil -> default
19 any -> any
20 end
21 end
22
23 def get(key, default) do
24 Application.get_env(:pleroma, key, default)
25 end
26
27 def get!(key) do
28 value = get(key, nil)
29
30 if value == nil do
31 raise(Error, message: "Missing configuration value: #{inspect(key)}")
32 else
33 value
34 end
35 end
36
37 def put([key], value), do: put(key, value)
38
39 def put([parent_key | keys], value) do
40 parent =
41 Application.get_env(:pleroma, parent_key, [])
42 |> put_in(keys, value)
43
44 Application.put_env(:pleroma, parent_key, parent)
45 end
46
47 def put(key, value) do
48 Application.put_env(:pleroma, key, value)
49 end
50
51 def delete([key]), do: delete(key)
52
53 def delete([parent_key | keys]) do
54 {_, parent} =
55 Application.get_env(:pleroma, parent_key)
56 |> get_and_update_in(keys, fn _ -> :pop end)
57
58 Application.put_env(:pleroma, parent_key, parent)
59 end
60
61 def delete(key) do
62 Application.delete_env(:pleroma, key)
63 end
64
65 def oauth_consumer_strategies, do: get([:auth, :oauth_consumer_strategies], [])
66
67 def oauth_consumer_enabled?, do: oauth_consumer_strategies() != []
68
69 def enforce_oauth_admin_scope_usage?, do: !!get([:auth, :enforce_oauth_admin_scope_usage])
70
71 def oauth_admin_scopes(scopes) when is_list(scopes) do
72 Enum.flat_map(
73 scopes,
74 fn scope ->
75 ["admin:#{scope}"] ++
76 if enforce_oauth_admin_scope_usage?(), do: [], else: [scope]
77 end
78 )
79 end
80 end