diff --git a/app/assets/javascripts/discourse/components/desktop-notification-config.js.es6 b/app/assets/javascripts/discourse/components/desktop-notification-config.js.es6 index eb4e893f1..dc4673c17 100644 --- a/app/assets/javascripts/discourse/components/desktop-notification-config.js.es6 +++ b/app/assets/javascripts/discourse/components/desktop-notification-config.js.es6 @@ -1,22 +1,24 @@ import computed from 'ember-addons/ember-computed-decorators'; +import KeyValueStore from 'discourse/lib/key-value-store'; + +const keyValueStore = new KeyValueStore("discourse_desktop_notifications_"); export default Ember.Component.extend({ classNames: ['controls'], - @computed - notificationsPermission() { - if (this.get('isNotSupported')) return ''; - return Notification.permission; + @computed("isNotSupported") + notificationsPermission(isNotSupported) { + return isNotSupported ? "" : Notification.permission; }, @computed notificationsDisabled: { set(value) { - localStorage.setItem('notifications-disabled', value); - return localStorage.getItem('notifications-disabled'); + keyValueStore.setItem('notifications-disabled', value); + return keyValueStore.getItem('notifications-disabled'); }, get() { - return localStorage.getItem('notifications-disabled'); + return keyValueStore.getItem('notifications-disabled'); } }, @@ -25,44 +27,40 @@ export default Ember.Component.extend({ return typeof window.Notification === "undefined"; }, - isDefaultPermission: function() { - if (this.get('isNotSupported')) return false; + @computed("isNotSupported", "notificationsPermission") + isDefaultPermission(isNotSupported, notificationsPermission) { + return isNotSupported ? false : notificationsPermission === "default"; + }, - return Notification.permission === "default"; - }.property('isNotSupported', 'notificationsPermission'), + @computed("isNotSupported", "notificationsPermission") + isDeniedPermission(isNotSupported, notificationsPermission) { + return isNotSupported ? false : notificationsPermission === "denied"; + }, - isDeniedPermission: function() { - if (this.get('isNotSupported')) return false; + @computed("isNotSupported", "notificationsPermission") + isGrantedPermission(isNotSupported, notificationsPermission) { + return isNotSupported ? false : notificationsPermission === "granted"; + }, - return Notification.permission === "denied"; - }.property('isNotSupported', 'notificationsPermission'), - - isGrantedPermission: function() { - if (this.get('isNotSupported')) return false; - - return Notification.permission === "granted"; - }.property('isNotSupported', 'notificationsPermission'), - - isEnabled: function() { - if (!this.get('isGrantedPermission')) return false; - - return !this.get('notificationsDisabled'); - }.property('isGrantedPermission', 'notificationsDisabled'), + @computed("isGrantedPermission", "notificationsDisabled") + isEnabled(isGrantedPermission, notificationsDisabled) { + return isGrantedPermission ? !notificationsDisabled : false; + }, actions: { requestPermission() { - const self = this; - Notification.requestPermission(function() { - self.propertyDidChange('notificationsPermission'); - }); + Notification.requestPermission(() => this.propertyDidChange('notificationsPermission')); }, + recheckPermission() { this.propertyDidChange('notificationsPermission'); }, + turnoff() { this.set('notificationsDisabled', 'disabled'); this.propertyDidChange('notificationsPermission'); }, + turnon() { this.set('notificationsDisabled', ''); this.propertyDidChange('notificationsPermission'); diff --git a/app/assets/javascripts/discourse/lib/desktop-notifications.js.es6 b/app/assets/javascripts/discourse/lib/desktop-notifications.js.es6 index 641b721cf..b55631623 100644 --- a/app/assets/javascripts/discourse/lib/desktop-notifications.js.es6 +++ b/app/assets/javascripts/discourse/lib/desktop-notifications.js.es6 @@ -1,5 +1,6 @@ import DiscourseURL from 'discourse/lib/url'; import PageTracker from 'discourse/lib/page-tracker'; +import KeyValueStore from 'discourse/lib/key-value-store'; let primaryTab = false; let liveEnabled = false; @@ -10,6 +11,8 @@ let lastAction = -1; const focusTrackerKey = "focus-tracker"; const idleThresholdTime = 1000 * 10; // 10 seconds +const keyValueStore = new KeyValueStore("discourse_desktop_notifications_"); + // Called from an initializer function init(messageBus) { liveEnabled = false; @@ -20,7 +23,7 @@ function init(messageBus) { } try { - localStorage.getItem(focusTrackerKey); + keyValueStore.getItem(focusTrackerKey); } catch (e) { Em.Logger.info('Discourse desktop notifications are disabled - localStorage denied.'); return; @@ -66,7 +69,7 @@ function setupNotifications() { window.addEventListener("focus", function() { if (!primaryTab) { primaryTab = true; - localStorage.setItem(focusTrackerKey, mbClientId); + keyValueStore.setItem(focusTrackerKey, mbClientId); } }); @@ -74,7 +77,7 @@ function setupNotifications() { primaryTab = false; } else { primaryTab = true; - localStorage.setItem(focusTrackerKey, mbClientId); + keyValueStore.setItem(focusTrackerKey, mbClientId); } if (document) { @@ -95,7 +98,7 @@ function onNotification(data) { if (!liveEnabled) { return; } if (!primaryTab) { return; } if (!isIdle()) { return; } - if (localStorage.getItem('notifications-disabled')) { return; } + if (keyValueStore.getItem('notifications-disabled')) { return; } const notificationTitle = I18n.t(i18nKey(data.notification_type), { site_title: Discourse.SiteSettings.title,