From f9c5cded6f3b5bc4eaa9e3460afb09fc07eacd46 Mon Sep 17 00:00:00 2001 From: Sam <sam.saffron@gmail.com> Date: Thu, 18 Feb 2016 13:20:22 +1100 Subject: [PATCH] Correct live refresh routine for notifications --- .../subscribe-user-notifications.js.es6 | 45 ++++++++++--------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 b/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 index 5923dfca8..fd820d820 100644 --- a/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 +++ b/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 @@ -49,34 +49,35 @@ export default { const oldNotifications = stale.results.get('content'); const staleIndex = _.findIndex(oldNotifications, {id: lastNotification.id}); - if (staleIndex > -1) { - oldNotifications.splice(staleIndex, 1); + if (staleIndex === -1) { + // this gets a bit tricky, uread pms are bumped to front + var insertPosition = 0; + if (lastNotification.notification_type !== 6) { + insertPosition = _.findIndex(oldNotifications, function(n){ + return n.notification_type !== 6 || n.read; + }); + insertPosition = insertPosition === -1 ? oldNotifications.length - 1 : insertPosition; + } + + oldNotifications.insertAt(insertPosition, Em.Object.create(lastNotification)); } - // this gets a bit tricky, uread pms are bumped to front - var insertPosition = 0; - if (lastNotification.notification_type !== 6) { - insertPosition = _.findIndex(oldNotifications, function(n){ - return n.notification_type !== 6 || n.read; - }); - insertPosition = insertPosition === -1 ? oldNotifications.length - 1 : insertPosition; - } + for (var idx=0; idx < data.recent.length; idx++) { + var old; + while(old = oldNotifications[idx]) { + var info = data.recent[idx]; - oldNotifications.splice(insertPosition, 0, Em.Object.create(lastNotification)); - - var idx=0; - data.recent.forEach((info)=> { - var old = oldNotifications[idx]; - if (old) { if (old.get('id') !== info[0]) { - oldNotifications.splice(idx, 1); - return; - } else if (old.get('read') !== info[1]) { - old.set('read', info[1]); + oldNotifications.removeAt(idx); + } else { + if (old.get('read') !== info[1]) { + old.set('read', info[1]); + } + break; } } - idx += 1; - }); + if ( !old ) { break; } + } } }, user.notification_channel_position);