From 096672ed85d7a3ef59622e327a75e643d7c34ecb Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Thu, 14 Sep 2017 11:48:29 -0300 Subject: [PATCH 1/3] Fix new room sound being played too much --- client/notifications/notification.js | 11 +++++++++++ .../client/accountBox.js | 2 +- .../client/chatRoomItem.js | 12 ------------ .../rocketchat-ui-sidenav/client/sideNav.html | 2 +- .../rocketchat-ui-sidenav/client/sideNav.js | 19 ++++++++++--------- .../rocketchat-ui/client/lib/notification.js | 9 +++++++-- 6 files changed, 30 insertions(+), 25 deletions(-) diff --git a/client/notifications/notification.js b/client/notifications/notification.js index 6adec5a6e035..c5aff3fd6783 100644 --- a/client/notifications/notification.js +++ b/client/notifications/notification.js @@ -55,6 +55,17 @@ Meteor.startup(function() { KonchatNotification.newMessage(notification.payload.rid); } }); + + RocketChat.Notifications.onUser('subscriptions-changed', function(action, sub) { + // Do not play new room sound if user is busy + if (Session.equals(`user_${ Meteor.userId() }_status`, 'busy')) { + return; + } + + if (!(FlowRouter.getParam('name') && FlowRouter.getParam('name') === sub.name) && !sub.ls && sub.alert === true) { + return KonchatNotification.newRoom(sub.rid); + } + }); } }); }); diff --git a/packages/rocketchat-ui-sidenav/client/accountBox.js b/packages/rocketchat-ui-sidenav/client/accountBox.js index 4ce10ac67b1c..6c112705f442 100644 --- a/packages/rocketchat-ui-sidenav/client/accountBox.js +++ b/packages/rocketchat-ui-sidenav/client/accountBox.js @@ -29,7 +29,7 @@ Template.accountBox.helpers({ }, isAnonymous() { - if (Meteor.user() == null && RocketChat.settings.get('Accounts_AllowAnonymousRead')) { + if (Meteor.userId() == null && RocketChat.settings.get('Accounts_AllowAnonymousRead')) { return 'disabled'; } } diff --git a/packages/rocketchat-ui-sidenav/client/chatRoomItem.js b/packages/rocketchat-ui-sidenav/client/chatRoomItem.js index 44b00e6588f6..877f4290e8af 100644 --- a/packages/rocketchat-ui-sidenav/client/chatRoomItem.js +++ b/packages/rocketchat-ui-sidenav/client/chatRoomItem.js @@ -1,5 +1,3 @@ -/* globals KonchatNotification */ - Template.chatRoomItem.helpers({ roomData() { let name = this.name; @@ -25,10 +23,6 @@ Template.chatRoomItem.helpers({ if (!this.hideUnreadStatus && (FlowRouter.getParam('_id') !== this.rid || !document.hasFocus()) && this.alert) { alertClass = 'sidebar-item__link--active'; } - // Sound notification - if (!(FlowRouter.getParam('name') === this.name) && !this.ls && this.alert === true) { - KonchatNotification.newRoom(this.rid); - } const icon = RocketChat.roomTypes.getIcon(this.t); const avatar = !icon; @@ -47,9 +41,3 @@ Template.chatRoomItem.helpers({ }; } }); - -Template.chatRoomItem.onRendered = function() { - if (!(FlowRouter.getParam('name') && (FlowRouter.getParam('name') === this.name)) && !this.ls && (this.alert === true)) { - return KonchatNotification.newRoom(this.rid); - } -}; diff --git a/packages/rocketchat-ui-sidenav/client/sideNav.html b/packages/rocketchat-ui-sidenav/client/sideNav.html index 35d3ec88aefd..55bf3323b7f6 100644 --- a/packages/rocketchat-ui-sidenav/client/sideNav.html +++ b/packages/rocketchat-ui-sidenav/client/sideNav.html @@ -5,7 +5,7 @@ {{> toolbar}} - {{#if currentUser}} + {{#if loggedInUser}} diff --git a/packages/rocketchat-ui-sidenav/client/sideNav.js b/packages/rocketchat-ui-sidenav/client/sideNav.js index cecd50972f1c..76415343588a 100644 --- a/packages/rocketchat-ui-sidenav/client/sideNav.js +++ b/packages/rocketchat-ui-sidenav/client/sideNav.js @@ -1,14 +1,6 @@ /* globals menu*/ Template.sideNav.helpers({ - hasUnread() { - const user = Meteor.user(); - return user && user.settings && user.settings.preferences && user.settings.preferences.roomsListExhibitionMode === 'unread'; - }, - sortByActivity() { - const user = Meteor.user(); - return user && user.settings && user.settings.preferences && user.settings.preferences.roomsListExhibitionMode === 'activity'; - }, flexTemplate() { return SideNav.getFlex().template; }, @@ -23,6 +15,10 @@ Template.sideNav.helpers({ roomType() { return RocketChat.roomTypes.getTypes(); + }, + + loggedInUser() { + return !!Meteor.userId(); } }); @@ -69,7 +65,12 @@ Template.sideNav.onCreated(function() { this.mergedChannels = new ReactiveVar(false); this.autorun(() => { - const user = Meteor.user(); + const user = RocketChat.models.Users.findOne(Meteor.userId(), { + fields: { + 'settings.preferences.roomsListExhibitionMode': 1, + 'settings.preferences.mergeChannels': 1 + } + }); let userPref = null; if (user && user.settings && user.settings.preferences) { userPref = user.settings.preferences.roomsListExhibitionMode === 'category' && user.settings.preferences.mergeChannels; diff --git a/packages/rocketchat-ui/client/lib/notification.js b/packages/rocketchat-ui/client/lib/notification.js index af8519ecfcb7..be073f49416c 100644 --- a/packages/rocketchat-ui/client/lib/notification.js +++ b/packages/rocketchat-ui/client/lib/notification.js @@ -125,13 +125,18 @@ const KonchatNotification = { }; Tracker.autorun(function() { - const user = Meteor.user(); + const user = RocketChat.models.Users.findOne(Meteor.userId(), { + fields: { + 'settings.preferences.newRoomNotification': 1, + 'settings.preferences.notificationsSoundVolume': 1 + } + }); const newRoomNotification = user && user.settings && user.settings.preferences && user.settings.preferences.newRoomNotification || 'door'; const audioVolume = user && user.settings && user.settings.preferences && user.settings.preferences.notificationsSoundVolume || 100; if ((Session.get('newRoomSound') || []).length > 0) { Tracker.nonreactive(function() { - if (!Session.equals(`user_${ Meteor.userId() }_status`, 'busy') && newRoomNotification !== 'none') { + if (newRoomNotification !== 'none') { const [audio] = $(`audio#${ newRoomNotification }`); if (audio && audio.play) { audio.volume = Number((audioVolume/100).toPrecision(2)); From dc0aa06651453e41a1cb58814ddb6418f80d2f7e Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Fri, 15 Sep 2017 11:48:46 -0300 Subject: [PATCH 2/3] Notify new room on initial sync data as well --- client/notifications/notification.js | 27 +++++++++++++------ .../client/lib/cachedCollection.js | 8 +++++- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/client/notifications/notification.js b/client/notifications/notification.js index c5aff3fd6783..167b6e5962a6 100644 --- a/client/notifications/notification.js +++ b/client/notifications/notification.js @@ -1,9 +1,21 @@ -/* globals KonchatNotification, fireGlobalEvent, readMessage */ +/* globals KonchatNotification, fireGlobalEvent, readMessage, CachedChatSubscription */ // Show notifications and play a sound for new messages. // We trust the server to only send notifications for interesting messages, e.g. direct messages or // group messages in which the user is mentioned. +function notifyNewRoom(sub) { + + // Do not play new room sound if user is busy + if (Session.equals(`user_${ Meteor.userId() }_status`, 'busy')) { + return; + } + + if (!(FlowRouter.getParam('name') && FlowRouter.getParam('name') === sub.name) && !sub.ls && sub.alert === true) { + return KonchatNotification.newRoom(sub.rid); + } +} + Meteor.startup(function() { Tracker.autorun(function() { if (Meteor.userId()) { @@ -56,15 +68,14 @@ Meteor.startup(function() { } }); - RocketChat.Notifications.onUser('subscriptions-changed', function(action, sub) { - // Do not play new room sound if user is busy - if (Session.equals(`user_${ Meteor.userId() }_status`, 'busy')) { - return; + CachedChatSubscription.onSyncData = function(action, sub) { + if (action === 'changed') { + notifyNewRoom(sub); } + }; - if (!(FlowRouter.getParam('name') && FlowRouter.getParam('name') === sub.name) && !sub.ls && sub.alert === true) { - return KonchatNotification.newRoom(sub.rid); - } + RocketChat.Notifications.onUser('subscriptions-changed', (action, sub) => { + notifyNewRoom(sub); }); } }); diff --git a/packages/rocketchat-lib/client/lib/cachedCollection.js b/packages/rocketchat-lib/client/lib/cachedCollection.js index beb3d4995fe2..497d120d7c12 100644 --- a/packages/rocketchat-lib/client/lib/cachedCollection.js +++ b/packages/rocketchat-lib/client/lib/cachedCollection.js @@ -99,7 +99,8 @@ class CachedCollection { useCache = true, debug = false, version = 6, - maxCacheTime = 60*60*24*30 + maxCacheTime = 60*60*24*30, + onSyncData = (/* action, record */) => {} }) { this.collection = collection || new Mongo.Collection(null); @@ -116,6 +117,7 @@ class CachedCollection { this.userRelated = userRelated; this.updatedAt = new Date(0); this.maxCacheTime = maxCacheTime; + this.onSyncData = onSyncData; RocketChat.CachedCollectionManager.register(this); @@ -268,12 +270,16 @@ class CachedCollection { if (record._deletedAt) { this.collection.remove({ _id: record._id }); + this.onSyncData('removed', record); + if (record._deletedAt && record._deletedAt > this.updatedAt) { this.updatedAt = record._deletedAt; } } else { this.collection.upsert({ _id: record._id }, _.omit(record, '_id')); + this.onSyncData('changed', record); + if (record._updatedAt && record._updatedAt > this.updatedAt) { this.updatedAt = record._updatedAt; } From ffb24806aea22c6dda3a99ba70c670cfe7d78a99 Mon Sep 17 00:00:00 2001 From: Diego Sampaio Date: Fri, 15 Sep 2017 17:03:16 -0300 Subject: [PATCH 3/3] Plays new room sound on first subscriptions load (empty cache) --- client/notifications/notification.js | 4 +- .../client/lib/cachedCollection.js | 2 + .../rocketchat-ui/client/lib/notification.js | 54 ++++++++++--------- 3 files changed, 32 insertions(+), 28 deletions(-) diff --git a/client/notifications/notification.js b/client/notifications/notification.js index 167b6e5962a6..6ad4ada930c5 100644 --- a/client/notifications/notification.js +++ b/client/notifications/notification.js @@ -11,7 +11,7 @@ function notifyNewRoom(sub) { return; } - if (!(FlowRouter.getParam('name') && FlowRouter.getParam('name') === sub.name) && !sub.ls && sub.alert === true) { + if ((!FlowRouter.getParam('name') || FlowRouter.getParam('name') !== sub.name) && !sub.ls && sub.alert === true) { return KonchatNotification.newRoom(sub.rid); } } @@ -69,7 +69,7 @@ Meteor.startup(function() { }); CachedChatSubscription.onSyncData = function(action, sub) { - if (action === 'changed') { + if (action !== 'removed') { notifyNewRoom(sub); } }; diff --git a/packages/rocketchat-lib/client/lib/cachedCollection.js b/packages/rocketchat-lib/client/lib/cachedCollection.js index 497d120d7c12..88f369f83a52 100644 --- a/packages/rocketchat-lib/client/lib/cachedCollection.js +++ b/packages/rocketchat-lib/client/lib/cachedCollection.js @@ -208,6 +208,8 @@ class CachedCollection { delete record.$loki; this.collection.upsert({ _id: record._id }, _.omit(record, '_id')); + this.onSyncData('changed', record); + if (record._updatedAt && record._updatedAt > this.updatedAt) { this.updatedAt = record._updatedAt; } diff --git a/packages/rocketchat-ui/client/lib/notification.js b/packages/rocketchat-ui/client/lib/notification.js index be073f49416c..fd0cfbac7f22 100644 --- a/packages/rocketchat-ui/client/lib/notification.js +++ b/packages/rocketchat-ui/client/lib/notification.js @@ -124,36 +124,38 @@ const KonchatNotification = { } }; -Tracker.autorun(function() { - const user = RocketChat.models.Users.findOne(Meteor.userId(), { - fields: { - 'settings.preferences.newRoomNotification': 1, - 'settings.preferences.notificationsSoundVolume': 1 - } - }); - const newRoomNotification = user && user.settings && user.settings.preferences && user.settings.preferences.newRoomNotification || 'door'; - const audioVolume = user && user.settings && user.settings.preferences && user.settings.preferences.notificationsSoundVolume || 100; +Meteor.startup(() => { + Tracker.autorun(function() { + const user = RocketChat.models.Users.findOne(Meteor.userId(), { + fields: { + 'settings.preferences.newRoomNotification': 1, + 'settings.preferences.notificationsSoundVolume': 1 + } + }); + const newRoomNotification = user && user.settings && user.settings.preferences && user.settings.preferences.newRoomNotification || 'door'; + const audioVolume = user && user.settings && user.settings.preferences && user.settings.preferences.notificationsSoundVolume || 100; - if ((Session.get('newRoomSound') || []).length > 0) { - Tracker.nonreactive(function() { - if (newRoomNotification !== 'none') { - const [audio] = $(`audio#${ newRoomNotification }`); - if (audio && audio.play) { - audio.volume = Number((audioVolume/100).toPrecision(2)); - return audio.play(); + if ((Session.get('newRoomSound') || []).length > 0) { + Meteor.defer(function() { + if (newRoomNotification !== 'none') { + const [audio] = $(`audio#${ newRoomNotification }`); + if (audio && audio.play) { + audio.volume = Number((audioVolume/100).toPrecision(2)); + return audio.play(); + } } + }); + } else { + const [room] = $(`audio#${ newRoomNotification }`); + if (!room) { + return; + } + if (room.pause) { + room.pause(); + return room.currentTime = 0; } - }); - } else { - const [room] = $(`audio#${ newRoomNotification }`); - if (!room) { - return; - } - if (room.pause) { - room.pause(); - return room.currentTime = 0; } - } + }); }); export { KonchatNotification }; this.KonchatNotification = KonchatNotification;