[IMPROVE] Don't show unread count badge in burger menu if it is from the opened room (#12971)

* Don't show unread count badge in burger menu if it is from the opened room

* Remove package-lock.json

* fix lint

* fix lint

* Query unread message count on burger template

* fix review
pull/13919/head
Tasso Evangelista 7 years ago committed by Rodrigo Nascimento
parent 3bfc92609c
commit 9bc520d079
  1. 40
      app/ui/client/views/app/burger.js
  2. 52
      client/startup/unread.js

@ -1,17 +1,53 @@
import { Meteor } from 'meteor/meteor';
import { Session } from 'meteor/session';
import { Template } from 'meteor/templating';
import { Layout } from '../../../../ui-utils';
import { ChatSubscription } from '../../../../models/client';
import { Layout } from '../../../../ui-utils/client';
import { getUserPreference } from '../../../../utils';
Template.burger.helpers({
unread() {
return Session.get('unread');
const userUnreadAlert = getUserPreference(Meteor.userId(), 'unreadAlert');
const [unreadCount, unreadAlert] = ChatSubscription
.find({
open: true,
hideUnreadStatus: { $ne: true },
rid: { $ne: Session.get('openedRoom') },
}, {
fields: {
unread: 1,
alert: 1,
unreadAlert: 1,
},
})
.fetch()
.reduce(([unreadCount, unreadAlert], { alert, unread, unreadAlert: alertType }) => {
if (alert || unread > 0) {
unreadCount += unread;
if (alert === true && alertType !== 'nothing') {
if (alertType === 'all' || userUnreadAlert !== false) {
unreadAlert = '•';
}
}
}
return [unreadCount, unreadAlert];
}, [0, false]);
if (unreadCount > 0) {
return unreadCount > 99 ? '99+' : unreadCount;
}
return unreadAlert || '';
},
isMenuOpen() {
if (Session.equals('isMenuOpen', true)) {
return 'menu-opened';
}
},
embeddedVersion() {
return Layout.isEmbedded();
},

@ -9,31 +9,45 @@ import { RoomManager, menu, fireGlobalEvent, readMessage } from '../../app/ui-ut
import { getUserPreference } from '../../app/utils';
import { settings } from '../../app/settings';
Meteor.startup(function() {
Tracker.autorun(function() {
let unreadCount = 0;
let unreadAlert = false;
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();
const subscriptions = ChatSubscription.find({ open: true, hideUnreadStatus: { $ne: true } }, { fields: { unread: 1, alert: 1, rid: 1, t: 1, name: 1, ls: 1, unreadAlert: 1 } });
// TODO: make it a helper
const getOpenRoomId = () => Tracker.nonreactive(() => {
if (['channel', 'group', 'direct'].includes(FlowRouter.getRouteName())) {
return Session.get('openedRoom');
}
});
let openedRoomId = undefined;
Tracker.nonreactive(function() {
if (['channel', 'group', 'direct'].includes(FlowRouter.getRouteName())) {
openedRoomId = Session.get('openedRoom');
}
});
Meteor.startup(() => {
Tracker.autorun(() => {
const openedRoomId = getOpenRoomId();
for (const subscription of subscriptions.fetch()) {
let unreadCount = 0;
let unreadAlert = false;
for (const subscription of fetchSubscriptions()) {
fireGlobalEvent('unread-changed-by-subscription', subscription);
if (subscription.alert || subscription.unread > 0) {
// This logic is duplicated in /client/notifications/notification.coffee.
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.
Meteor.setTimeout(function() {
setTimeout(() => {
readMessage.readNow();
}, 500);
}
@ -69,20 +83,22 @@ Meteor.startup(function() {
});
});
Meteor.startup(function() {
window.favico = new Favico({
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 (window.favico) {
window.favico.badge(unread, {
if (favicon) {
favicon.badge(unread, {
bgColor: typeof unread !== 'number' ? '#3d8a3a' : '#ac1b1b',
});
}

Loading…
Cancel
Save