From 21b915f209b8b291f1efad798c89b403bf24aa27 Mon Sep 17 00:00:00 2001 From: riking Date: Thu, 23 Apr 2015 13:39:11 -0700 Subject: [PATCH] Suggested changes --- .../controllers/notifications.js.es6 | 4 +- .../subscribe-user-notifications.js.es6 | 2 +- .../lib/desktop-notifications.js.es6 | 42 ++++++++----------- 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/app/assets/javascripts/discourse/controllers/notifications.js.es6 b/app/assets/javascripts/discourse/controllers/notifications.js.es6 index 280db96cc9a..f0ae8ad42e7 100644 --- a/app/assets/javascripts/discourse/controllers/notifications.js.es6 +++ b/app/assets/javascripts/discourse/controllers/notifications.js.es6 @@ -1,7 +1,5 @@ -const NotificationsController = Ember.ArrayController.extend({ +export default Ember.ArrayController.extend({ needs: ['header'], loadingNotifications: Em.computed.alias('controllers.header.loadingNotifications'), myNotificationsUrl: Discourse.computed.url('/my/notifications') }); - -export default NotificationsController; 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 d82cfc87a86..7ee8088b5e5 100644 --- a/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 +++ b/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 @@ -56,7 +56,7 @@ export default { }); }); - initDesktopNotifications(container); + initDesktopNotifications(bus); } } }; diff --git a/app/assets/javascripts/discourse/lib/desktop-notifications.js.es6 b/app/assets/javascripts/discourse/lib/desktop-notifications.js.es6 index e24f325223e..45c720e3df3 100644 --- a/app/assets/javascripts/discourse/lib/desktop-notifications.js.es6 +++ b/app/assets/javascripts/discourse/lib/desktop-notifications.js.es6 @@ -1,35 +1,34 @@ -let primaryTab; -let liveEnabled; -let notificationTagName; -let mbClientId; -let lastAction; +let primaryTab = false; +let liveEnabled = false; +let mbClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"; +let lastAction = -1; const focusTrackerKey = "focus-tracker"; const seenDataKey = "seen-notifications"; const recentUpdateThreshold = 1000 * 60 * 2; // 2 minutes const idleThresholdTime = 1000 * 10; // 10 seconds const INVITED_TYPE = 8; +let notificationTagName; // "discourse-notification-popup-" + Discourse.SiteSettings.title; -function init(container) { +// Called from an initializer +function init(messageBus) { liveEnabled = false; + mbClientId = messageBus.clientId; requestPermission().then(function() { try { localStorage.getItem(focusTrackerKey); } catch (e) { Em.Logger.info('Discourse desktop notifications are disabled - localStorage denied.'); - return false; + return; } liveEnabled = true; Em.Logger.info('Discourse desktop notifications are enabled.'); - return true; - }).then(function(c) { - if (c) { - try { - init2(container); - } catch (e) { - Em.Logger.error(e); - } + try { + // Permission is granted, continue with setup + setupNotifications(); + } catch (e) { + Em.Logger.error(e); } }).catch(function() { liveEnabled = false; @@ -37,7 +36,8 @@ function init(container) { }); } -function init2(container) { +// This function is only called if permission was granted +function setupNotifications() { // Load up the current state of the notifications const seenData = JSON.parse(localStorage.getItem(seenDataKey)); let markAllSeen = true; @@ -59,8 +59,6 @@ function init2(container) { notificationTagName = "discourse-notification-popup-" + Discourse.SiteSettings.title; - const messageBus = container.lookup('message-bus:main'); - mbClientId = messageBus.clientId; window.addEventListener("storage", function(e) { // note: This event only fires when other tabs setItem() @@ -68,9 +66,7 @@ function init2(container) { if (key !== focusTrackerKey) { return true; } - if (primaryTab) { - primaryTab = false; - } + primaryTab = false; }); window.addEventListener("focus", function() { @@ -150,10 +146,6 @@ function onNotification(currentUser) { tag: notificationTagName }); - // if (enableSound) { - // soundElement.play(); - // } - const firstUnseen = unseen[0]; function clickEventHandler() {