miniflux/ui/session/session.go

70 lines
2.1 KiB
Go
Raw Normal View History

// Copyright 2018 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
2018-08-25 06:51:50 +02:00
package session // import "miniflux.app/ui/session"
import (
2018-08-25 06:51:50 +02:00
"miniflux.app/crypto"
"miniflux.app/storage"
)
// Session handles session data.
type Session struct {
2018-09-03 23:26:40 +02:00
store *storage.Storage
sessionID string
}
// NewOAuth2State generates a new OAuth2 state and stores the value into the database.
func (s *Session) NewOAuth2State() string {
state := crypto.GenerateRandomString(32)
2018-09-03 23:26:40 +02:00
s.store.UpdateSessionField(s.sessionID, "oauth2_state", state)
return state
}
// NewFlashMessage creates a new flash message.
func (s *Session) NewFlashMessage(message string) {
2018-09-03 23:26:40 +02:00
s.store.UpdateSessionField(s.sessionID, "flash_message", message)
}
// FlashMessage returns the current flash message if any.
2018-09-03 23:26:40 +02:00
func (s *Session) FlashMessage(message string) string {
if message != "" {
2018-09-03 23:26:40 +02:00
s.store.UpdateSessionField(s.sessionID, "flash_message", "")
}
return message
}
// NewFlashErrorMessage creates a new flash error message.
func (s *Session) NewFlashErrorMessage(message string) {
2018-09-03 23:26:40 +02:00
s.store.UpdateSessionField(s.sessionID, "flash_error_message", message)
}
// FlashErrorMessage returns the last flash error message if any.
2018-09-03 23:26:40 +02:00
func (s *Session) FlashErrorMessage(message string) string {
if message != "" {
2018-09-03 23:26:40 +02:00
s.store.UpdateSessionField(s.sessionID, "flash_error_message", "")
}
return message
}
// SetLanguage updates the language field in session.
func (s *Session) SetLanguage(language string) {
2018-09-03 23:26:40 +02:00
s.store.UpdateSessionField(s.sessionID, "language", language)
}
// SetTheme updates the theme field in session.
func (s *Session) SetTheme(theme string) {
2018-09-03 23:26:40 +02:00
s.store.UpdateSessionField(s.sessionID, "theme", theme)
}
// SetPocketRequestToken updates Pocket Request Token.
func (s *Session) SetPocketRequestToken(requestToken string) {
2018-09-03 23:26:40 +02:00
s.store.UpdateSessionField(s.sessionID, "pocket_request_token", requestToken)
}
// New returns a new session handler.
2018-09-03 23:26:40 +02:00
func New(store *storage.Storage, sessionID string) *Session {
return &Session{store, sessionID}
}