forked from ortuman/jackal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
48 lines (42 loc) · 1023 Bytes
/
user.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
/*
* Copyright (c) 2018 Miguel Ángel Ortuño.
* See the LICENSE file for more information.
*/
package model
import (
"encoding/gob"
"time"
"github.com/ortuman/jackal/xmpp"
)
// User represents a user storage entity.
type User struct {
Username string
Password string
LastPresence *xmpp.Presence
LastPresenceAt time.Time
}
// FromGob deserializes a User entity from it's gob binary representation.
func (u *User) FromGob(dec *gob.Decoder) {
dec.Decode(&u.Username)
dec.Decode(&u.Password)
var hasPresence bool
dec.Decode(&hasPresence)
if hasPresence {
p := &xmpp.Presence{}
p.FromGob(dec)
u.LastPresence = p
dec.Decode(&u.LastPresenceAt)
}
}
// ToGob converts a User entity to it's gob binary representation.
func (u *User) ToGob(enc *gob.Encoder) {
enc.Encode(&u.Username)
enc.Encode(&u.Password)
hasPresence := u.LastPresence != nil
enc.Encode(&hasPresence)
if hasPresence {
u.LastPresence.ToGob(enc)
u.LastPresenceAt = time.Now()
enc.Encode(&u.LastPresenceAt)
}
}