2023-06-19 23:42:47 +02:00
|
|
|
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
2017-12-17 03:07:53 +01:00
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
package storage // import "miniflux.app/v2/internal/storage"
|
2017-12-17 03:07:53 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
|
2023-08-11 04:46:45 +02:00
|
|
|
"miniflux.app/v2/internal/crypto"
|
|
|
|
"miniflux.app/v2/internal/model"
|
2017-12-17 03:07:53 +01:00
|
|
|
)
|
|
|
|
|
2019-01-22 05:21:05 +01:00
|
|
|
// CreateAppSessionWithUserPrefs creates a new application session with the given user preferences.
|
|
|
|
func (s *Storage) CreateAppSessionWithUserPrefs(userID int64) (*model.Session, error) {
|
|
|
|
user, err := s.UserByID(userID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-12-17 03:07:53 +01:00
|
|
|
session := model.Session{
|
2019-01-22 05:21:05 +01:00
|
|
|
ID: crypto.GenerateRandomString(32),
|
|
|
|
Data: &model.SessionData{
|
|
|
|
CSRF: crypto.GenerateRandomString(64),
|
|
|
|
Theme: user.Theme,
|
|
|
|
Language: user.Language,
|
|
|
|
},
|
2017-12-17 03:07:53 +01:00
|
|
|
}
|
|
|
|
|
2019-01-22 05:21:05 +01:00
|
|
|
return s.createAppSession(&session)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateAppSession creates a new application session.
|
|
|
|
func (s *Storage) CreateAppSession() (*model.Session, error) {
|
|
|
|
session := model.Session{
|
|
|
|
ID: crypto.GenerateRandomString(32),
|
|
|
|
Data: &model.SessionData{
|
|
|
|
CSRF: crypto.GenerateRandomString(64),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.createAppSession(&session)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Storage) createAppSession(session *model.Session) (*model.Session, error) {
|
2019-10-30 06:48:07 +01:00
|
|
|
query := `INSERT INTO sessions (id, data) VALUES ($1, $2)`
|
|
|
|
_, err := s.db.Exec(query, session.ID, session.Data)
|
2017-12-17 03:07:53 +01:00
|
|
|
if err != nil {
|
2019-10-30 06:48:07 +01:00
|
|
|
return nil, fmt.Errorf(`store: unable to create app session: %v`, err)
|
2017-12-17 03:07:53 +01:00
|
|
|
}
|
|
|
|
|
2019-01-22 05:21:05 +01:00
|
|
|
return session, nil
|
2017-12-17 03:07:53 +01:00
|
|
|
}
|
|
|
|
|
2019-01-22 05:21:05 +01:00
|
|
|
// UpdateAppSessionField updates only one session field.
|
2023-09-03 06:35:10 +02:00
|
|
|
func (s *Storage) UpdateAppSessionField(sessionID, field string, value any) error {
|
2019-10-30 06:48:07 +01:00
|
|
|
query := `
|
|
|
|
UPDATE
|
|
|
|
sessions
|
|
|
|
SET
|
|
|
|
data = jsonb_set(data, '{%s}', to_jsonb($1::text), true)
|
|
|
|
WHERE
|
|
|
|
id=$2
|
|
|
|
`
|
2017-12-17 03:07:53 +01:00
|
|
|
_, err := s.db.Exec(fmt.Sprintf(query, field), value, sessionID)
|
|
|
|
if err != nil {
|
2019-10-30 06:48:07 +01:00
|
|
|
return fmt.Errorf(`store: unable to update session field: %v`, err)
|
2017-12-17 03:07:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-11-05 18:57:35 +01:00
|
|
|
func (s *Storage) UpdateAppSessionObjectField(sessionID, field string, value interface{}) error {
|
|
|
|
query := `
|
|
|
|
UPDATE
|
|
|
|
sessions
|
|
|
|
SET
|
|
|
|
data = jsonb_set(data, '{%s}', $1, true)
|
|
|
|
WHERE
|
|
|
|
id=$2
|
|
|
|
`
|
|
|
|
_, err := s.db.Exec(fmt.Sprintf(query, field), value, sessionID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(`store: unable to update session field: %v`, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-01-22 05:21:05 +01:00
|
|
|
// AppSession returns the given session.
|
|
|
|
func (s *Storage) AppSession(id string) (*model.Session, error) {
|
2017-12-17 03:07:53 +01:00
|
|
|
var session model.Session
|
|
|
|
|
|
|
|
query := "SELECT id, data FROM sessions WHERE id=$1"
|
|
|
|
err := s.db.QueryRow(query, id).Scan(
|
|
|
|
&session.ID,
|
|
|
|
&session.Data,
|
|
|
|
)
|
|
|
|
|
2019-10-30 06:48:07 +01:00
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
|
|
|
return nil, fmt.Errorf(`store: session not found: %s`, id)
|
|
|
|
case err != nil:
|
|
|
|
return nil, fmt.Errorf(`store: unable to fetch session: %v`, err)
|
|
|
|
default:
|
|
|
|
return &session, nil
|
2017-12-17 03:07:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlushAllSessions removes all sessions from the database.
|
|
|
|
func (s *Storage) FlushAllSessions() (err error) {
|
|
|
|
_, err = s.db.Exec(`DELETE FROM user_sessions`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = s.db.Exec(`DELETE FROM sessions`)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-17 03:48:17 +01:00
|
|
|
|
2019-09-12 05:10:34 +02:00
|
|
|
// CleanOldSessions removes sessions older than specified days.
|
|
|
|
func (s *Storage) CleanOldSessions(days int) int64 {
|
2019-10-30 06:48:07 +01:00
|
|
|
query := `
|
|
|
|
DELETE FROM
|
|
|
|
sessions
|
|
|
|
WHERE
|
2024-02-25 16:04:46 +01:00
|
|
|
created_at < now() - interval '%d days'
|
2019-10-30 06:48:07 +01:00
|
|
|
`
|
|
|
|
result, err := s.db.Exec(fmt.Sprintf(query, days))
|
2017-12-17 03:48:17 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
n, _ := result.RowsAffected()
|
|
|
|
return n
|
|
|
|
}
|