-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathconfig_manager.js
111 lines (98 loc) · 4.08 KB
/
config_manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
const SettingTypes = {
ARRAY: 'ARRAY',
STRING: 'STRING',
INTEGER: 'INTEGER',
BOOLEAN: 'BOOLEAN'
}
// TODO: make more configuration settings modifiable without restart
const keys = [
{ name: 'allowlist', type: SettingTypes.ARRAY },
{ name: 'alwaysAutoquiz', type: SettingTypes.BOOLEAN },
{ name: 'autoquizHintTimeout', type: SettingTypes.INTEGER },
{ name: 'autoquizSkipTimeout', type: SettingTypes.INTEGER },
{ name: 'bitrate', type: SettingTypes.INTEGER },
{ name: 'blocklist', type: SettingTypes.ARRAY },
{ name: 'enableAutoquiz', type: SettingTypes.BOOLEAN },
{ name: 'enableExclusiveTokens', type: SettingTypes.BOOLEAN },
{ name: 'forumHoldTimeoutSeconds', type: SettingTypes.INTEGER },
{ name: 'maxConcurrentRiddles', type: SettingTypes.INTEGER },
{ name: 'nagTimeoutInSeconds', type: SettingTypes.INTEGER },
{ name: 'ranks', type: SettingTypes.ARRAY },
{ name: 'recitalUpperBoundId', type: SettingTypes.STRING },
{ name: 'recitalLowerBoundId', type: SettingTypes.STRING },
{ name: 'rejectedRiddleAction', type: SettingTypes.STRING, values: ['reject', 'ignore'] },
{ name: 'riddleAcceptancePolicy', type: SettingTypes.STRING, values: ['blocklist', 'allowlist'] },
{ name: 'quizSuccessTimeout', type: SettingTypes.INTEGER },
{ name: 'skipTimeoutInSeconds', type: SettingTypes.INTEGER }
]
class ConfigManager {
constructor (repository, id) {
this._repository = repository
this._id = id
}
async loadFromRepository () {
this._cache = await this._repository.get(this._id)
return this._cache != null
}
get (key) {
return this._cache[key]
}
add (key, value) {
const setting = keys.find(k => k.name === key)
if (setting == null) {
throw new Error(`Cannot find configuration key \`${key}\`.`)
}
if (setting.type !== SettingTypes.ARRAY) {
throw new Error(`Cannot add to configuration key \`${key}\` because it does not describe a setting of type ARRAY.`)
}
this._repository.addToSet(this._id, key, value).then(result => { this._cache = result })
}
remove (key, value) {
const setting = keys.find(k => k.name === key)
if (setting == null) {
throw new Error(`Cannot find configuration key \`${key}\`.`)
}
if (setting.type !== SettingTypes.ARRAY) {
throw new Error(`Cannot remove from configuration key \`${key}\` because it does not describe a setting of type ARRAY.`)
}
this._repository.removeFromSet(this._id, key, value).then(result => { this._cache = result })
}
set (key, value) {
const setting = keys.find(k => k.name === key)
if (setting == null) {
throw new Error(`Cannot find configuration key \`${key}\`.`)
}
if (setting.type === SettingTypes.ARRAY) {
throw new Error(`Cannot set configuration key \`${key}\` because it describes a setting of type ARRAY.`)
}
if (setting.type === SettingTypes.INTEGER) {
value = parseInt(value)
if (!Number.isInteger(value)) {
throw new Error(`Cannot set configuration key \`${key}\` to \`${value}\`. The value must be of type INTEGER.`)
}
} else if (setting.type === SettingTypes.BOOLEAN) {
if (value === 'true') {
value = true
} else if (value === 'false') {
value = false
} else {
throw new Error(`Cannot set configuration key \`${key}\` to \`${value}\`. The value must be of type BOOLEAN.`)
}
}
if (setting.values != null && !setting.values.includes(value)) {
throw new Error(`Cannot set configuration key \`${key}\` to \`${value}\`. Possible values are: \`${setting.values}\``)
}
this._repository.setField(this._id, key, value).then(result => { this._cache = result })
}
unset (key) {
const setting = keys.find(k => k.name === key)
if (setting == null) {
throw new Error(`Cannot find configuration key \`${key}\`.`)
}
if (setting.type === SettingTypes.ARRAY) {
throw new Error(`Cannot set configuration key \`${key}\` because it describes a setting of type ARRAY.`)
}
this._repository.unsetField(this._id, key).then(result => { this._cache = result })
}
}
module.exports = ConfigManager