1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Config do
7 defexception [:message]
10 def get(key), do: get(key, nil)
12 def get([key], default), do: get(key, default)
14 def get([root_key | keys], default) do
15 # This is to mimic Application.get_env/3 behaviour that returns `nil` if the
16 # actual value is `nil`.
17 Enum.reduce_while(keys, Application.get_env(:pleroma, root_key), fn key, config ->
19 [last_key] when is_map(config) ->
20 {:halt, Map.get(config, last_key, default)}
22 [last_key] when is_list(config) ->
23 {:halt, Keyword.get(config, last_key, default)}
31 case :lists.keyfind(key, 1, config) do
32 {_, value} -> {:cont, value}
43 def get(key, default) do
44 Application.get_env(:pleroma, key, default)
51 raise(Error, message: "Missing configuration value: #{inspect(key)}")
57 def put([key], value), do: put(key, value)
59 def put([parent_key | keys], value) do
61 Application.get_env(:pleroma, parent_key, [])
62 |> put_in(keys, value)
64 Application.put_env(:pleroma, parent_key, parent)
67 def put(key, value) do
68 Application.put_env(:pleroma, key, value)
71 def delete([key]), do: delete(key)
73 def delete([parent_key | keys]) do
75 Application.get_env(:pleroma, parent_key)
76 |> get_and_update_in(keys, fn _ -> :pop end)
78 Application.put_env(:pleroma, parent_key, parent)
82 Application.delete_env(:pleroma, key)
85 def oauth_consumer_strategies, do: get([:auth, :oauth_consumer_strategies], [])
87 def oauth_consumer_enabled?, do: oauth_consumer_strategies() != []
89 def enforce_oauth_admin_scope_usage?, do: !!get([:auth, :enforce_oauth_admin_scope_usage])
91 def oauth_admin_scopes(scopes) when is_list(scopes) do
96 if enforce_oauth_admin_scope_usage?(), do: [], else: [scope]