From bbafb1bc6da325f5edbf15d99a59ed16f05a3298 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Wed, 8 Jul 2020 13:25:41 +0200 Subject: [PATCH] Store values in session, and automatically save changes --- api/handlers.go | 4 ++++ model/session.go | 41 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/api/handlers.go b/api/handlers.go index 8104782..7c434da 100644 --- a/api/handlers.go +++ b/api/handlers.go @@ -214,6 +214,10 @@ func apiAuthHandler(f func(*config.Options, *RequestResources, io.Reader) Respon User: user, } f(opts, req, r.Body).WriteResponse(w) + + if session.HasChanged() { + storage.MainStore.UpdateSession(session) + } } } } diff --git a/model/session.go b/model/session.go index 5c55064..cb30966 100644 --- a/model/session.go +++ b/model/session.go @@ -37,9 +37,11 @@ import ( ) type Session struct { - Id []byte `json:"id"` - IdUser int64 `json:"login"` - Time time.Time `json:"time"` + Id []byte `json:"id"` + IdUser int64 `json:"login"` + Time time.Time `json:"time"` + Content map[string]interface{} `json:"content,omitempty"` + changed bool } func NewSession(user *User) (s *Session, err error) { @@ -55,3 +57,36 @@ func NewSession(user *User) (s *Session, err error) { return } + +func (s *Session) HasChanged() bool { + return s.changed +} + +func (s *Session) SetValue(key string, value interface{}) { + if s.Content == nil && value != nil { + s.Content = map[string]interface{}{} + } + + if value == nil { + if s.Content == nil { + return + } else if _, ok := s.Content[key]; !ok { + return + } else { + delete(s.Content, key) + s.changed = true + } + } else { + s.Content[key] = value + s.changed = true + } +} + +func (s *Session) GetValue(key string, value interface{}) (ok bool) { + value, ok = s.Content[key] + return +} + +func (s *Session) DropKey(key string) { + s.SetValue(key, nil) +}