diff --git a/client/notifications/notification.coffee b/client/notifications/notification.coffee index 2bb8c584fc2a8dfb6f019617a2666e81aaca6d71..42b4bfce67a7d28b8bdf499b93cd02ed4e0aafee 100644 --- a/client/notifications/notification.coffee +++ b/client/notifications/notification.coffee @@ -1,3 +1,21 @@ +# 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. + Meteor.startup -> - RocketChat.Notifications.onUser 'notification', (data) -> - KonchatNotification.showDesktop data + RocketChat.Notifications.onUser 'notification', (notification) -> + + openedRoomId = undefined + if FlowRouter.getRouteName() in ['channel', 'group', 'direct'] + openedRoomId = Session.get 'openedRoom' + + # This logic is duplicated in /client/startup/unread.coffee. + hasFocus = readMessage.isEnable() + messageIsInOpenedRoom = openedRoomId is notification.payload.rid + + if !(hasFocus and messageIsInOpenedRoom) + # Play a sound. + KonchatNotification.newMessage() + + # Show a notification. + KonchatNotification.showDesktop notification diff --git a/client/startup/unread.coffee b/client/startup/unread.coffee index fd302b6e5edc52f3f453832e270ec97b4f2063e6..8b251305ab0c2c1e74dada3dd25b44c1b42ec35d 100644 --- a/client/startup/unread.coffee +++ b/client/startup/unread.coffee @@ -1,10 +1,3 @@ -Meteor.startup -> - - ChatSubscription.find({}, { fields: { unread: 1 } }).observeChanges - changed: (id, fields) -> - if fields.unread and fields.unread > 0 - KonchatNotification.newMessage() - Meteor.startup -> Tracker.autorun -> @@ -14,18 +7,26 @@ Meteor.startup -> subscriptions = ChatSubscription.find({open: true}, { fields: { unread: 1, alert: 1, rid: 1, t: 1, name: 1, ls: 1 } }) - rid = undefined + openedRoomId = undefined Tracker.nonreactive -> if FlowRouter.getRouteName() in ['channel', 'group', 'direct'] - rid = Session.get 'openedRoom' + openedRoomId = Session.get 'openedRoom' for subscription in subscriptions.fetch() - if subscription.rid is rid and (subscription.alert or subscription.unread > 0) - readMessage.readNow() - else - unreadCount += subscription.unread - if subscription.alert is true - unreadAlert = '•' + + if subscription.alert or subscription.unread > 0 + # This logic is duplicated in /client/notifications/notification.coffee. + hasFocus = readMessage.isEnable() + subscriptionIsTheOpenedRoom = openedRoomId is subscription.rid + if hasFocus and subscriptionIsTheOpenedRoom + # The user has probably read all messages in this room. + # TODO: readNow() should return whether it has actually marked the room as read. + readMessage.readNow() + else + # Increment the total unread count. + unreadCount += subscription.unread + if subscription.alert is true + unreadAlert = '•' readMessage.refreshUnreadMark(subscription.rid)