config.ex 1.86 KB
Newer Older
1
# Pleroma: A lightweight social networking server
feld's avatar
feld committed
2
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3
4
# SPDX-License-Identifier: AGPL-3.0-only

5
defmodule Pleroma.Config do
href's avatar
href committed
6
7
8
9
10
11
12
  defmodule Error do
    defexception [:message]
  end

  def get(key), do: get(key, nil)

  def get([key], default), do: get(key, default)
13

href's avatar
href committed
14
  def get([parent_key | keys], default) do
15
16
17
18
19
20
    case :pleroma
         |> Application.get_env(parent_key)
         |> get_in(keys) do
      nil -> default
      any -> any
    end
21
22
  end

href's avatar
href committed
23
24
25
26
27
28
29
30
31
32
33
34
  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
35
36
37
38
39
40
  end

  def put([key], value), do: put(key, value)

  def put([parent_key | keys], value) do
    parent =
41
      Application.get_env(:pleroma, parent_key, [])
42
43
44
45
46
47
48
49
      |> put_in(keys, value)

    Application.put_env(:pleroma, parent_key, parent)
  end

  def put(key, value) do
    Application.put_env(:pleroma, key, value)
  end
href's avatar
href committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63

  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
64
65
66
67

  def oauth_consumer_strategies, do: get([:auth, :oauth_consumer_strategies], [])

  def oauth_consumer_enabled?, do: oauth_consumer_strategies() != []
68
69
70

  def enforce_oauth_admin_scope_usage?, do: !!get([:auth, :enforce_oauth_admin_scope_usage])

71
72
73
74
75
76
77
78
  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
    )
79
  end
80
end