@@ -5517,7 +5517,7 @@ var qm = {
5517
5517
refreshIfEmpty : function ( successHandler , errorHandler ) {
5518
5518
if ( ! qm . notifications . getNumberInGlobalsOrLocalStorage ( ) ) {
5519
5519
qm . qmLog . info ( 'No notifications in local storage' ) ;
5520
- qm . notifications . refreshNotifications ( successHandler , errorHandler ) ;
5520
+ qm . notifications . syncTrackingReminderNotifications ( successHandler , errorHandler ) ;
5521
5521
return true ;
5522
5522
}
5523
5523
qm . qmLog . info ( qm . notifications . getNumberInGlobalsOrLocalStorage ( ) + ' notifications in local storage' ) ;
@@ -5528,7 +5528,7 @@ var qm = {
5528
5528
qm . qmLog . info ( "qm.notifications.refreshIfEmptyOrStale" ) ;
5529
5529
if ( ! qm . notifications . getNumberInGlobalsOrLocalStorage ( ) || qm . notifications . getSecondsSinceLastNotificationsRefresh ( ) > 3600 ) {
5530
5530
qm . qmLog . info ( 'Refreshing notifications because empty or last refresh was more than an hour ago' ) ;
5531
- qm . notifications . refreshNotifications ( callback ) ;
5531
+ qm . notifications . syncTrackingReminderNotifications ( callback ) ;
5532
5532
} else {
5533
5533
qm . qmLog . info ( 'Not refreshing notifications because last refresh was last than an hour ago and we have notifications in local storage' ) ;
5534
5534
if ( callback ) {
@@ -5620,39 +5620,16 @@ var qm = {
5620
5620
} else {
5621
5621
console . info ( 'No rating notifications for popup' ) ;
5622
5622
qm . notifications . getLastNotificationsRefreshTime ( ) ;
5623
- qm . notifications . refreshNotifications ( ) ;
5623
+ qm . notifications . syncTrackingReminderNotifications ( ) ;
5624
5624
return null ;
5625
5625
}
5626
5626
} ,
5627
5627
deleteByVariableName : function ( variableName ) {
5628
5628
return qm . storage . deleteByProperty ( qm . items . trackingReminderNotifications , 'variableName' , variableName ) ;
5629
5629
} ,
5630
5630
promise : null ,
5631
- refreshNotifications : function ( successHandler , errorHandler , options ) {
5632
- var route = qm . apiPaths . trackingReminderNotificationsPast ;
5633
- qm . api . getRequestUrl ( route , function ( url ) {
5634
- // Can't use QM SDK in service worker
5635
- qm . api . getViaXhrOrFetch ( url , function ( response ) {
5636
- if ( ! response ) {
5637
- qm . qmLog . error ( "No response from " + url ) ;
5638
- if ( errorHandler ) {
5639
- errorHandler ( "No response from " + url ) ;
5640
- }
5641
- return ;
5642
- }
5643
- if ( response . status === 401 ) {
5644
- qm . chrome . showSignInNotification ( ) ;
5645
- } else {
5646
- qm . storage . setTrackingReminderNotifications ( response . data ) ;
5647
- if ( successHandler ) {
5648
- successHandler ( response . data ) ;
5649
- }
5650
- }
5651
- } )
5652
- } , options ) ;
5653
- } ,
5654
5631
refreshAndShowPopupIfNecessary : function ( notificationParams ) {
5655
- qm . notifications . refreshNotifications ( notificationParams , function ( trackingReminderNotifications ) {
5632
+ qm . notifications . syncTrackingReminderNotifications ( function ( response ) {
5656
5633
var uniqueNotification = qm . notifications . getMostRecentUniqueNotificationNotInSyncQueue ( ) ;
5657
5634
function objectLength ( obj ) {
5658
5635
var result = 0 ;
@@ -5664,6 +5641,7 @@ var qm = {
5664
5641
}
5665
5642
return result ;
5666
5643
}
5644
+ var trackingReminderNotifications = qm . notifications . getFromGlobalsOrLocalStorage ( ) ;
5667
5645
var numberOfWaitingNotifications = objectLength ( trackingReminderNotifications ) ;
5668
5646
if ( uniqueNotification ) {
5669
5647
function getChromeRatingNotificationParams ( trackingReminderNotification ) {
@@ -5769,7 +5747,7 @@ var qm = {
5769
5747
if ( ! successHandler ) {
5770
5748
return null ;
5771
5749
}
5772
- qm . notifications . refreshNotifications ( function ( notifications ) {
5750
+ qm . notifications . syncTrackingReminderNotifications ( function ( response ) {
5773
5751
var notification = qm . notifications . getMostRecentNotification ( ) ;
5774
5752
if ( notification ) {
5775
5753
successHandler ( notification ) ;
@@ -5781,7 +5759,7 @@ var qm = {
5781
5759
schedulePost : function ( delayInMilliseconds ) {
5782
5760
var queue = qm . storage . getItem ( qm . items . notificationsSyncQueue ) ;
5783
5761
if ( queue && queue . length > 10 ) {
5784
- qm . notifications . post ( ) ;
5762
+ qm . notifications . syncTrackingReminderNotifications ( ) ;
5785
5763
return ;
5786
5764
}
5787
5765
if ( ! delayInMilliseconds ) {
@@ -5797,7 +5775,7 @@ var qm = {
5797
5775
}
5798
5776
setTimeout ( function ( ) {
5799
5777
qm . qmLog . info ( "Notifications sync countdown completed. Syncing now... " ) ;
5800
- qm . notifications . post ( ) ;
5778
+ qm . notifications . syncTrackingReminderNotifications ( ) ;
5801
5779
} , delayInMilliseconds ) ;
5802
5780
} else {
5803
5781
if ( ! qm . platform . isMobile ( ) ) { // Better performance
@@ -5826,8 +5804,8 @@ var qm = {
5826
5804
qm . notifications . deleteByVariableName ( n . variableName ) ;
5827
5805
qm . notifications . addToSyncQueue ( n ) ;
5828
5806
} ,
5829
- post : function ( successHandler , errorHandler ) {
5830
- qm . qmLog . debug ( "Called postNotifications ..." ) ;
5807
+ syncTrackingReminderNotifications : function ( successHandler , errorHandler ) {
5808
+ qm . qmLog . debug ( "Called syncTrackingReminderNotifications ..." ) ;
5831
5809
var notifications = qm . storage . getItem ( qm . items . notificationsSyncQueue ) ;
5832
5810
qm . storage . removeItem ( qm . items . notificationsSyncQueue ) ;
5833
5811
qm . storage . removeItem ( qm . items . trackingReminderNotificationSyncScheduled ) ;
@@ -5851,21 +5829,29 @@ var qm = {
5851
5829
} )
5852
5830
qm . api . postToQuantiModo ( body , 'v3/trackingReminderNotifications' ,
5853
5831
function ( response ) {
5854
- var measurements = response . measurements ;
5855
- if ( ! measurements && response . data ) { measurements = response . data . measurements ; }
5832
+ if ( ! response ) {
5833
+ var err = "No response from postToQuantiModo(body, 'v3/trackingReminderNotifications" ;
5834
+ if ( errorHandler ) {
5835
+ errorHandler ( err ) ;
5836
+ return ;
5837
+ } else {
5838
+ throw err ;
5839
+ }
5840
+ }
5841
+ var measurements = response . measurements || response . data . measurements ;
5856
5842
if ( measurements ) { qm . measurements . addMeasurementsToMemory ( measurements ) ; }
5843
+ var trackingReminderNotifications = response . trackingReminderNotifications || response . data . trackingReminderNotifications ;
5844
+ if ( trackingReminderNotifications ) { qm . storage . setTrackingReminderNotifications ( notifications ) ; }
5857
5845
if ( successHandler ) { successHandler ( response ) ; }
5858
5846
} , function ( response ) {
5859
- if ( ! response . success ) {
5860
- qm . qmLog . error ( response . message )
5861
- var newNotificationsSyncQueue = qm . storage . getItem ( qm . items . notificationsSyncQueue ) ;
5862
- if ( newNotificationsSyncQueue ) { notifications = notifications . concat ( newNotificationsSyncQueue ) ; }
5863
- qm . storage . setItem ( qm . items . notificationsSyncQueue , notifications ) ;
5864
- if ( errorHandler ) { errorHandler ( response . message || response . error ) ; }
5865
- } else { // This happens when the error is a message saying the notification was already deleted
5866
- // so we don't want to put notifications back in queue
5867
- qm . qmLog . warn ( response . message )
5868
- }
5847
+ qm . qmLog . error ( response . message )
5848
+ // This happens when the error is a message saying the notification was already deleted
5849
+ // so we don't want to put notifications back in queue
5850
+ // Don't return to queue or we cause an infinite loop if we get a no changes error
5851
+ // var newNotificationsSyncQueue = qm.storage.getItem(qm.items.notificationsSyncQueue);
5852
+ // if(newNotificationsSyncQueue){notifications = notifications.concat(newNotificationsSyncQueue);}
5853
+ // qm.storage.setItem(qm.items.notificationsSyncQueue, notifications);
5854
+ if ( errorHandler ) { errorHandler ( response . message || response . error ) ; }
5869
5855
} ) ;
5870
5856
} ,
5871
5857
skip : function ( trackingReminderNotification ) {
@@ -9519,6 +9505,7 @@ var qm = {
9519
9505
}
9520
9506
} ,
9521
9507
userVariables : {
9508
+ defaultLimit : 20 ,
9522
9509
updateLatestMeasurementTime : function ( variableName , lastValue ) {
9523
9510
qm . storage . getUserVariableByName ( variableName , true , lastValue ) ;
9524
9511
} ,
@@ -9531,7 +9518,7 @@ var qm = {
9531
9518
params . sort = '-latestMeasurementTime' ;
9532
9519
}
9533
9520
if ( ! params . limit ) {
9534
- params . limit = 50 ;
9521
+ params . limit = qm . userVariables . defaultLimit ;
9535
9522
}
9536
9523
params = qm . api . addGlobalParams ( params ) ;
9537
9524
var cacheKey = 'getUserVariablesFromApi' ;
0 commit comments