From 904d9735aba716ffa92e19cf63c5393f173a249c Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Thu, 30 Jun 2016 12:03:52 +0800 Subject: [PATCH] Refactor desktop notifications to be more modular. --- .../desktop-notification-config.js.es6 | 3 ++- .../subscribe-user-notifications.js.es6 | 11 ++++++----- .../discourse/lib/desktop-notifications.js.es6 | 10 ++++++++-- app/controllers/metadata_controller.rb | 18 +++++++++++------- 4 files changed, 27 insertions(+), 15 deletions(-) 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 dc4673c1714..d1ad4f28abb 100644 --- a/app/assets/javascripts/discourse/components/desktop-notification-config.js.es6 +++ b/app/assets/javascripts/discourse/components/desktop-notification-config.js.es6 @@ -1,7 +1,8 @@ import computed from 'ember-addons/ember-computed-decorators'; import KeyValueStore from 'discourse/lib/key-value-store'; +import { context } from 'discourse/lib/desktop-notifications'; -const keyValueStore = new KeyValueStore("discourse_desktop_notifications_"); +const keyValueStore = new KeyValueStore(context); export default Ember.Component.extend({ classNames: ['controls'], 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 406b050d3b8..1d8260f4015 100644 --- a/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 +++ b/app/assets/javascripts/discourse/initializers/subscribe-user-notifications.js.es6 @@ -1,5 +1,9 @@ // Subscribes to user events on the message bus -import { init as initDesktopNotifications, onNotification } from 'discourse/lib/desktop-notifications'; +import { + init as initDesktopNotifications, + onNotification, + alertChannel +} from 'discourse/lib/desktop-notifications'; export default { name: 'subscribe-user-notifications', @@ -98,10 +102,7 @@ export default { if (!Ember.testing) { if (!site.mobileView) { - bus.subscribe("/notification-alert/" + user.get('id'), function(data){ - onNotification(data, user); - }); - + bus.subscribe(alertChannel(user), data => onNotification(data, user)); 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 de1d6afc338..8d5c010def4 100644 --- a/app/assets/javascripts/discourse/lib/desktop-notifications.js.es6 +++ b/app/assets/javascripts/discourse/lib/desktop-notifications.js.es6 @@ -159,6 +159,12 @@ function i18nKey(notification_type) { return "notifications.popup." + Discourse.Site.current().get("notificationLookup")[notification_type]; } -// Exported for controllers/notification.js.es6 +function alertChannel(user) { + return `/notification-alert/${user.get('id')}`; +} -export { init, onNotification }; +function unsubscribe(bus, user) { + bus.unsubscribe(alertChannel(user)); +} + +export { context, init, onNotification, unsubscribe, alertChannel }; diff --git a/app/controllers/metadata_controller.rb b/app/controllers/metadata_controller.rb index a240bc10d1c..ab317885a19 100644 --- a/app/controllers/metadata_controller.rb +++ b/app/controllers/metadata_controller.rb @@ -3,7 +3,17 @@ class MetadataController < ApplicationController skip_before_filter :preload_json, :check_xhr, :redirect_to_login_if_required def manifest - manifest = { + render json: default_manifest.to_json + end + + def opensearch + render file: "#{Rails.root}/app/views/metadata/opensearch.xml" + end + + private + + def default_manifest + { name: SiteSetting.title, short_name: SiteSetting.title, display: 'standalone', @@ -19,11 +29,5 @@ class MetadataController < ApplicationController } ] } - - render json: manifest.to_json - end - - def opensearch - render file: "#{Rails.root}/app/views/metadata/opensearch.xml" end end