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([_ | _] = path, default) do
21 def get(key, default) do
22 Application.get_env(:pleroma, key, default)
29 raise(Error, message: "Missing configuration value: #{inspect(key)}")
35 def fetch([root_key | keys]) do
36 Enum.reduce_while(keys, Application.fetch_env(:pleroma, root_key), fn
37 key, {:ok, config} when is_map(config) or is_list(config) ->
38 case Access.fetch(config, key) do
51 def put([key], value), do: put(key, value)
53 def put([parent_key | keys], value) do
55 Application.get_env(:pleroma, parent_key, [])
56 |> put_in(keys, value)
58 Application.put_env(:pleroma, parent_key, parent)
61 def put(key, value) do
62 Application.put_env(:pleroma, key, value)
65 def delete([key]), do: delete(key)
67 def delete([parent_key | keys] = path) do
68 with {:ok, _} <- fetch(path) do
72 |> get_and_update_in(keys, fn _ -> :pop end)
74 Application.put_env(:pleroma, parent_key, parent)
79 Application.delete_env(:pleroma, key)
82 def oauth_consumer_strategies, do: get([:auth, :oauth_consumer_strategies], [])
84 def oauth_consumer_enabled?, do: oauth_consumer_strategies() != []
86 def enforce_oauth_admin_scope_usage?, do: !!get([:auth, :enforce_oauth_admin_scope_usage])
88 def oauth_admin_scopes(scopes) when is_list(scopes) do
93 if enforce_oauth_admin_scope_usage?(), do: [], else: [scope]