diff --git a/client/app/core/appliance-info.service.js b/client/app/core/appliance-info.service.js index 7dd175fe0..3e6b1cd29 100644 --- a/client/app/core/appliance-info.service.js +++ b/client/app/core/appliance-info.service.js @@ -23,7 +23,8 @@ export function ApplianceInfo ($sessionStorage) { role: data.identity.role, suiVersion: gitHash.gitCommit, miqVersion: data.server_info.version + '.' + data.server_info.build, - server: data.server_info.appliance + server: data.server_info.appliance, + asyncNotify: data.settings.asynchronous_notifications || true } $sessionStorage.applianceInfo = applianceInfo } diff --git a/client/app/core/appliance-info.service.spec.js b/client/app/core/appliance-info.service.spec.js index f42601c81..96679432d 100644 --- a/client/app/core/appliance-info.service.spec.js +++ b/client/app/core/appliance-info.service.spec.js @@ -40,7 +40,8 @@ describe('Appliance Info Service', () => { 'role': 'EvmRole-super_administrator', 'miqVersion': 'master.20170510164252_9e5df30', 'suiVersion': '', - 'server': 'EVM' + 'server': 'EVM', + 'asyncNotify': 'true' } ApplianceInfo.set(applianceInfoData) const currentApplianceInfo = ApplianceInfo.get() diff --git a/client/app/core/authorization.config.js b/client/app/core/authorization.config.js index 0dac49017..6f80f8405 100644 --- a/client/app/core/authorization.config.js +++ b/client/app/core/authorization.config.js @@ -91,10 +91,6 @@ export function authInit ($rootScope, $state, $log, Session, $sessionStorage, La } syncSession() .then(rbacReloadOrLogin(toState, toParams)) - .then(() => { - const cable = ActionCable.createConsumer('/ws/notifications') - cable.subscriptions.create('NotificationChannel', {}) - }) .catch(badUser) } diff --git a/client/app/core/event-notifications.service.js b/client/app/core/event-notifications.service.js index 527b76de9..d51c1a3df 100644 --- a/client/app/core/event-notifications.service.js +++ b/client/app/core/event-notifications.service.js @@ -1,5 +1,6 @@ /** @ngInject */ -export function EventNotificationsFactory ($timeout, lodash, CollectionsApi, RBAC) { +export function EventNotificationsFactory ($log, $timeout, lodash, CollectionsApi, RBAC, ApplianceInfo, ActionCable, + Session) { const state = {} const toastDelay = 8 * 1000 const service = { @@ -21,6 +22,7 @@ export function EventNotificationsFactory ($timeout, lodash, CollectionsApi, RBA } notificationsInit() + actionCableInit() return service @@ -233,6 +235,26 @@ export function EventNotificationsFactory ($timeout, lodash, CollectionsApi, RBA } } + function actionCableInit () { + if (ApplianceInfo.get().asyncNotify) { + const cable = ActionCable.createConsumer('/ws/notifications') + cable.subscriptions.create('NotificationChannel', { + disconnected: () => { + const vm = this + Session.requestWsToken().then(null, () => { + $log.warn('Unable to retrieve a valid ws_token!') + // Disconnect permanently if the ws_token cannot be fetched + vm.consumer.connection.close({allowReconnect: false}) + }) + }, + received: (data) => { + const msg = miqFormatNotification(data.text, data.bindings) + add(data.level, data.level === 'error' ? 'danger' : data.level, msg, {message: msg}, data.id) + } + }) + } + } + function removeToast (notification) { const index = state.toastNotifications.indexOf(notification) if (index > -1) { diff --git a/client/app/states/login/login.state.js b/client/app/states/login/login.state.js index b3a9075ba..6aa0cd54f 100644 --- a/client/app/states/login/login.state.js +++ b/client/app/states/login/login.state.js @@ -74,10 +74,6 @@ function StateController ($window, $state, Text, RBAC, API_LOGIN, API_PASSWORD, Session.destroy() } }) - .then(() => { - const cable = ActionCable.createConsumer('/ws/notifications') - cable.subscriptions.create('NotificationChannel', {}) - }) .catch((response) => { if (response.status === 401) { vm.credentials.login = ''