|
| 1 | +package db |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/alist-org/alist/v3/internal/model" |
| 5 | + "github.com/pkg/errors" |
| 6 | + "gorm.io/gorm/clause" |
| 7 | +) |
| 8 | + |
| 9 | +func GetSession(userID uint, deviceKey string) (*model.Session, error) { |
| 10 | + s := model.Session{UserID: userID, DeviceKey: deviceKey} |
| 11 | + if err := db.Select("user_id, device_key, last_active, status, user_agent, ip").Where(&s).First(&s).Error; err != nil { |
| 12 | + return nil, errors.Wrap(err, "failed find session") |
| 13 | + } |
| 14 | + return &s, nil |
| 15 | +} |
| 16 | + |
| 17 | +func CreateSession(s *model.Session) error { |
| 18 | + return errors.WithStack(db.Create(s).Error) |
| 19 | +} |
| 20 | + |
| 21 | +func UpsertSession(s *model.Session) error { |
| 22 | + return errors.WithStack(db.Clauses(clause.OnConflict{UpdateAll: true}).Create(s).Error) |
| 23 | +} |
| 24 | + |
| 25 | +func DeleteSession(userID uint, deviceKey string) error { |
| 26 | + return errors.WithStack(db.Where("user_id = ? AND device_key = ?", userID, deviceKey).Delete(&model.Session{}).Error) |
| 27 | +} |
| 28 | + |
| 29 | +func CountSessionsByUser(userID uint) (int64, error) { |
| 30 | + var count int64 |
| 31 | + err := db.Model(&model.Session{}).Where("user_id = ?", userID).Count(&count).Error |
| 32 | + return count, errors.WithStack(err) |
| 33 | +} |
| 34 | + |
| 35 | +func DeleteSessionsBefore(ts int64) error { |
| 36 | + return errors.WithStack(db.Where("last_active < ?", ts).Delete(&model.Session{}).Error) |
| 37 | +} |
| 38 | + |
| 39 | +func GetOldestSession(userID uint) (*model.Session, error) { |
| 40 | + var s model.Session |
| 41 | + if err := db.Where("user_id = ?", userID).Order("last_active ASC").First(&s).Error; err != nil { |
| 42 | + return nil, errors.Wrap(err, "failed get oldest session") |
| 43 | + } |
| 44 | + return &s, nil |
| 45 | +} |
| 46 | + |
| 47 | +func UpdateSessionLastActive(userID uint, deviceKey string, lastActive int64) error { |
| 48 | + return errors.WithStack(db.Model(&model.Session{}).Where("user_id = ? AND device_key = ?", userID, deviceKey).Update("last_active", lastActive).Error) |
| 49 | +} |
| 50 | + |
| 51 | +func ListSessionsByUser(userID uint) ([]model.Session, error) { |
| 52 | + var sessions []model.Session |
| 53 | + err := db.Select("user_id, device_key, last_active, status, user_agent, ip").Where("user_id = ? AND status = ?", userID, model.SessionActive).Find(&sessions).Error |
| 54 | + return sessions, errors.WithStack(err) |
| 55 | +} |
| 56 | + |
| 57 | +func ListSessions() ([]model.Session, error) { |
| 58 | + var sessions []model.Session |
| 59 | + err := db.Select("user_id, device_key, last_active, status, user_agent, ip").Where("status = ?", model.SessionActive).Find(&sessions).Error |
| 60 | + return sessions, errors.WithStack(err) |
| 61 | +} |
| 62 | + |
| 63 | +func MarkInactive(sessionID string) error { |
| 64 | + return errors.WithStack(db.Model(&model.Session{}).Where("device_key = ?", sessionID).Update("status", model.SessionInactive).Error) |
| 65 | +} |
0 commit comments