2018-12-23 21:04:54 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 07:49:20 +01:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2018-12-23 21:04:54 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2018-11-06 16:00:48 +01:00
|
|
|
defmodule Pleroma.Config do
|
2020-12-16 17:51:48 +01:00
|
|
|
@behaviour Pleroma.Config.Getting
|
2018-11-06 19:34:57 +01:00
|
|
|
defmodule Error do
|
|
|
|
defexception [:message]
|
|
|
|
end
|
|
|
|
|
2020-12-16 17:51:48 +01:00
|
|
|
@impl true
|
2018-11-06 19:34:57 +01:00
|
|
|
def get(key), do: get(key, nil)
|
|
|
|
|
2020-12-16 17:51:48 +01:00
|
|
|
@impl true
|
2018-11-06 19:34:57 +01:00
|
|
|
def get([key], default), do: get(key, default)
|
2018-11-06 16:00:48 +01:00
|
|
|
|
2020-12-16 17:51:48 +01:00
|
|
|
@impl true
|
2020-08-05 12:00:49 +02:00
|
|
|
def get([_ | _] = path, default) do
|
|
|
|
case fetch(path) do
|
|
|
|
{:ok, value} -> value
|
|
|
|
:error -> default
|
|
|
|
end
|
2018-11-06 16:00:48 +01:00
|
|
|
end
|
|
|
|
|
2020-12-16 17:51:48 +01:00
|
|
|
@impl true
|
2018-11-06 19:34:57 +01:00
|
|
|
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
|
2018-11-06 16:00:48 +01:00
|
|
|
end
|
|
|
|
|
2020-08-05 17:23:12 +02:00
|
|
|
def fetch(key) when is_atom(key), do: fetch([key])
|
|
|
|
|
2020-08-05 12:00:49 +02:00
|
|
|
def fetch([root_key | keys]) do
|
|
|
|
Enum.reduce_while(keys, Application.fetch_env(:pleroma, root_key), fn
|
|
|
|
key, {:ok, config} when is_map(config) or is_list(config) ->
|
|
|
|
case Access.fetch(config, key) do
|
|
|
|
:error ->
|
|
|
|
{:halt, :error}
|
|
|
|
|
|
|
|
value ->
|
|
|
|
{:cont, value}
|
|
|
|
end
|
|
|
|
|
|
|
|
_key, _config ->
|
|
|
|
{:halt, :error}
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2018-11-06 16:00:48 +01:00
|
|
|
def put([key], value), do: put(key, value)
|
|
|
|
|
|
|
|
def put([parent_key | keys], value) do
|
|
|
|
parent =
|
2019-06-30 09:28:35 +02:00
|
|
|
Application.get_env(:pleroma, parent_key, [])
|
2018-11-06 16:00:48 +01:00
|
|
|
|> 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 18:19:22 +01:00
|
|
|
|
|
|
|
def delete([key]), do: delete(key)
|
|
|
|
|
2020-08-05 12:00:49 +02:00
|
|
|
def delete([parent_key | keys] = path) do
|
|
|
|
with {:ok, _} <- fetch(path) do
|
|
|
|
{_, parent} =
|
|
|
|
parent_key
|
|
|
|
|> get()
|
|
|
|
|> get_and_update_in(keys, fn _ -> :pop end)
|
2018-11-30 18:19:22 +01:00
|
|
|
|
2020-08-05 12:00:49 +02:00
|
|
|
Application.put_env(:pleroma, parent_key, parent)
|
|
|
|
end
|
2018-11-30 18:19:22 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
def delete(key) do
|
|
|
|
Application.delete_env(:pleroma, key)
|
|
|
|
end
|
2019-04-05 14:12:02 +02:00
|
|
|
|
2020-08-14 19:55:45 +02:00
|
|
|
def restrict_unauthenticated_access?(resource, kind) do
|
|
|
|
setting = get([:restrict_unauthenticated, resource, kind])
|
|
|
|
|
|
|
|
if setting in [nil, :if_instance_is_private] do
|
|
|
|
!get!([:instance, :public])
|
|
|
|
else
|
|
|
|
setting
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-13 20:07:38 +01:00
|
|
|
def improved_hashtag_timeline_path, do: [:instance, :improved_hashtag_timeline]
|
|
|
|
def improved_hashtag_timeline, do: get(improved_hashtag_timeline_path())
|
2020-12-27 22:08:09 +01:00
|
|
|
|
2019-04-05 14:12:02 +02:00
|
|
|
def oauth_consumer_strategies, do: get([:auth, :oauth_consumer_strategies], [])
|
|
|
|
|
|
|
|
def oauth_consumer_enabled?, do: oauth_consumer_strategies() != []
|
2019-12-05 22:25:44 +01:00
|
|
|
|
|
|
|
def enforce_oauth_admin_scope_usage?, do: !!get([:auth, :enforce_oauth_admin_scope_usage])
|
|
|
|
|
2019-12-11 09:42:02 +01:00
|
|
|
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
|
|
|
|
)
|
2019-12-05 22:25:44 +01:00
|
|
|
end
|
2018-11-06 16:00:48 +01:00
|
|
|
end
|