akkoma/lib/pleroma/config.ex

101 lines
2.5 KiB
Elixir
Raw Normal View History

# Pleroma: A lightweight social networking server
2020-03-03 22:44:49 +00:00
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Config do
defmodule Error do
defexception [:message]
end
def get(key), do: get(key, nil)
def get([key], default), do: get(key, default)
def get([root_key | keys], default) do
# This is to mimic Application.get_env/3 behaviour that returns `nil` if the
# actual value is `nil`.
Enum.reduce_while(keys, Application.get_env(:pleroma, root_key), fn key, config ->
case key do
[last_key] when is_map(config) ->
{:halt, Map.get(config, last_key, default)}
[last_key] when is_list(config) ->
{:halt, Keyword.get(config, last_key, default)}
_ ->
case config do
%{^key => value} ->
{:cont, value}
[_ | _] ->
case :lists.keyfind(key, 1, config) do
{_, value} -> {:cont, value}
_ -> {:halt, default}
end
_ ->
{:halt, default}
end
end
end)
end
def get(key, default) do
Application.get_env(:pleroma, key, default)
end
def get!(key) do
value = get(key, nil)
if value == nil do
raise(Error, message: "Missing configuration value: #{inspect(key)}")
else
value
end
end
def put([key], value), do: put(key, value)
def put([parent_key | keys], value) do
parent =
Application.get_env(:pleroma, parent_key, [])
|> put_in(keys, value)
Application.put_env(:pleroma, parent_key, parent)
end
def put(key, value) do
Application.put_env(:pleroma, key, value)
end
2018-11-30 17:19:22 +00:00
def delete([key]), do: delete(key)
def delete([parent_key | keys]) do
{_, parent} =
Application.get_env(:pleroma, parent_key)
|> get_and_update_in(keys, fn _ -> :pop end)
Application.put_env(:pleroma, parent_key, parent)
end
def delete(key) do
Application.delete_env(:pleroma, key)
end
def oauth_consumer_strategies, do: get([:auth, :oauth_consumer_strategies], [])
def oauth_consumer_enabled?, do: oauth_consumer_strategies() != []
def enforce_oauth_admin_scope_usage?, do: !!get([:auth, :enforce_oauth_admin_scope_usage])
def oauth_admin_scopes(scopes) when is_list(scopes) do
Enum.flat_map(
scopes,
fn scope ->
["admin:#{scope}"] ++
if enforce_oauth_admin_scope_usage?(), do: [], else: [scope]
end
)
end
end