@@ -5517,7 +5517,7 @@ var qm = {
55175517 refreshIfEmpty : function ( successHandler , errorHandler ) {
55185518 if ( ! qm . notifications . getNumberInGlobalsOrLocalStorage ( ) ) {
55195519 qm . qmLog . info ( 'No notifications in local storage' ) ;
5520- qm . notifications . refreshNotifications ( successHandler , errorHandler ) ;
5520+ qm . notifications . syncTrackingReminderNotifications ( successHandler , errorHandler ) ;
55215521 return true ;
55225522 }
55235523 qm . qmLog . info ( qm . notifications . getNumberInGlobalsOrLocalStorage ( ) + ' notifications in local storage' ) ;
@@ -5528,7 +5528,7 @@ var qm = {
55285528 qm . qmLog . info ( "qm.notifications.refreshIfEmptyOrStale" ) ;
55295529 if ( ! qm . notifications . getNumberInGlobalsOrLocalStorage ( ) || qm . notifications . getSecondsSinceLastNotificationsRefresh ( ) > 3600 ) {
55305530 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 ) ;
55325532 } else {
55335533 qm . qmLog . info ( 'Not refreshing notifications because last refresh was last than an hour ago and we have notifications in local storage' ) ;
55345534 if ( callback ) {
@@ -5620,39 +5620,16 @@ var qm = {
56205620 } else {
56215621 console . info ( 'No rating notifications for popup' ) ;
56225622 qm . notifications . getLastNotificationsRefreshTime ( ) ;
5623- qm . notifications . refreshNotifications ( ) ;
5623+ qm . notifications . syncTrackingReminderNotifications ( ) ;
56245624 return null ;
56255625 }
56265626 } ,
56275627 deleteByVariableName : function ( variableName ) {
56285628 return qm . storage . deleteByProperty ( qm . items . trackingReminderNotifications , 'variableName' , variableName ) ;
56295629 } ,
56305630 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- } ,
56545631 refreshAndShowPopupIfNecessary : function ( notificationParams ) {
5655- qm . notifications . refreshNotifications ( notificationParams , function ( trackingReminderNotifications ) {
5632+ qm . notifications . syncTrackingReminderNotifications ( function ( response ) {
56565633 var uniqueNotification = qm . notifications . getMostRecentUniqueNotificationNotInSyncQueue ( ) ;
56575634 function objectLength ( obj ) {
56585635 var result = 0 ;
@@ -5664,6 +5641,7 @@ var qm = {
56645641 }
56655642 return result ;
56665643 }
5644+ var trackingReminderNotifications = qm . notifications . getFromGlobalsOrLocalStorage ( ) ;
56675645 var numberOfWaitingNotifications = objectLength ( trackingReminderNotifications ) ;
56685646 if ( uniqueNotification ) {
56695647 function getChromeRatingNotificationParams ( trackingReminderNotification ) {
@@ -5769,7 +5747,7 @@ var qm = {
57695747 if ( ! successHandler ) {
57705748 return null ;
57715749 }
5772- qm . notifications . refreshNotifications ( function ( notifications ) {
5750+ qm . notifications . syncTrackingReminderNotifications ( function ( response ) {
57735751 var notification = qm . notifications . getMostRecentNotification ( ) ;
57745752 if ( notification ) {
57755753 successHandler ( notification ) ;
@@ -5781,7 +5759,7 @@ var qm = {
57815759 schedulePost : function ( delayInMilliseconds ) {
57825760 var queue = qm . storage . getItem ( qm . items . notificationsSyncQueue ) ;
57835761 if ( queue && queue . length > 10 ) {
5784- qm . notifications . post ( ) ;
5762+ qm . notifications . syncTrackingReminderNotifications ( ) ;
57855763 return ;
57865764 }
57875765 if ( ! delayInMilliseconds ) {
@@ -5797,7 +5775,7 @@ var qm = {
57975775 }
57985776 setTimeout ( function ( ) {
57995777 qm . qmLog . info ( "Notifications sync countdown completed. Syncing now... " ) ;
5800- qm . notifications . post ( ) ;
5778+ qm . notifications . syncTrackingReminderNotifications ( ) ;
58015779 } , delayInMilliseconds ) ;
58025780 } else {
58035781 if ( ! qm . platform . isMobile ( ) ) { // Better performance
@@ -5826,8 +5804,8 @@ var qm = {
58265804 qm . notifications . deleteByVariableName ( n . variableName ) ;
58275805 qm . notifications . addToSyncQueue ( n ) ;
58285806 } ,
5829- post : function ( successHandler , errorHandler ) {
5830- qm . qmLog . debug ( "Called postNotifications ..." ) ;
5807+ syncTrackingReminderNotifications : function ( successHandler , errorHandler ) {
5808+ qm . qmLog . debug ( "Called syncTrackingReminderNotifications ..." ) ;
58315809 var notifications = qm . storage . getItem ( qm . items . notificationsSyncQueue ) ;
58325810 qm . storage . removeItem ( qm . items . notificationsSyncQueue ) ;
58335811 qm . storage . removeItem ( qm . items . trackingReminderNotificationSyncScheduled ) ;
@@ -5851,21 +5829,29 @@ var qm = {
58515829 } )
58525830 qm . api . postToQuantiModo ( body , 'v3/trackingReminderNotifications' ,
58535831 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 ;
58565842 if ( measurements ) { qm . measurements . addMeasurementsToMemory ( measurements ) ; }
5843+ var trackingReminderNotifications = response . trackingReminderNotifications || response . data . trackingReminderNotifications ;
5844+ if ( trackingReminderNotifications ) { qm . storage . setTrackingReminderNotifications ( notifications ) ; }
58575845 if ( successHandler ) { successHandler ( response ) ; }
58585846 } , 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 ) ; }
58695855 } ) ;
58705856 } ,
58715857 skip : function ( trackingReminderNotification ) {
@@ -9519,6 +9505,7 @@ var qm = {
95199505 }
95209506 } ,
95219507 userVariables : {
9508+ defaultLimit : 20 ,
95229509 updateLatestMeasurementTime : function ( variableName , lastValue ) {
95239510 qm . storage . getUserVariableByName ( variableName , true , lastValue ) ;
95249511 } ,
@@ -9531,7 +9518,7 @@ var qm = {
95319518 params . sort = '-latestMeasurementTime' ;
95329519 }
95339520 if ( ! params . limit ) {
9534- params . limit = 50 ;
9521+ params . limit = qm . userVariables . defaultLimit ;
95359522 }
95369523 params = qm . api . addGlobalParams ( params ) ;
95379524 var cacheKey = 'getUserVariablesFromApi' ;
0 commit comments