-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
session.go
63 lines (52 loc) · 1.35 KB
/
session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package manager
import (
"github.com/emilhauk/chitchat/internal/model"
"github.com/google/uuid"
"time"
)
type SessionBackend interface {
Create(model model.Session) error
FindByID(id string) (model.Session, error)
SetLastSeenAt(id string, lastSeenAt time.Time) error
Delete(id string) error
}
type Session struct {
sessionBackend SessionBackend
}
func NewSessionManager(sessionBackend SessionBackend) Session {
return Session{
sessionBackend: sessionBackend,
}
}
func (m Session) CreateSession(userUUID string) (model.Session, error) {
session := model.Session{
ID: uuid.NewString(),
UserUUID: userUUID,
CreatedAt: time.Now(),
}
err := m.sessionBackend.Create(session)
if err != nil {
return session, err
}
return session, nil
}
func (m Session) FindByIdAndMarkSeen(id string) (model.Session, error) {
session, err := m.FindByID(id)
if err == nil {
go func() {
if err := m.SetLastSeenAt(id, time.Now()); err != nil {
log.Error().Err(err).Msgf("Failed to mark session=%s seen", id)
}
}()
}
return session, err
}
func (m Session) FindByID(id string) (model.Session, error) {
return m.sessionBackend.FindByID(id)
}
func (m Session) SetLastSeenAt(id string, lastSeenAt time.Time) error {
return m.sessionBackend.SetLastSeenAt(id, lastSeenAt)
}
func (m Session) Delete(id string) error {
return m.sessionBackend.Delete(id)
}