diff --git a/client/notifications/notification.js b/client/notifications/notification.js index 6adec5a6e035..6ad4ada930c5 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()) { @@ -55,6 +67,16 @@ Meteor.startup(function() { KonchatNotification.newMessage(notification.payload.rid); } }); + + CachedChatSubscription.onSyncData = function(action, sub) { + if (action !== 'removed') { + notifyNewRoom(sub); + } + }; + + 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..88f369f83a52 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); @@ -206,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; } @@ -268,12 +272,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; } 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..fd0cfbac7f22 100644 --- a/packages/rocketchat-ui/client/lib/notification.js +++ b/packages/rocketchat-ui/client/lib/notification.js @@ -124,31 +124,38 @@ const KonchatNotification = { } }; -Tracker.autorun(function() { - const user = Meteor.user(); - 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 (!Session.equals(`user_${ Meteor.userId() }_status`, 'busy') && 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;