aboutsummaryrefslogtreecommitdiff
path: root/persistence
diff options
context:
space:
mode:
authorBohdan Horbeshko <bodqhrohro@gmail.com>2022-01-06 00:04:22 +0300
committerBohdan Horbeshko <bodqhrohro@gmail.com>2022-01-06 00:04:22 +0300
commit077edae986f2229d263f978b7cf35ed88254c042 (patch)
tree01877034c1989565e2de30bce04d1c15eb06579e /persistence
parentd48cb8b58682d3b9c5798913de35ae62dd20dd38 (diff)
Add keeponline option
Diffstat (limited to 'persistence')
-rw-r--r--persistence/sessions.go40
1 files changed, 37 insertions, 3 deletions
diff --git a/persistence/sessions.go b/persistence/sessions.go
index fd520d1..b42d3a3 100644
--- a/persistence/sessions.go
+++ b/persistence/sessions.go
@@ -34,8 +34,14 @@ type SessionsMap struct {
// Session is a key-values subtree
type Session struct {
- Login string `yaml:":login"`
- Timezone string `yaml:":timezone"`
+ Login string `yaml:":login"`
+ Timezone string `yaml:":timezone"`
+ KeepOnline bool `yaml:":keeponline"`
+}
+
+var configKeys = []string{
+ "timezone",
+ "keeponline",
}
var sessionDB *SessionsYamlDB
@@ -102,6 +108,8 @@ func (s *Session) Get(key string) (string, error) {
switch key {
case "timezone":
return s.Timezone, nil
+ case "keeponline":
+ return fromBool(s.KeepOnline), nil
}
return "", errors.New("Unknown session property")
@@ -110,7 +118,7 @@ func (s *Session) Get(key string) (string, error) {
// ToMap converts the session to a map
func (s *Session) ToMap() map[string]string {
m := make(map[string]string)
- for _, configKey := range []string{"timezone"} {
+ for _, configKey := range configKeys {
value, _ := s.Get(configKey)
m[configKey] = value
}
@@ -124,6 +132,13 @@ func (s *Session) Set(key string, value string) (string, error) {
case "timezone":
s.Timezone = value
return value, nil
+ case "keeponline":
+ b, err := toBool(value)
+ if err != nil {
+ return "", err
+ }
+ s.KeepOnline = b
+ return value, nil
}
return "", errors.New("Unknown session property")
@@ -139,3 +154,22 @@ func (s *Session) TimezoneToLocation() *time.Location {
// default
return zeroLocation
}
+
+func fromBool(b bool) string {
+ if b {
+ return "true"
+ } else {
+ return "false"
+ }
+}
+
+func toBool(s string) (bool, error) {
+ switch s {
+ case "true":
+ return true, nil
+ case "false":
+ return false, nil
+ }
+
+ return false, errors.New("Invalid boolean value")
+}