From 3e661bab8a6bfaccdb6bccc707d1dd32a15f26e2 Mon Sep 17 00:00:00 2001 From: Zed Date: Wed, 23 Oct 2019 14:06:47 +0200 Subject: [PATCH] Add defaultTheme config option --- nitter.conf | 3 +++ src/config.nim | 4 +++- src/prefs.nim | 15 ++++++++------- src/routes/preferences.nim | 2 +- src/routes/router_utils.nim | 2 +- src/types.nim | 1 + src/views/general.nim | 2 +- 7 files changed, 18 insertions(+), 11 deletions(-) diff --git a/nitter.conf b/nitter.conf index 857b9f0..e957e68 100644 --- a/nitter.conf +++ b/nitter.conf @@ -9,3 +9,6 @@ hostname = "nitter.net" [Cache] directory = "./tmp" profileMinutes = 10 # how long to cache profiles + +[Config] +defaultTheme = "Dark" diff --git a/src/config.nim b/src/config.nim index ea62b1c..359d872 100644 --- a/src/config.nim +++ b/src/config.nim @@ -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") ) diff --git a/src/prefs.nim b/src/prefs.nim index 7656f08..8abba26 100644 --- a/src/prefs.nim +++ b/src/prefs.nim @@ -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 diff --git a/src/routes/preferences.nim b/src/routes/preferences.nim index 233b94c..96cd51e 100644 --- a/src/routes/preferences.nim +++ b/src/routes/preferences.nim @@ -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))) diff --git a/src/routes/router_utils.nim b/src/routes/router_utils.nim index 44ffba8..2aedd6c 100644 --- a/src/routes/router_utils.nim +++ b/src/routes/router_utils.nim @@ -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)) diff --git a/src/types.nim b/src/types.nim index d4965fd..c0d9492 100644 --- a/src/types.nim +++ b/src/types.nim @@ -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) diff --git a/src/views/general.nim b/src/views/general.nim index e0433aa..49f911a 100644 --- a/src/views/general.nim +++ b/src/views/general.nim @@ -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: