1
1
'use strict' ;
2
2
3
3
const axios = require ( 'axios' ) ;
4
- const config = require ( './config.js' ) ;
4
+ const { webhooks } = require ( './config.js' ) ;
5
5
6
6
/**
7
7
* WebhookController relay class.
8
8
*/
9
9
class WebhookController {
10
- static instance = new WebhookController ( config . webhooks . urls , config . webhooks . delay , config . webhooks . retryCount ) ;
10
+ static instance = new WebhookController ( webhooks . urls , webhooks . delay , webhooks . retryCount , webhooks . polling ) ;
11
11
12
12
/**
13
13
* Initialize new WebhookController object.
14
14
* @param {* } urls
15
15
* @param {number } delay
16
16
* @param {number } retryCount
17
17
*/
18
- constructor ( urls , delay = 5 , retryCount ) {
18
+ constructor ( urls , delay = 5 , retryCount , polling ) {
19
19
console . info ( '[WebhookController] Starting up...' ) ;
20
20
this . urls = urls ;
21
21
this . delay = delay ;
22
22
this . retryCount = retryCount ;
23
+ this . polling = polling ;
24
+ this . online = [ ] ;
23
25
this . pokemonEvents = [ ] ;
24
26
this . pokestopEvents = [ ] ;
25
27
this . lureEvents = [ ] ;
@@ -37,6 +39,7 @@ class WebhookController {
37
39
*/
38
40
start ( ) {
39
41
this . timer = setInterval ( ( ) => this . loopEvents ( ) , this . delay * 1000 ) ;
42
+ setInterval ( ( ) => this . checkOnline ( ) , this . polling * 1000 ) ;
40
43
}
41
44
42
45
/**
@@ -52,7 +55,7 @@ class WebhookController {
52
55
* @param {* } pokemon
53
56
*/
54
57
addPokemonEvent ( pokemon ) {
55
- if ( ! config . webhooks . enabled || this . urls . length === 0 ) {
58
+ if ( ! webhooks . enabled || this . urls . length === 0 ) {
56
59
return ;
57
60
}
58
61
this . pokemonEvents . push ( pokemon ) ;
@@ -63,7 +66,7 @@ class WebhookController {
63
66
* @param {* } pokestop
64
67
*/
65
68
addPokestopEvent ( pokestop ) {
66
- if ( ! config . webhooks . enabled || this . urls . length === 0 ) {
69
+ if ( ! webhooks . enabled || this . urls . length === 0 ) {
67
70
return ;
68
71
}
69
72
this . pokestopEvents . push ( pokestop ) ;
@@ -74,7 +77,7 @@ class WebhookController {
74
77
* @param {* } pokestop
75
78
*/
76
79
addLureEvent ( pokestop ) {
77
- if ( ! config . webhooks . enabled || this . urls . length === 0 ) {
80
+ if ( ! webhooks . enabled || this . urls . length === 0 ) {
78
81
return ;
79
82
}
80
83
this . lureEvents . push ( pokestop ) ;
@@ -85,7 +88,7 @@ class WebhookController {
85
88
* @param {* } pokestop
86
89
*/
87
90
addInvasionEvent ( pokestop ) {
88
- if ( ! config . webhooks . enabled || this . urls . length === 0 ) {
91
+ if ( ! webhooks . enabled || this . urls . length === 0 ) {
89
92
return ;
90
93
}
91
94
this . invasionEvents . push ( pokestop ) ;
@@ -96,7 +99,7 @@ class WebhookController {
96
99
* @param {* } pokestop
97
100
*/
98
101
addQuestEvent ( pokestop ) {
99
- if ( ! config . webhooks . enabled || this . urls . length === 0 ) {
102
+ if ( ! webhooks . enabled || this . urls . length === 0 ) {
100
103
return ;
101
104
}
102
105
this . questEvents . push ( pokestop ) ;
@@ -107,7 +110,7 @@ class WebhookController {
107
110
* @param {* } gym
108
111
*/
109
112
addGymEvent ( gym ) {
110
- if ( ! config . webhooks . enabled || this . urls . length === 0 ) {
113
+ if ( ! webhooks . enabled || this . urls . length === 0 ) {
111
114
return ;
112
115
}
113
116
this . gymEvents . push ( gym ) ;
@@ -118,7 +121,7 @@ class WebhookController {
118
121
* @param {* } gym
119
122
*/
120
123
addGymInfoEvent ( gym ) {
121
- if ( ! config . webhooks . enabled || this . urls . length === 0 ) {
124
+ if ( ! webhooks . enabled || this . urls . length === 0 ) {
122
125
return ;
123
126
}
124
127
this . gymInfoEvents . push ( gym ) ;
@@ -129,7 +132,7 @@ class WebhookController {
129
132
* @param {* } gym
130
133
*/
131
134
addEggEvent ( gym ) {
132
- if ( ! config . webhooks . enabled || this . urls . length === 0 ) {
135
+ if ( ! webhooks . enabled || this . urls . length === 0 ) {
133
136
return ;
134
137
}
135
138
this . eggEvents . push ( gym ) ;
@@ -140,7 +143,7 @@ class WebhookController {
140
143
* @param {* } gym
141
144
*/
142
145
addRaidEvent ( gym ) {
143
- if ( ! config . webhooks . enabled || this . urls . length === 0 ) {
146
+ if ( ! webhooks . enabled || this . urls . length === 0 ) {
144
147
return ;
145
148
}
146
149
this . raidEvents . push ( gym ) ;
@@ -151,7 +154,7 @@ class WebhookController {
151
154
* @param {* } weather
152
155
*/
153
156
addWeatherEvent ( weather ) {
154
- if ( ! config . webhooks . enabled || this . urls . length === 0 ) {
157
+ if ( ! webhooks . enabled || this . urls . length === 0 ) {
155
158
return ;
156
159
}
157
160
this . weatherEvents . push ( weather ) ;
@@ -277,7 +280,7 @@ class WebhookController {
277
280
*/
278
281
sendEvents ( events , url , retryCount ) {
279
282
// If events is not set, skip..
280
- if ( ! events ) {
283
+ if ( ! events || ! this . online . includes ( url ) ) {
281
284
return ;
282
285
}
283
286
// axios request options
@@ -298,14 +301,37 @@ class WebhookController {
298
301
. catch ( err => {
299
302
if ( err ) {
300
303
if ( retryCount < this . retryCount ) {
301
- console . error ( ' [WebhookController] Error occurred, trying again:' , err ) ;
304
+ console . error ( ` [WebhookController] Error occurred, trying again for ${ url } ` ) ;
302
305
this . sendEvents ( events , url , retryCount ++ ) ;
303
306
} else {
304
- console . error ( ' [WebhookController] Error occurred, max retry count reached:' , err ) ;
307
+ console . error ( ` [WebhookController] Error occurred, max retry count reached for ${ url } ` ) ;
305
308
}
306
309
}
307
310
} ) ;
308
311
}
312
+
313
+ checkOnline ( ) {
314
+ this . online = [ ] ;
315
+ this . urls . forEach ( url => {
316
+ axios ( {
317
+ url : url ,
318
+ method : 'POST' ,
319
+ data : { pokemon_id : 0 } ,
320
+ headers : {
321
+ 'Accept' : 'application/json' ,
322
+ 'Content-Type' : 'application/json' ,
323
+ 'Cache-Control' : 'no-cache' ,
324
+ 'User-Agent' : 'Nodedradamus' ,
325
+ } ,
326
+ } )
327
+ . then ( ( ) => this . online . push ( url ) )
328
+ . catch ( err => {
329
+ if ( err ) {
330
+ console . error ( `${ url } is offline` ) ;
331
+ } ;
332
+ } ) ;
333
+ } ) ;
334
+ } ;
309
335
}
310
336
311
337
module . exports = WebhookController ;
0 commit comments