added sessions with session clean up

This commit is contained in:
overflowerror 2021-08-14 15:15:51 +02:00
parent a833d9d76c
commit 798db0e0f2
7 changed files with 41 additions and 2 deletions

View file

@ -3,6 +3,8 @@ package app
import "threadule/backend/internal/data/models"
type Data interface {
CleanupSessions() error
UpdateTweet(tweet *models.Tweet) error
GetScheduledThreads() ([]models.Thread, error)

View file

@ -21,6 +21,7 @@ func migrate(ctx *app.Context, db *gorm.DB) error {
errs = append(errs, db.AutoMigrate(&models.Group{}))
errs = append(errs, db.AutoMigrate(&models.User{}))
errs = append(errs, db.AutoMigrate(&models.Session{}))
errs = append(errs, db.AutoMigrate(&models.Account{}))
errs = append(errs, db.AutoMigrate(&models.Tweet{}))
errs = append(errs, db.AutoMigrate(&models.Thread{}))

View file

@ -0,0 +1,14 @@
package models
import (
uuid "github.com/satori/go.uuid"
"time"
)
type Session struct {
BaseModel
UserID uuid.UUID
User *User
ValidUntil time.Time
}

View file

@ -0,0 +1,13 @@
package data
import (
"threadule/backend/internal/data/models"
"time"
)
func (d *Data) CleanupSessions() error {
return d.db.
Where("valid_until < ?", time.Now()).
Delete(&models.Session{}).
Error
}

View file

@ -0,0 +1,8 @@
package logic
func (l *Logic) scheduleTriggerAuth() {
err := l.ctx.Data.CleanupSessions()
if err != nil {
l.ctx.Log.Errorf("couldn't clean up sessions: %v", err)
}
}

View file

@ -8,7 +8,8 @@ func (l *Logic) startScheduler() {
go func() {
for {
_ = <-ticker.C
l.scheduleTrigger()
l.scheduleTriggerTwitter()
l.scheduleTriggerAuth()
}
}()
}

View file

@ -109,7 +109,7 @@ func (l *Logic) sendThread(thread *models.Thread) {
l.ctx.Log.Errorf("couldn't update thread in DB: %v", err)
}
func (l *Logic) scheduleTrigger() {
func (l *Logic) scheduleTriggerTwitter() {
threads, err := l.ctx.Data.GetScheduledThreads()
if err != nil {
l.ctx.Log.Errorf("couldn't get scheduled threads from DB: %v", err)