-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoosh.go
More file actions
165 lines (142 loc) · 3.44 KB
/
goosh.go
File metadata and controls
165 lines (142 loc) · 3.44 KB
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package goosh
import (
"encoding/json"
"time"
)
type PushService interface {
Process(Request) (Response, error)
}
type Request struct {
PushID string `json:"push_id"`
Multiplexed *Multiplexed `json:"multiplexed,omitempty"`
Batched *Batched `json:"batched,omitempty"`
APNSAuth *APNSAuth `json:"apns,omitempty"`
FCMAuth *FCMAuth `json:"fcm,omitempty"`
CustomID string `json:"custom_id"`
iterator int
batchedKeys []string
initialized bool
total int
multiLen int
done func() error
}
// Multiplexed provide a single payload for multiple devices
type Multiplexed struct {
Devices []string `json:"devices"`
Payload json.RawMessage `json:"payload"`
}
// Batched provides a payload for each device
type Batched map[string]json.RawMessage
func (r Request) Platform() string {
if r.FCMAuth != nil && r.APNSAuth == nil {
return "fcm"
}
if r.FCMAuth == nil && r.APNSAuth != nil {
return "apns"
}
return ""
}
func (r Request) IsAPNS() bool {
return r.Platform() == "apns"
}
func (r Request) IsFCM() bool {
return r.Platform() == "fcm"
}
func (r *Request) Reset() {
r.iterator = -1
}
func (r *Request) initialize() {
if !r.initialized {
r.Reset()
r.initialized = true
r.batchedKeys = []string{}
if r.Batched != nil {
for k, _ := range *r.Batched {
r.batchedKeys = append(r.batchedKeys, k)
}
}
if r.Multiplexed != nil {
r.total += len(r.Multiplexed.Devices)
r.multiLen = len(r.Multiplexed.Devices)
}
r.total += len(r.batchedKeys)
}
}
func (r *Request) Next() bool {
r.initialize()
r.iterator++
if r.iterator >= r.total {
return false
}
return true
}
func (r *Request) Value() (msg Message) {
if r.iterator >= r.total {
return
}
if r.iterator < r.multiLen {
msg.Payload = r.Multiplexed.Payload
msg.Token = r.Multiplexed.Devices[r.iterator]
return
}
offi := r.iterator - r.multiLen
msg.Token = r.batchedKeys[offi]
msg.Payload = (*r.Batched)[r.batchedKeys[offi]]
return
}
func (r Request) Count() int64 {
r.initialize()
return int64(r.total)
}
func (r *Request) SetDone(f func() error) {
r.done = f
}
func (r Request) Done() {
if r.done != nil {
r.done()
}
}
type Message struct {
Token string
Payload json.RawMessage
}
type Response struct {
Failed bool `json:"failed"`
Error *Error `json:"error,omitempty"`
Devices []DeviceResponse `json:"devices,omitempty"`
Success int64 `json:"success"`
Failure int64 `json:"failure"`
PushID string `json:"push_id"`
CustomID string `json:"custom_id"`
Service string `json:"service"`
done func() error
}
func (r *Response) SetDone(f func() error) {
r.done = f
}
func (r Response) Done() {
if r.done != nil {
r.done()
}
}
type Error struct {
Description string `json:"description"`
Code int64 `json:"code"`
ShouldRetry bool `json:"should_retry"`
RetryAt *time.Time `json:"retry_at,omitempty"`
}
type DeviceResponse struct {
Identifier string `json:"identifier"`
Delivered bool `json:"delivered"`
Error *Error `json:"error,omitempty"`
ShouldRetry bool `json:"should_retry,omitempty"`
Canonical string `json:"canonical,omitempty"`
}
type FCMAuth struct {
AuthKey string `json:"auth_key"`
}
type APNSAuth struct {
Certificate string `json:"certificate"`
CertificatePassword string `json:"certificate_password"`
Sandbox bool `json:"sandbox"`
}