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] [Cache]
directory = "./tmp" directory = "./tmp"
profileMinutes = 10 # how long to cache profiles 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"), hostname: cfg.get("Server", "hostname", "nitter.net"),
cacheDir: cfg.get("Cache", "directory", "/tmp/nitter"), 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 discard
Prefs.theme.safeAddColumn Prefs.theme.safeAddColumn
proc getDefaultPrefs(hostname: string): Prefs = proc getDefaultPrefs(cfg: Config): Prefs =
result = genDefaultPrefs() result = genDefaultPrefs()
result.replaceTwitter = hostname result.replaceTwitter = cfg.hostname
result.theme = cfg.defaultTheme
proc cache*(prefs: var Prefs) = proc cache*(prefs: var Prefs) =
withDb: withDb:
@ -40,18 +41,18 @@ proc cache*(prefs: var Prefs) =
except AssertionError, KeyError: except AssertionError, KeyError:
prefs.insert() prefs.insert()
proc getPrefs*(id, hostname: string): Prefs = proc getPrefs*(id: string; cfg: Config): Prefs =
if id.len == 0: if id.len == 0:
return getDefaultPrefs(hostname) return getDefaultPrefs(cfg)
withDb: withDb:
try: try:
result.getOne("id = ?", id) result.getOne("id = ?", id)
except KeyError: except KeyError:
result = getDefaultPrefs(hostname) result = getDefaultPrefs(cfg)
proc resetPrefs*(prefs: var Prefs; hostname: string) = proc resetPrefs*(prefs: var Prefs; cfg: Config) =
var defPrefs = getDefaultPrefs(hostname) var defPrefs = getDefaultPrefs(cfg)
defPrefs.id = prefs.id defPrefs.id = prefs.id
cache(defPrefs) cache(defPrefs)
prefs = defPrefs prefs = defPrefs

View File

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

View File

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

View File

@ -177,6 +177,7 @@ type
hostname*: string hostname*: string
cacheDir*: string cacheDir*: string
profileCacheTime*: int profileCacheTime*: int
defaultTheme*: string
proc contains*(thread: Chain; tweet: Tweet): bool = proc contains*(thread: Chain; tweet: Tweet): bool =
thread.content.anyIt(it.id == tweet.id) 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=""; proc renderMain*(body: VNode; req: Request; cfg: Config; titleText=""; desc="";
rss=""; `type`="article"; video=""; images: seq[string] = @[]): string = 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 theme = "/css/themes/" & toLowerAscii(prefs.theme) & ".css"
let node = buildHtml(html(lang="en")): let node = buildHtml(html(lang="en")):
head: head: