1
1
2
- var JsonDB = require ( 'node-json-db' ) ;
3
- var FCM = require ( 'fcm-push' ) ;
4
- var diff = require ( 'rus-diff' ) . diff ;
5
- var log4js = require ( 'log4js' ) ;
6
- var SN = require ( 'sync-node' ) ;
2
+ var JsonDB = require ( 'node-json-db' ) ;
3
+ var FCM = require ( 'fcm-push' ) ;
4
+ var diff = require ( 'rus-diff' ) . diff ;
5
+ var log4js = require ( 'log4js' ) ;
6
+ var SN = require ( 'sync-node' ) ;
7
+ var sha1 = require ( 'sha1' ) ;
8
+
7
9
8
10
const TAG = "Flamebase Database" ;
9
11
var logger = log4js . getLogger ( TAG ) ;
@@ -12,6 +14,7 @@ JSON.stringifyAligned = require('json-align');
12
14
13
15
var ACTION_SIMPLE_UPDATE = "simple_update" ;
14
16
var ACTION_SLICE_UPDATE = "slice_update" ;
17
+ var ACTION_NO_UPDATE = "no_update" ;
15
18
16
19
function FlamebaseDatabase ( database , path ) {
17
20
@@ -49,8 +52,14 @@ function FlamebaseDatabase(database, path) {
49
52
*/
50
53
this . syncFromDatabase = function ( ) {
51
54
try {
52
- console . log ( "####################### data path: " + path ) ;
55
+ if ( this . debugVal ) {
56
+ logger . debug ( "####################### data path: " + path ) ;
57
+ }
53
58
object . ref = object . db . getData ( path ) ;
59
+ this . lastStringReference = JSON . stringify ( object . ref ) ;
60
+ if ( this . debugVal ) {
61
+ logger . debug ( "####################### ref: " + JSON . stringify ( object . ref ) ) ;
62
+ }
54
63
object . syncNotifications ( ) ;
55
64
} catch ( e ) {
56
65
console . log ( "####################### not found, generating {} " ) ;
@@ -138,8 +147,8 @@ function FlamebaseDatabase(database, path) {
138
147
}
139
148
}
140
149
141
- var data_android = this . getPartsFor ( this . OS . ANDROID ) ;
142
- var data_ios = this . getPartsFor ( this . OS . IOS ) ;
150
+ var data_android = this . getPartsFor ( this . OS . ANDROID , JSON . parse ( this . lastStringReference ) , this . ref ) ;
151
+ var data_ios = this . getPartsFor ( this . OS . IOS , JSON . parse ( this . lastStringReference ) , this . ref ) ;
143
152
144
153
if ( object . debugVal ) {
145
154
logger . debug ( "android_tokens_size: " + android_tokens . length ) ;
@@ -179,6 +188,8 @@ function FlamebaseDatabase(database, path) {
179
188
sen . notification = notification ;
180
189
this . sendPushMessage ( sen ) ;
181
190
}
191
+ } else if ( this . debugVal ) {
192
+ logger . debug ( "no differences located" ) ;
182
193
}
183
194
}
184
195
@@ -211,6 +222,119 @@ function FlamebaseDatabase(database, path) {
211
222
s . notification = notification ;
212
223
this . sendPushMessage ( s ) ;
213
224
}
225
+ } else if ( this . debugVal ) {
226
+ logger . debug ( "no differences located" ) ;
227
+ }
228
+ }
229
+ } ;
230
+
231
+ this . sendDifferencesForClient = function ( before , device ) {
232
+
233
+ var ios_tokens = [ ] ;
234
+ var android_tokens = [ ] ;
235
+
236
+ var id = this . pushConfig . referenceId ( ) ;
237
+ var notification = this . pushConfig . notification ( ) ;
238
+
239
+ if ( device . os . indexOf ( this . OS . IOS ) !== - 1 ) {
240
+ ios_tokens . push ( device . token ) ;
241
+ } else {
242
+ android_tokens . push ( device . token ) ;
243
+ }
244
+
245
+ var data_android = this . getPartsFor ( this . OS . ANDROID , JSON . parse ( before ) , this . ref ) ;
246
+ var data_ios = this . getPartsFor ( this . OS . IOS , JSON . parse ( before ) , this . ref ) ;
247
+
248
+ if ( object . debugVal ) {
249
+ logger . debug ( "android_tokens_size: " + android_tokens . length ) ;
250
+ logger . debug ( "ios_tokens_size: " + ios_tokens . length ) ;
251
+ logger . debug ( "data_android_size: " + data_android . parts . length ) ;
252
+ logger . debug ( "data_ios_size: " + data_ios . parts . length ) ;
253
+ }
254
+
255
+ this . lastStringReference = JSON . stringify ( this . ref ) ;
256
+
257
+ if ( android_tokens . length > 0 ) {
258
+ if ( data_android . parts . length === 1 ) {
259
+ var data = { } ;
260
+ data . id = id ;
261
+ data . tag = this . pushConfig . tag ( ) ;
262
+ data . reference = data_android . parts [ 0 ] ;
263
+ data . action = ACTION_SIMPLE_UPDATE ;
264
+ data . size = data_android . parts . length ;
265
+ data . index = 0 ;
266
+ var send = { } ;
267
+ send . data = data ;
268
+ send . tokens = android_tokens ;
269
+ send . notification = notification ;
270
+ this . sendPushMessage ( send ) ;
271
+ } else if ( data_android . parts . length > 1 ) {
272
+ for ( var i = 0 ; i < data_android . parts . length ; i ++ ) {
273
+ var dat = { } ;
274
+ dat . id = id ;
275
+ dat . tag = this . pushConfig . tag ( ) ;
276
+ dat . reference = data_android . parts [ i ] ;
277
+ dat . action = ACTION_SLICE_UPDATE ;
278
+ dat . index = i ;
279
+ dat . size = data_android . parts . length ;
280
+ var sen = { } ;
281
+ sen . data = dat ;
282
+ sen . tokens = android_tokens ;
283
+ sen . notification = notification ;
284
+ this . sendPushMessage ( sen ) ;
285
+ }
286
+ } else {
287
+ var data = { } ;
288
+ data . id = id ;
289
+ data . tag = this . pushConfig . tag ( ) ;
290
+ data . action = ACTION_NO_UPDATE ;
291
+ var send = { } ;
292
+ send . data = data ;
293
+ send . tokens = android_tokens ;
294
+ send . notification = notification ;
295
+ this . sendPushMessage ( send ) ;
296
+ }
297
+ }
298
+
299
+ if ( ios_tokens . length > 0 ) {
300
+ if ( data_ios . parts . length === 1 ) {
301
+ var da = { } ;
302
+ da . id = id ;
303
+ da . tag = this . pushConfig . tag ( ) ;
304
+ da . reference = data_ios . parts [ 0 ] ;
305
+ da . action = ACTION_SIMPLE_UPDATE ;
306
+ da . size = data_ios . parts . length ;
307
+ da . index = 0 ;
308
+ var se = { } ;
309
+ se . data = da ;
310
+ se . tokens = ios_tokens ;
311
+ se . notification = notification ;
312
+ this . sendPushMessage ( se ) ;
313
+ } else if ( data_ios . parts . length > 1 ) {
314
+ for ( var i = 0 ; i < data_ios . parts . length ; i ++ ) {
315
+ var d = { } ;
316
+ d . id = id ;
317
+ d . tag = this . pushConfig . tag ( ) ;
318
+ d . reference = data_ios . parts [ i ] ;
319
+ d . action = ACTION_SLICE_UPDATE ;
320
+ d . index = i ;
321
+ d . size = data_ios . parts . length ;
322
+ var s = { } ;
323
+ s . data = d ;
324
+ s . tokens = ios_tokens ;
325
+ s . notification = notification ;
326
+ this . sendPushMessage ( s ) ;
327
+ }
328
+ } else {
329
+ var data = { } ;
330
+ data . id = id ;
331
+ data . tag = this . pushConfig . tag ( ) ;
332
+ data . action = ACTION_NO_UPDATE ;
333
+ var send = { } ;
334
+ send . data = data ;
335
+ send . tokens = ios_tokens ;
336
+ send . notification = notification ;
337
+ this . sendPushMessage ( send ) ;
214
338
}
215
339
}
216
340
} ;
@@ -245,25 +369,43 @@ function FlamebaseDatabase(database, path) {
245
369
} ) ;
246
370
} ;
247
371
248
- this . getPartsFor = function ( os ) {
372
+ this . getPartsFor = function ( os , before , after ) {
249
373
var notification = this . pushConfig . notification ( ) ;
250
374
var notificationLength = JSON . stringify ( notification ) . length ;
251
375
252
- var differences = JSON . stringify ( diff ( JSON . parse ( this . lastStringReference ) , this . ref ) ) ;
253
- differences = this . string2Hex ( differences ) ;
376
+ // var differences = JSON.stringify(diff(JSON.parse(this.lastStringReference), this.ref));
377
+ var differences = JSON . stringify ( diff ( before , after ) ) ;
254
378
var partsToSend = [ ] ;
255
- var limit = os . indexOf ( this . OS . IOS ) !== - 1 ? this . lengthLimit . IOS - notificationLength : this . lengthLimit . ANDROID - notificationLength ;
256
- if ( differences . length > limit ) {
257
- var index = - 1 ;
258
- var pendingChars = differences . length ;
259
- while ( pendingChars > 0 ) {
260
- index ++ ;
261
- var part = differences . slice ( index * limit , ( pendingChars < limit ? index * limit + pendingChars : ( index + 1 ) * limit ) ) ;
262
- pendingChars = pendingChars - part . length ;
263
- partsToSend . push ( part ) ;
379
+
380
+ if ( this . debugVal ) {
381
+ logger . debug ( "diff: " + differences ) ;
382
+ }
383
+
384
+ if ( differences === "false" ) {
385
+ var currentStringAfter = JSON . stringify ( after ) ;
386
+ var currentStringBefore = JSON . stringify ( before ) ;
387
+ if ( currentStringBefore . length !== currentStringAfter . length ) {
388
+ logger . error ( "something went wrong; sha1 diff: " + currentStringBefore . length + " - " + currentStringAfter . length ) ;
389
+ }
390
+ if ( this . debugVal ) {
391
+ logger . debug ( "no differences" ) ;
264
392
}
265
393
} else {
266
- partsToSend . push ( differences ) ;
394
+ differences = this . string2Hex ( differences ) ;
395
+
396
+ var limit = os . indexOf ( this . OS . IOS ) !== - 1 ? this . lengthLimit . IOS - notificationLength : this . lengthLimit . ANDROID - notificationLength ;
397
+ if ( differences . length > limit ) {
398
+ var index = - 1 ;
399
+ var pendingChars = differences . length ;
400
+ while ( pendingChars > 0 ) {
401
+ index ++ ;
402
+ var part = differences . slice ( index * limit , ( pendingChars < limit ? index * limit + pendingChars : ( index + 1 ) * limit ) ) ;
403
+ pendingChars = pendingChars - part . length ;
404
+ partsToSend . push ( part ) ;
405
+ }
406
+ } else {
407
+ partsToSend . push ( differences ) ;
408
+ }
267
409
}
268
410
269
411
var result = { } ;
0 commit comments