-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsettings.osl
More file actions
48 lines (46 loc) · 1.31 KB
/
Copy pathsettings.osl
File metadata and controls
48 lines (46 loc) · 1.31 KB
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
def settingsPath(string username) string (
return userSettingsDir ++ normalizeUsername(username) ++ ".json"
)
def handleGetMySettings(*serve.Context c) (
string username = c.getString("username")
string key = normalizeUsername(username)
if !isSafePathPart(key) (
c.badRequest("invalid username")
return
)
auto result = readFile(settingsPath(username))
if !result.ok (
c.json(200, {ok: true, settings: {}})
return
)
c.json(200, {ok: true, settings: result.data})
)
def handlePutMySettings(*serve.Context c) (
string username = c.getString("username")
string key = normalizeUsername(username)
if !isSafePathPart(key) (
c.badRequest("invalid username")
return
)
string body = c.body()
if body.len > 131072 (
c.badRequest("settings too large")
return
)
auto parsed = json.parse(body, {type: "object"})
if parsed.isErr() (
c.badRequest("settings must be a JSON object")
return
)
object settings = parsed.unwrap().assert(object)
if !settings.contains("settings") or settings.settings == null (
settings.settings = {}
)
object inner = settings.settings
inner.updatedAt = timestamp
if !writeFile(settingsPath(username), settings) (
c.internalError("failed to save settings")
return
)
c.json(200, {ok: true, updatedAt: inner.updatedAt})
)