Add defaultTheme config option

This commit is contained in:
Zed 2019-10-23 14:06:47 +02:00
parent b018dbdf27
commit 3e661bab8a
7 changed files with 18 additions and 11 deletions

View file

@ -9,3 +9,6 @@ hostname = "nitter.net"
[Cache]
directory = "./tmp"
profileMinutes = 10 # how long to cache profiles
[Config]
defaultTheme = "Dark"

View file

@ -21,5 +21,7 @@ proc getConfig*(path: string): Config =
hostname: cfg.get("Server", "hostname", "nitter.net"),
cacheDir: cfg.get("Cache", "directory", "/tmp/nitter"),
profileCacheTime: cfg.get("Cache", "profileMinutes", 10)
profileCacheTime: cfg.get("Cache", "profileMinutes", 10),
defaultTheme: cfg.get("Config", "defaultTheme", "Dark")
)

View file

@ -27,9 +27,10 @@ withDb:
discard
Prefs.theme.safeAddColumn
proc getDefaultPrefs(hostname: string): Prefs =
proc getDefaultPrefs(cfg: Config): Prefs =
result = genDefaultPrefs()
result.replaceTwitter = hostname
result.replaceTwitter = cfg.hostname
result.theme = cfg.defaultTheme
proc cache*(prefs: var Prefs) =
withDb:
@ -40,18 +41,18 @@ proc cache*(prefs: var Prefs) =
except AssertionError, KeyError:
prefs.insert()
proc getPrefs*(id, hostname: string): Prefs =
proc getPrefs*(id: string; cfg: Config): Prefs =
if id.len == 0:
return getDefaultPrefs(hostname)
return getDefaultPrefs(cfg)
withDb:
try:
result.getOne("id = ?", id)
except KeyError:
result = getDefaultPrefs(hostname)
result = getDefaultPrefs(cfg)
proc resetPrefs*(prefs: var Prefs; hostname: string) =
var defPrefs = getDefaultPrefs(hostname)
proc resetPrefs*(prefs: var Prefs; cfg: Config) =
var defPrefs = getDefaultPrefs(cfg)
defPrefs.id = prefs.id
cache(defPrefs)
prefs = defPrefs

View file

@ -33,7 +33,7 @@ proc createPrefRouter*(cfg: Config) =
post "/resetprefs":
var prefs = cookiePrefs()
resetPrefs(prefs, cfg.hostname)
resetPrefs(prefs, cfg)
savePrefs()
redirect($(parseUri("/settings") ? filterParams(request.params)))

View file

@ -2,7 +2,7 @@ import ../utils, ../prefs
export utils, prefs
template cookiePrefs*(): untyped {.dirty.} =
getPrefs(request.cookies.getOrDefault("preferences"), cfg.hostname)
getPrefs(request.cookies.getOrDefault("preferences"), cfg)
template getPath*(): untyped {.dirty.} =
$(parseUri(request.path) ? filterParams(request.params))

View file

@ -177,6 +177,7 @@ type
hostname*: string
cacheDir*: string
profileCacheTime*: int
defaultTheme*: string
proc contains*(thread: Chain; tweet: Tweet): bool =
thread.content.anyIt(it.id == tweet.id)

View file

@ -29,7 +29,7 @@ proc renderNavbar*(title, rss: string; req: Request): VNode =
proc renderMain*(body: VNode; req: Request; cfg: Config; titleText=""; desc="";
rss=""; `type`="article"; video=""; images: seq[string] = @[]): string =
let prefs = getPrefs(req.cookies.getOrDefault("preferences"), cfg.hostname)
let prefs = getPrefs(req.cookies.getOrDefault("preferences"), cfg)
let theme = "/css/themes/" & toLowerAscii(prefs.theme) & ".css"
let node = buildHtml(html(lang="en")):
head: