2019-09-08 10:22:52 +00:00
|
|
|
import strutils, sequtils, macros
|
2019-10-26 13:33:38 +00:00
|
|
|
import norm/sqlite
|
2019-08-13 17:44:29 +00:00
|
|
|
|
2019-10-26 13:33:38 +00:00
|
|
|
import prefs_impl, types
|
2019-08-17 19:49:41 +00:00
|
|
|
export genUpdatePrefs
|
2019-08-15 16:41:35 +00:00
|
|
|
|
2019-10-23 10:32:23 +00:00
|
|
|
template safeAddColumn(field: typedesc): untyped =
|
|
|
|
try: field.addColumn
|
|
|
|
except DbError: discard
|
|
|
|
|
2019-09-08 10:22:52 +00:00
|
|
|
dbFromTypes("prefs.db", "", "", "", [Prefs])
|
|
|
|
|
|
|
|
withDb:
|
2019-08-13 17:44:29 +00:00
|
|
|
try:
|
|
|
|
createTables()
|
|
|
|
except DbError:
|
|
|
|
discard
|
2020-03-08 23:12:42 +00:00
|
|
|
safeAddColumn Prefs.theme
|
|
|
|
safeAddColumn Prefs.hidePins
|
|
|
|
safeAddColumn Prefs.hideReplies
|
|
|
|
safeAddColumn Prefs.infiniteScroll
|
2020-03-29 07:03:06 +00:00
|
|
|
safeAddColumn Prefs.replaceInstagram
|
2019-08-13 17:44:29 +00:00
|
|
|
|
2019-10-23 12:06:47 +00:00
|
|
|
proc getDefaultPrefs(cfg: Config): Prefs =
|
2019-10-21 03:19:00 +00:00
|
|
|
result = genDefaultPrefs()
|
2019-10-23 12:06:47 +00:00
|
|
|
result.replaceTwitter = cfg.hostname
|
|
|
|
result.theme = cfg.defaultTheme
|
2019-10-21 03:19:00 +00:00
|
|
|
|
2019-08-13 17:44:29 +00:00
|
|
|
proc cache*(prefs: var Prefs) =
|
2019-09-08 10:22:52 +00:00
|
|
|
withDb:
|
2019-08-13 17:44:29 +00:00
|
|
|
try:
|
|
|
|
doAssert prefs.id != 0
|
|
|
|
discard Prefs.getOne("id = ?", prefs.id)
|
|
|
|
prefs.update()
|
|
|
|
except AssertionError, KeyError:
|
|
|
|
prefs.insert()
|
|
|
|
|
2019-10-23 12:06:47 +00:00
|
|
|
proc getPrefs*(id: string; cfg: Config): Prefs =
|
2019-10-21 03:19:00 +00:00
|
|
|
if id.len == 0:
|
2019-10-23 12:06:47 +00:00
|
|
|
return getDefaultPrefs(cfg)
|
2019-08-13 17:44:29 +00:00
|
|
|
|
2019-09-08 10:22:52 +00:00
|
|
|
withDb:
|
2019-08-13 17:44:29 +00:00
|
|
|
try:
|
|
|
|
result.getOne("id = ?", id)
|
2019-10-28 03:10:21 +00:00
|
|
|
if result.theme.len == 0:
|
|
|
|
result.theme = cfg.defaultTheme
|
2019-08-13 17:44:29 +00:00
|
|
|
except KeyError:
|
2019-10-23 12:06:47 +00:00
|
|
|
result = getDefaultPrefs(cfg)
|
2019-08-13 17:44:29 +00:00
|
|
|
|
2019-10-23 12:06:47 +00:00
|
|
|
proc resetPrefs*(prefs: var Prefs; cfg: Config) =
|
|
|
|
var defPrefs = getDefaultPrefs(cfg)
|
2019-08-15 17:13:54 +00:00
|
|
|
defPrefs.id = prefs.id
|
|
|
|
cache(defPrefs)
|
|
|
|
prefs = defPrefs
|