2018-04-29 19:35:04 -04:00
|
|
|
// 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 00:51:50 -04:00
|
|
|
package session // import "miniflux.app/ui/session"
|
2018-04-29 19:35:04 -04:00
|
|
|
|
|
|
|
import (
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/crypto"
|
|
|
|
"miniflux.app/storage"
|
2018-04-29 19:35:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Session handles session data.
|
|
|
|
type Session struct {
|
2018-09-03 17:26:40 -04:00
|
|
|
store *storage.Storage
|
|
|
|
sessionID string
|
2018-04-29 19:35:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewOAuth2State generates a new OAuth2 state and stores the value into the database.
|
|
|
|
func (s *Session) NewOAuth2State() string {
|
|
|
|
state := crypto.GenerateRandomString(32)
|
2019-01-21 23:21:05 -05:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "oauth2_state", state)
|
2018-04-29 19:35:04 -04:00
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFlashMessage creates a new flash message.
|
|
|
|
func (s *Session) NewFlashMessage(message string) {
|
2019-01-21 23:21:05 -05:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "flash_message", message)
|
2018-04-29 19:35:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// FlashMessage returns the current flash message if any.
|
2018-09-03 17:26:40 -04:00
|
|
|
func (s *Session) FlashMessage(message string) string {
|
2018-04-29 19:35:04 -04:00
|
|
|
if message != "" {
|
2019-01-21 23:21:05 -05:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "flash_message", "")
|
2018-04-29 19:35:04 -04:00
|
|
|
}
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFlashErrorMessage creates a new flash error message.
|
|
|
|
func (s *Session) NewFlashErrorMessage(message string) {
|
2019-01-21 23:21:05 -05:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "flash_error_message", message)
|
2018-04-29 19:35:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// FlashErrorMessage returns the last flash error message if any.
|
2018-09-03 17:26:40 -04:00
|
|
|
func (s *Session) FlashErrorMessage(message string) string {
|
2018-04-29 19:35:04 -04:00
|
|
|
if message != "" {
|
2019-01-21 23:21:05 -05:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "flash_error_message", "")
|
2018-04-29 19:35:04 -04:00
|
|
|
}
|
|
|
|
return message
|
|
|
|
}
|
|
|
|
|
2018-07-19 01:30:05 -04:00
|
|
|
// SetLanguage updates the language field in session.
|
2018-04-29 19:35:04 -04:00
|
|
|
func (s *Session) SetLanguage(language string) {
|
2019-01-21 23:21:05 -05:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "language", language)
|
2018-04-29 19:35:04 -04:00
|
|
|
}
|
|
|
|
|
2018-07-19 01:30:05 -04:00
|
|
|
// SetTheme updates the theme field in session.
|
|
|
|
func (s *Session) SetTheme(theme string) {
|
2019-01-21 23:21:05 -05:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "theme", theme)
|
2018-07-19 01:30:05 -04:00
|
|
|
}
|
|
|
|
|
2018-05-20 18:29:14 -04:00
|
|
|
// SetPocketRequestToken updates Pocket Request Token.
|
|
|
|
func (s *Session) SetPocketRequestToken(requestToken string) {
|
2019-01-21 23:21:05 -05:00
|
|
|
s.store.UpdateAppSessionField(s.sessionID, "pocket_request_token", requestToken)
|
2018-05-20 18:29:14 -04:00
|
|
|
}
|
|
|
|
|
2018-04-29 19:35:04 -04:00
|
|
|
// New returns a new session handler.
|
2018-09-03 17:26:40 -04:00
|
|
|
func New(store *storage.Storage, sessionID string) *Session {
|
|
|
|
return &Session{store, sessionID}
|
2018-04-29 19:35:04 -04:00
|
|
|
}
|