-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
session.go
55 lines (47 loc) · 1.26 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
package model
import (
"fmt"
"time"
"github.com/neoxelox/odin/internal/class"
"github.com/neoxelox/odin/internal/utility"
"github.com/rs/xid"
)
type Session struct {
class.Model
ID string `db:"id"`
UserID string `db:"user_id"`
Metadata SessionMetadata `db:"metadata"`
CreatedAt time.Time `db:"created_at"`
LastSeenAt time.Time `db:"last_seen_at"`
ExpiredAt *time.Time `db:"expired_at"`
}
type SessionMetadata struct {
IP string
Device string
ApiVersion string
}
func NewSession() *Session {
now := time.Now()
return &Session{
ID: xid.New().String(),
CreatedAt: now,
LastSeenAt: now,
}
}
func (self Session) String() string {
return fmt.Sprintf("<%s: %s>", self.UserID, self.ID)
}
func (self *Session) Copy() *Session {
return &Session{
ID: *utility.CopyString(&self.ID),
UserID: *utility.CopyString(&self.UserID),
Metadata: SessionMetadata{
IP: *utility.CopyString(&self.Metadata.IP),
Device: *utility.CopyString(&self.Metadata.Device),
ApiVersion: *utility.CopyString(&self.Metadata.ApiVersion),
},
CreatedAt: *utility.CopyTime(&self.CreatedAt),
LastSeenAt: *utility.CopyTime(&self.LastSeenAt),
ExpiredAt: utility.CopyTime(self.ExpiredAt),
}
}