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/startup/unread.js

108 lines
2.8 KiB

import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
import { FlowRouter } from 'meteor/kadira:flow-router';
import { Session } from 'meteor/session';
import { Favico } from '../../app/favico';
import { ChatSubscription } from '../../app/models';
import { RoomManager, menu, fireGlobalEvent, readMessage } from '../../app/ui-utils';
import { getUserPreference } from '../../app/utils';
import { settings } from '../../app/settings';
const fetchSubscriptions = () => ChatSubscription.find({
open: true,
hideUnreadStatus: { $ne: true },
}, {
fields: {
unread: 1,
alert: 1,
rid: 1,
t: 1,
name: 1,
ls: 1,
unreadAlert: 1,
},
}).fetch();
// TODO: make it a helper
const getOpenRoomId = () => Tracker.nonreactive(() => {
if (['channel', 'group', 'direct'].includes(FlowRouter.getRouteName())) {
return Session.get('openedRoom');
}
});
Meteor.startup(() => {
Tracker.autorun(() => {
const openedRoomId = getOpenRoomId();
let unreadCount = 0;
let unreadAlert = false;
for (const subscription of fetchSubscriptions()) {
fireGlobalEvent('unread-changed-by-subscription', subscription);
if (subscription.alert || subscription.unread > 0) {
const hasFocus = readMessage.isEnable();
const subscriptionIsTheOpenedRoom = openedRoomId === subscription.rid;
if (hasFocus && subscriptionIsTheOpenedRoom) {
// The user has probably read all messages in this room.
// TODO: readNow() should return whether it has actually marked the room as read.
setTimeout(() => {
readMessage.readNow();
}, 500);
}
// Increment the total unread count.
unreadCount += subscription.unread;
if (subscription.alert === true && subscription.unreadAlert !== 'nothing') {
const userUnreadAlert = getUserPreference(Meteor.userId(), 'unreadAlert');
if (subscription.unreadAlert === 'all' || userUnreadAlert !== false) {
unreadAlert = '•';
}
}
}
if (RoomManager.openedRooms[subscription.t + subscription.name]) {
readMessage.refreshUnreadMark(subscription.rid);
}
}
menu.updateUnreadBars();
if (unreadCount > 0) {
if (unreadCount > 999) {
Session.set('unread', '999+');
} else {
Session.set('unread', unreadCount);
}
} else if (unreadAlert !== false) {
Session.set('unread', unreadAlert);
} else {
Session.set('unread', '');
}
});
});
Meteor.startup(() => {
const favicon = new Favico({
position: 'up',
animation: 'none',
});
window.favico = favicon;
Tracker.autorun(function() {
const siteName = settings.get('Site_Name') || '';
const unread = Session.get('unread');
fireGlobalEvent('unread-changed', unread);
if (favicon) {
favicon.badge(unread, {
bgColor: typeof unread !== 'number' ? '#3d8a3a' : '#ac1b1b',
});
}
document.title = unread === '' ? siteName : `(${ unread }) ${ siteName }`;
});
});