nitter/src/prefs.nim

126 lines
3.3 KiB
Nim
Raw Normal View History

import asyncdispatch, times, macros, tables, xmltree
2019-08-13 17:44:29 +00:00
import types
const hostname {.strdefine.} = "nitter.net"
2019-08-13 17:44:29 +00:00
withCustomDb("prefs.db", "", "", ""):
try:
createTables()
except DbError:
discard
type
PrefKind* = enum
checkbox, select, input
Pref* = object
name*: string
label*: string
case kind*: PrefKind
of checkbox:
defaultState*: bool
of select:
defaultOption*: string
options*: seq[string]
of input:
defaultInput*: string
placeholder*: string
# TODO: write DSL to simplify this
2019-08-13 17:44:29 +00:00
const prefList*: Table[string, seq[Pref]] = {
"Privacy": @[
Pref(kind: input, name: "replaceTwitter",
label: "Replace Twitter links with Nitter (blank to disable)",
defaultInput: hostname, placeholder: "Nitter hostname"),
Pref(kind: input, name: "replaceYouTube",
label: "Replace YouTube links with Invidious (blank to disable)",
defaultInput: "invidio.us", placeholder: "Invidious hostname")
],
2019-08-13 17:44:29 +00:00
"Media": @[
Pref(kind: checkbox, name: "videoPlayback",
label: "Enable hls.js video playback (requires JavaScript)",
defaultState: false),
Pref(kind: checkbox, name: "autoplayGifs", label: "Autoplay gifs",
2019-08-15 16:45:56 +00:00
defaultState: true)
2019-08-13 19:25:29 +00:00
],
"Display": @[
Pref(kind: checkbox, name: "hideTweetStats",
2019-08-15 16:25:47 +00:00
label: "Hide tweet stats (replies, retweets, likes)",
2019-08-13 19:25:29 +00:00
defaultState: false),
Pref(kind: checkbox, name: "hideBanner", label: "Hide profile banner",
defaultState: false),
Pref(kind: checkbox, name: "stickyProfile",
label: "Make profile sidebar stick to top",
defaultState: true)
2019-08-13 17:44:29 +00:00
]
}.toTable
iterator allPrefs(): Pref =
for k, v in prefList:
for pref in v:
yield pref
macro genDefaultPrefs*(): untyped =
result = nnkObjConstr.newTree(ident("Prefs"))
for pref in allPrefs():
result.add nnkExprColonExpr.newTree(
ident(pref.name),
case pref.kind
of checkbox: newLit(pref.defaultState)
of select: newLit(pref.defaultOption)
of input: newLit(pref.defaultInput))
proc cache*(prefs: var Prefs) =
withCustomDb("prefs.db", "", "", ""):
try:
doAssert prefs.id != 0
discard Prefs.getOne("id = ?", prefs.id)
prefs.update()
except AssertionError, KeyError:
prefs.insert()
proc getPrefs*(id: string): Prefs =
if id.len == 0: return genDefaultPrefs()
withCustomDb("prefs.db", "", "", ""):
try:
result.getOne("id = ?", id)
except KeyError:
result = genDefaultPrefs()
cache(result)
2019-08-15 17:13:54 +00:00
proc resetPrefs*(prefs: var Prefs) =
var defPrefs = genDefaultPrefs()
defPrefs.id = prefs.id
cache(defPrefs)
prefs = defPrefs
2019-08-13 17:44:29 +00:00
macro genUpdatePrefs*(): untyped =
result = nnkStmtList.newTree()
for pref in allPrefs():
let ident = ident(pref.name)
let value = nnkPrefix.newTree(ident("@"), newLit(pref.name))
case pref.kind
of checkbox:
2019-08-15 12:38:14 +00:00
result.add quote do: prefs.`ident` = `value` == "on"
of input:
result.add quote do: prefs.`ident` = xmltree.escape(strip(`value`))
2019-08-13 17:44:29 +00:00
of select:
let options = pref.options
let default = pref.defaultOption
result.add quote do:
if `value` in `options`: prefs.`ident` = `value`
else: prefs.`ident` = `default`
result.add quote do:
cache(prefs)