@@ -14,6 +14,7 @@ import (
14
14
)
15
15
16
16
var pusherPathRegex = regexp .MustCompile ("^/apps/([0-9]+)$" )
17
+ var maxTriggerableChannels = 100
17
18
18
19
/*
19
20
Client to the HTTP API of Pusher.
@@ -138,7 +139,7 @@ func (c *Client) Trigger(channel string, eventName string, data interface{}) (*B
138
139
139
140
/*
140
141
The same as `client.Trigger`, except one passes in a slice of `channels` as the first parameter.
141
- The maximum length of channels is 10 .
142
+ The maximum length of channels is 100 .
142
143
client.TriggerMulti([]string{"a_channel", "another_channel"}, "event", data)
143
144
*/
144
145
func (c * Client ) TriggerMulti (channels []string , eventName string , data interface {}) (* BufferedEvents , error ) {
@@ -167,30 +168,32 @@ func (c *Client) TriggerMultiExclusive(channels []string, eventName string, data
167
168
}
168
169
169
170
func (c * Client ) trigger (channels []string , eventName string , data interface {}, socketID * string ) (* BufferedEvents , error ) {
170
- if len (channels ) > 10 {
171
- return nil , errors .New ("You cannot trigger on more than 10 channels at once" )
171
+ hasEncryptedChannel := false
172
+ for _ , channel := range channels {
173
+ if isEncryptedChannel (channel ) {
174
+ hasEncryptedChannel = true
175
+ }
172
176
}
173
-
174
- if len (channels ) > 1 && encryptedChannelPresent (channels ) {
175
- return nil , errors .New ("You cannot trigger batch events when using encrypted channels" )
177
+ if len (channels ) > maxTriggerableChannels {
178
+ return nil , fmt .Errorf ("You cannot trigger on more than %d channels at once" , maxTriggerableChannels )
179
+ }
180
+ if hasEncryptedChannel && len (channels ) > 1 {
181
+ // For rationale, see limitations of end-to-end encryption in the README
182
+ return nil , errors .New ("You cannot trigger to multiple channels when using encrypted channels" )
176
183
}
177
-
178
184
if ! channelsAreValid (channels ) {
179
185
return nil , errors .New ("At least one of your channels' names are invalid" )
180
186
}
181
- if ! validEncryptionKey (c .EncryptionMasterKey ) && encryptedChannelPresent ( channels ) {
182
- return nil , errors .New ("Your Encryption key is not of the correct format" )
187
+ if hasEncryptedChannel && ! validEncryptionKey (c .EncryptionMasterKey ) {
188
+ return nil , errors .New ("Your encryptionMasterKey is not of the correct format" )
183
189
}
184
-
185
190
if err := validateSocketID (socketID ); err != nil {
186
191
return nil , err
187
192
}
188
-
189
193
payload , err := createTriggerPayload (channels , eventName , data , socketID , c .EncryptionMasterKey )
190
194
if err != nil {
191
195
return nil , err
192
196
}
193
-
194
197
path := fmt .Sprintf ("/apps/%s/events" , c .AppId )
195
198
u , err := createRequestURL ("POST" , c .Host , path , c .Key , c .Secret , authTimestamp (), c .Secure , payload , nil , c .Cluster )
196
199
if err != nil {
@@ -200,20 +203,43 @@ func (c *Client) trigger(channels []string, eventName string, data interface{},
200
203
if err != nil {
201
204
return nil , err
202
205
}
203
-
204
206
return unmarshalledBufferedEvents (response )
205
207
}
206
208
207
209
type batchRequest struct {
208
210
Batch []Event `json:"batch"`
209
211
}
210
212
213
+ /* TriggerBatch triggers multiple events on multiple types of channels in a single call:
214
+ client.TriggerBatch([]pusher.Event{
215
+ { Channel: "donut-1", Name: "ev1", Data: "pippo1" },
216
+ { Channel: "private-encrypted-secretdonut", Name: "ev2", Data: "pippo2" },
217
+ })
218
+ */
211
219
func (c * Client ) TriggerBatch (batch []Event ) (* BufferedEvents , error ) {
212
- payload , err := json .Marshal (& batchRequest {batch })
220
+ hasEncryptedChannel := false
221
+ // validate every channel name and every sockedID (if present) in batch
222
+ for _ , event := range batch {
223
+ if ! validChannel (event .Channel ) {
224
+ return nil , fmt .Errorf ("The channel named %s has a non-valid name" , event .Channel )
225
+ }
226
+ if err := validateSocketID (event .SocketId ); err != nil {
227
+ return nil , err
228
+ }
229
+ if isEncryptedChannel (event .Channel ) {
230
+ hasEncryptedChannel = true
231
+ }
232
+ }
233
+ if hasEncryptedChannel {
234
+ // validate EncryptionMasterKey
235
+ if ! validEncryptionKey (c .EncryptionMasterKey ) {
236
+ return nil , errors .New ("Your encryptionMasterKey is not of the correct format" )
237
+ }
238
+ }
239
+ payload , err := createTriggerBatchPayload (batch , c .EncryptionMasterKey )
213
240
if err != nil {
214
241
return nil , err
215
242
}
216
-
217
243
path := fmt .Sprintf ("/apps/%s/batch_events" , c .AppId )
218
244
u , err := createRequestURL ("POST" , c .Host , path , c .Key , c .Secret , authTimestamp (), c .Secure , payload , nil , c .Cluster )
219
245
if err != nil {
@@ -223,7 +249,6 @@ func (c *Client) TriggerBatch(batch []Event) (*BufferedEvents, error) {
223
249
if err != nil {
224
250
return nil , err
225
251
}
226
-
227
252
return unmarshalledBufferedEvents (response )
228
253
}
229
254
0 commit comments