nitter/src/routes/preferences.nim

52 lines
1.2 KiB
Nim
Raw Normal View History

2021-12-27 02:37:38 +01:00
# SPDX-License-Identifier: AGPL-3.0-only
import strutils, uri, os, re, algorithm
2019-09-06 02:42:35 +02:00
import jester
import router_utils
2019-09-21 01:08:30 +02:00
import ".."/[types]
2019-09-06 02:42:35 +02:00
import ../views/[general, preferences]
export preferences
let reTitleize = re"(?<![A-z])([a-z])"
proc titleize(str: string): string =
result = str
var idx = 0
while idx != -1:
idx = str.find(reTitleize, start = idx)
if idx != -1:
result[idx] = str[idx].toUpperAscii
idx.inc
2019-10-23 11:48:08 +02:00
proc findThemes*(dir: string): seq[string] =
for kind, path in walkDir(dir / "css" / "themes"):
2019-10-27 11:24:09 +01:00
let theme = path.splitFile.name
result.add theme.replace("_", " ").titleize
2019-10-23 12:46:52 +02:00
sort(result)
2019-10-23 11:48:08 +02:00
2019-09-06 02:42:35 +02:00
proc createPrefRouter*(cfg: Config) =
router preferences:
get "/settings":
2020-06-09 16:45:21 +02:00
let
prefs = cookiePrefs()
html = renderPreferences(prefs, refPath(), findThemes(cfg.staticDir))
resp renderMain(html, request, cfg, prefs, "Preferences")
2019-09-06 02:42:35 +02:00
get "/settings/@i?":
redirect("/settings")
2019-09-06 02:42:35 +02:00
post "/saveprefs":
genUpdatePrefs()
redirect(refPath())
post "/resetprefs":
genResetPrefs()
redirect("/settings?referer=" & encodeUrl(refPath()))
2019-09-06 02:42:35 +02:00
post "/enablehls":
savePref("hlsPlayback", "on", request)
2019-09-06 02:42:35 +02:00
redirect(refPath())