The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Rocket.Chat/client/notifications/notification.js

90 lines
3.0 KiB

/* 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()) {
RocketChat.Notifications.onUser('notification', function(notification) {
let openedRoomId = undefined;
if (['channel', 'group', 'direct'].includes(FlowRouter.getRouteName())) {
openedRoomId = Session.get('openedRoom');
}
// This logic is duplicated in /client/startup/unread.coffee.
const hasFocus = readMessage.isEnable();
const messageIsInOpenedRoom = openedRoomId === notification.payload.rid;
const muteFocusedConversations = RocketChat.getUserPreference(Meteor.user(), 'muteFocusedConversations');
fireGlobalEvent('notification', {
notification,
fromOpenedRoom: messageIsInOpenedRoom,
hasFocus
});
if (RocketChat.Layout.isEmbedded()) {
if (!hasFocus && messageIsInOpenedRoom) {
// Play a sound and show a notification.
KonchatNotification.newMessage(notification.payload.rid);
KonchatNotification.showDesktop(notification);
}
} else {
7 years ago
if (!hasFocus || !messageIsInOpenedRoom) {
// Play a sound and show a notification.
KonchatNotification.newMessage(notification.payload.rid);
KonchatNotification.showDesktop(notification);
} else if (!muteFocusedConversations) {
// Play a notification sound
KonchatNotification.newMessage(notification.payload.rid);
}
}
});
8 years ago
RocketChat.Notifications.onUser('audioNotification', function(notification) {
8 years ago
const openedRoomId = Session.get('openedRoom');
8 years ago
// This logic is duplicated in /client/startup/unread.coffee.
const hasFocus = readMessage.isEnable();
const messageIsInOpenedRoom = openedRoomId === notification.payload.rid;
7 years ago
const muteFocusedConversations = RocketChat.getUserPreference(Meteor.user(), 'muteFocusedConversations');
8 years ago
if (RocketChat.Layout.isEmbedded()) {
if (!hasFocus && messageIsInOpenedRoom) {
// Play a notification sound
8 years ago
KonchatNotification.newMessage(notification.payload.rid);
}
7 years ago
} else if (!hasFocus || !messageIsInOpenedRoom || !muteFocusedConversations) {
// Play a notification sound
8 years ago
KonchatNotification.newMessage(notification.payload.rid);
}
});
CachedChatSubscription.onSyncData = function(action, sub) {
if (action !== 'removed') {
notifyNewRoom(sub);
}
};
RocketChat.Notifications.onUser('subscriptions-changed', (action, sub) => {
notifyNewRoom(sub);
});
}
});
});