Skip to content

Commit d9afdd0

Browse files
authored
Merge pull request #4048 from QuantiModo/feature/post-notifications
Avoid infinite posting loop
2 parents aef26df + 7760f5f commit d9afdd0

File tree

6 files changed

+53
-164
lines changed

6 files changed

+53
-164
lines changed

src/js/controllers/appCtrl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ angular.module('starter')// Parent Controller - This controller runs before ever
1414
});
1515
$scope.$on('$ionicView.afterEnter', function(e){
1616
qmLog.debug($scope.controller_name + ".afterEnter so posting queued notifications if any");
17-
qm.notifications.post();
17+
qmService.syncTrackingReminderNotifications();
1818
qmService.refreshUserUsingAccessTokenInUrlIfNecessary();
1919
$rootScope.setMicAndSpeechEnabled(qm.mic.getMicEnabled());
2020
qm.chatButton.setZohoChatButtonZIndex();

src/js/controllers/remindersInboxCtrl.js

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,15 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state",
6060
});
6161
$scope.stateParams = $stateParams;
6262
qmService.actionSheet.setDefaultActionSheet(function(){
63-
$scope.refreshTrackingReminderNotifications();
63+
$scope.syncTrackingReminderNotifications();
6464
}, getVariableCategoryName());
6565
qmService.splash.hideSplashScreen();
6666
});
6767
$scope.$on('$ionicView.afterEnter', function(){
6868
qmLog.info('RemindersInboxCtrl afterEnter: ' + window.location.href);
6969
setPageTitle(); // Setting title afterEnter doesn't fix cutoff on Android
7070
if(needToRefresh()){
71-
$scope.refreshTrackingReminderNotifications();
71+
$scope.syncTrackingReminderNotifications();
7272
}
7373
if($rootScope.platform.isWeb){
7474
qm.webNotifications.registerServiceWorker();
@@ -77,7 +77,7 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state",
7777
});
7878
$scope.$on('$ionicView.beforeLeave', function(){
7979
qmLog.debug('RemindersInboxCtrl beforeLeave');
80-
qm.notifications.post();
80+
qmService.syncTrackingReminderNotifications();
8181
});
8282
$scope.$on('$ionicView.afterLeave', function(){
8383
qmLog.debug('RemindersInboxCtrl afterLeave');
@@ -105,12 +105,11 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state",
105105
function autoRefresh(){
106106
$timeout(function(){
107107
if($state.current.name.toLowerCase().indexOf('inbox') !== -1){
108-
$scope.refreshTrackingReminderNotifications();
108+
$scope.syncTrackingReminderNotifications();
109109
autoRefresh();
110110
}
111111
}, 30 * 60 * 1000);
112112
}
113-
114113
var setPageTitle = function(){
115114
if(getVariableCategoryName() === 'Treatments'){
116115
$scope.state.title = 'Overdue Meds';
@@ -148,7 +147,7 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state",
148147
if(qm.notifications.getNumberInGlobalsOrLocalStorage(cat)){
149148
getTrackingReminderNotifications();
150149
}else{
151-
$scope.refreshTrackingReminderNotifications();
150+
$scope.syncTrackingReminderNotifications();
152151
}
153152
}
154153
}
@@ -178,17 +177,6 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state",
178177
});
179178
}
180179
}
181-
function refreshNotificationsForCategory(cat) {
182-
qmLog.info('Falling back to getTrackingReminderNotificationsFromApi request for category ' + cat);
183-
qmService.refreshTrackingReminderNotifications({
184-
variableCategoryName: cat,
185-
onlyPast: true
186-
}, function (response) {
187-
qmLog.info('getTrackingReminderNotificationsFromApi response for ' + cat +
188-
': ' + JSON.stringify(response));
189-
addNotificationsToScope(response.data)
190-
});
191-
}
192180
function getNumberOfDisplayedNotifications() {
193181
var total = 0;
194182
var dividers = $scope.notificationDividers;
@@ -232,15 +220,13 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state",
232220
function getFallbackInboxContentIfNecessary(){
233221
var num = getNumberOfDisplayedNotifications();
234222
if(!num && !$scope.state.loading){
235-
var cat = getVariableCategoryName();
236-
if(cat){refreshNotificationsForCategory(cat);}
237223
getFavorites();
238224
getDiscoveries();
239225
}
240226
}
241227
var closeWindowIfNecessary = function(){
242228
if($state.current.name === "app.remindersInboxCompact" && !getNumberOfDisplayedNotifications()){
243-
$scope.refreshTrackingReminderNotifications();
229+
$scope.syncTrackingReminderNotifications();
244230
window.close();
245231
}
246232
};
@@ -432,19 +418,6 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state",
432418
logNotificationDividers($scope.notificationDividers);
433419
})
434420
}
435-
var getFilteredTodayTrackingReminderNotifications = function(){
436-
qmService.getTodayTrackingReminderNotificationsDeferred(getVariableCategoryName())
437-
.then(function(trackingReminderNotifications){
438-
addNotificationsToScope(trackingReminderNotifications)
439-
getFallbackInboxContentIfNecessary();
440-
hideInboxLoader();
441-
}, function(error){
442-
getFallbackInboxContentIfNecessary();
443-
qmLog.error(error);
444-
qmLog.error('failed to get reminder notifications!');
445-
hideInboxLoader();
446-
});
447-
};
448421
$rootScope.$on('broadcastGetTrackingReminderNotifications', function(){
449422
qmLog.info('getTrackingReminderNotifications broadcast received..');
450423
getFilteredTrackingReminderNotificationsFromLocalStorage();
@@ -465,16 +438,16 @@ angular.module('starter').controller('RemindersInboxCtrl', ["$scope", "$state",
465438
}
466439
}, 10000);
467440
};
468-
$scope.refreshTrackingReminderNotifications = function(params){
441+
$scope.syncTrackingReminderNotifications = function(params){
469442
showLoader();
470-
qmService.refreshTrackingReminderNotifications(params).then(function(){
443+
qmService.syncTrackingReminderNotifications(params).then(function(){
471444
getTrackingReminderNotifications();
472445
if(!getNumberOfDisplayedNotifications()){
473446
getFallbackInboxContentIfNecessary();
474447
}
475448
}, function(error){
476449
getFallbackInboxContentIfNecessary();
477-
qmLog.info('$scope.refreshTrackingReminderNotifications: ', error);
450+
qmLog.info('$scope.syncTrackingReminderNotifications: ', error);
478451
hideInboxLoader();
479452
});
480453
};

src/js/popup.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ document.addEventListener('DOMContentLoaded', function(){
312312
}else{
313313
qmLog.pushDebug("popup addEventListener: Calling hidePopup...");
314314
hidePopup();
315-
qm.notifications.post(updateQuestion, qm.notifications.closePopup);
315+
qm.notifications.syncTrackingReminderNotifications(updateQuestion, qm.notifications.closePopup);
316316
}
317317
qmLog.pushDebug("popup addEventListener: calling setFaceButtonListeners...");
318318
setFaceButtonListeners();

src/js/qmHelpers.js

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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.post(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.post(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,7 +5620,7 @@ var qm = {
56205620
}else{
56215621
console.info('No rating notifications for popup');
56225622
qm.notifications.getLastNotificationsRefreshTime();
5623-
qm.notifications.post();
5623+
qm.notifications.syncTrackingReminderNotifications();
56245624
return null;
56255625
}
56265626
},
@@ -5629,7 +5629,7 @@ var qm = {
56295629
},
56305630
promise: null,
56315631
refreshAndShowPopupIfNecessary: function(notificationParams){
5632-
qm.notifications.post(function(response){
5632+
qm.notifications.syncTrackingReminderNotifications(function(response){
56335633
var uniqueNotification = qm.notifications.getMostRecentUniqueNotificationNotInSyncQueue();
56345634
function objectLength(obj){
56355635
var result = 0;
@@ -5747,7 +5747,7 @@ var qm = {
57475747
if(!successHandler){
57485748
return null;
57495749
}
5750-
qm.notifications.post(function(response){
5750+
qm.notifications.syncTrackingReminderNotifications(function(response){
57515751
var notification = qm.notifications.getMostRecentNotification();
57525752
if(notification){
57535753
successHandler(notification);
@@ -5759,7 +5759,7 @@ var qm = {
57595759
schedulePost: function(delayInMilliseconds){
57605760
var queue = qm.storage.getItem(qm.items.notificationsSyncQueue);
57615761
if(queue && queue.length > 10){
5762-
qm.notifications.post();
5762+
qm.notifications.syncTrackingReminderNotifications();
57635763
return;
57645764
}
57655765
if(!delayInMilliseconds){
@@ -5775,7 +5775,7 @@ var qm = {
57755775
}
57765776
setTimeout(function(){
57775777
qm.qmLog.info("Notifications sync countdown completed. Syncing now... ");
5778-
qm.notifications.post();
5778+
qm.notifications.syncTrackingReminderNotifications();
57795779
}, delayInMilliseconds);
57805780
}else{
57815781
if(!qm.platform.isMobile()){ // Better performance
@@ -5804,8 +5804,8 @@ var qm = {
58045804
qm.notifications.deleteByVariableName(n.variableName);
58055805
qm.notifications.addToSyncQueue(n);
58065806
},
5807-
post: function(successHandler, errorHandler){
5808-
qm.qmLog.debug("Called postNotifications...");
5807+
syncTrackingReminderNotifications: function(successHandler, errorHandler){
5808+
qm.qmLog.debug("Called syncTrackingReminderNotifications...");
58095809
var notifications = qm.storage.getItem(qm.items.notificationsSyncQueue);
58105810
qm.storage.removeItem(qm.items.notificationsSyncQueue);
58115811
qm.storage.removeItem(qm.items.trackingReminderNotificationSyncScheduled);
@@ -5829,22 +5829,20 @@ var qm = {
58295829
})
58305830
qm.api.postToQuantiModo(body, 'v3/trackingReminderNotifications',
58315831
function(response){
5832-
var measurements = response.measurements;
5833-
if(!measurements && response.data){measurements = response.data.measurements;}
5832+
var measurements = response.measurements || response.data.measurements;
58345833
if(measurements){qm.measurements.addMeasurementsToMemory(measurements);}
5835-
qm.storage.setTrackingReminderNotifications(response.data.trackingReminderNotifications);
5834+
var trackingReminderNotifications = response.trackingReminderNotifications || response.data.trackingReminderNotifications;
5835+
if(trackingReminderNotifications){qm.storage.setTrackingReminderNotifications(notifications);}
58365836
if(successHandler){successHandler(response);}
58375837
}, function(response){
5838-
if(!response.success){
5839-
qm.qmLog.error(response.message)
5840-
var newNotificationsSyncQueue = qm.storage.getItem(qm.items.notificationsSyncQueue);
5841-
if(newNotificationsSyncQueue){notifications = notifications.concat(newNotificationsSyncQueue);}
5842-
qm.storage.setItem(qm.items.notificationsSyncQueue, notifications);
5843-
if(errorHandler){errorHandler(response.message || response.error);}
5844-
} else{ // This happens when the error is a message saying the notification was already deleted
5845-
// so we don't want to put notifications back in queue
5846-
qm.qmLog.warn(response.message)
5847-
}
5838+
qm.qmLog.error(response.message)
5839+
// This happens when the error is a message saying the notification was already deleted
5840+
// so we don't want to put notifications back in queue
5841+
// Don't return to queue or we cause an infinite loop if we get a no changes error
5842+
// var newNotificationsSyncQueue = qm.storage.getItem(qm.items.notificationsSyncQueue);
5843+
// if(newNotificationsSyncQueue){notifications = notifications.concat(newNotificationsSyncQueue);}
5844+
// qm.storage.setItem(qm.items.notificationsSyncQueue, notifications);
5845+
if(errorHandler){errorHandler(response.message || response.error);}
58485846
});
58495847
},
58505848
skip: function(trackingReminderNotification){

0 commit comments

Comments
 (0)