2019-09-08 12:22:52 +02:00
|
|
|
import strutils, sequtils, macros
|
|
|
|
import prefs_impl, types
|
2019-08-13 19:44:29 +02:00
|
|
|
|
2019-08-17 21:49:41 +02:00
|
|
|
export genUpdatePrefs
|
2019-08-15 18:41:35 +02:00
|
|
|
|
2019-08-18 01:26:38 +02:00
|
|
|
static:
|
|
|
|
var pFields: seq[string]
|
|
|
|
for id in getTypeImpl(Prefs)[2]:
|
|
|
|
if $id[0] == "id": continue
|
|
|
|
pFields.add $id[0]
|
|
|
|
|
|
|
|
let pDefs = toSeq(allPrefs()).mapIt(it.name)
|
|
|
|
let missing = pDefs.filterIt(it notin pFields)
|
|
|
|
if missing.len > 0:
|
|
|
|
raiseAssert("{$1} missing from the Prefs type" % missing.join(", "))
|
|
|
|
|
2019-09-08 12:22:52 +02:00
|
|
|
dbFromTypes("prefs.db", "", "", "", [Prefs])
|
|
|
|
|
|
|
|
withDb:
|
2019-08-13 19:44:29 +02:00
|
|
|
try:
|
|
|
|
createTables()
|
|
|
|
except DbError:
|
|
|
|
discard
|
|
|
|
|
2019-10-21 05:19:00 +02:00
|
|
|
proc getDefaultPrefs(hostname: string): Prefs =
|
|
|
|
result = genDefaultPrefs()
|
|
|
|
result.replaceTwitter = hostname
|
|
|
|
|
2019-08-13 19:44:29 +02:00
|
|
|
proc cache*(prefs: var Prefs) =
|
2019-09-08 12:22:52 +02:00
|
|
|
withDb:
|
2019-08-13 19:44:29 +02:00
|
|
|
try:
|
|
|
|
doAssert prefs.id != 0
|
|
|
|
discard Prefs.getOne("id = ?", prefs.id)
|
|
|
|
prefs.update()
|
|
|
|
except AssertionError, KeyError:
|
|
|
|
prefs.insert()
|
|
|
|
|
2019-10-21 05:19:00 +02:00
|
|
|
proc getPrefs*(id, hostname: string): Prefs =
|
|
|
|
if id.len == 0:
|
|
|
|
return getDefaultPrefs(hostname)
|
2019-08-13 19:44:29 +02:00
|
|
|
|
2019-09-08 12:22:52 +02:00
|
|
|
withDb:
|
2019-08-13 19:44:29 +02:00
|
|
|
try:
|
|
|
|
result.getOne("id = ?", id)
|
|
|
|
except KeyError:
|
2019-10-21 05:19:00 +02:00
|
|
|
result = getDefaultPrefs(hostname)
|
2019-08-13 19:44:29 +02:00
|
|
|
|
2019-10-21 05:19:00 +02:00
|
|
|
proc resetPrefs*(prefs: var Prefs; hostname: string) =
|
|
|
|
var defPrefs = getDefaultPrefs(hostname)
|
2019-08-15 19:13:54 +02:00
|
|
|
defPrefs.id = prefs.id
|
|
|
|
cache(defPrefs)
|
|
|
|
prefs = defPrefs
|