-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpush_token.go
78 lines (60 loc) · 1.55 KB
/
push_token.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package push
import (
"time"
"github.com/pkg/errors"
"github.com/code-payments/code-server/pkg/pointer"
"github.com/code-payments/code-server/pkg/code/data/user"
)
type TokenType uint8
const (
TokenTypeUnknown TokenType = iota
TokenTypeFcmAndroid
TokenTypeFcmApns
)
type Record struct {
Id uint64
DataContainerId user.DataContainerID
PushToken string
TokenType TokenType
IsValid bool
AppInstallId *string
CreatedAt time.Time
}
func (r *Record) Clone() *Record {
return &Record{
Id: r.Id,
DataContainerId: r.DataContainerId,
PushToken: r.PushToken,
TokenType: r.TokenType,
IsValid: r.IsValid,
AppInstallId: pointer.StringCopy(r.AppInstallId),
CreatedAt: r.CreatedAt,
}
}
func (r *Record) CopyTo(dst *Record) {
dst.Id = r.Id
dst.DataContainerId = r.DataContainerId
dst.PushToken = r.PushToken
dst.TokenType = r.TokenType
dst.IsValid = r.IsValid
dst.AppInstallId = pointer.StringCopy(r.AppInstallId)
dst.CreatedAt = r.CreatedAt
}
func (r *Record) Validate() error {
if err := r.DataContainerId.Validate(); err != nil {
return errors.Wrap(err, "invalid data container id")
}
if len(r.PushToken) == 0 {
return errors.New("push token is required")
}
if r.TokenType != TokenTypeFcmAndroid && r.TokenType != TokenTypeFcmApns {
return errors.New("invalid token type")
}
if r.AppInstallId != nil && len(*r.AppInstallId) == 0 {
return errors.New("app install id is required when set")
}
if r.CreatedAt.IsZero() {
return errors.New("creation timestamp is required")
}
return nil
}