Fix wrong notification count

Signed-off-by: Ajay Bura <ajbura@gmail.com>
This commit is contained in:
Ajay Bura 2022-03-13 15:12:54 +05:30
parent cf58a4376e
commit d6b880d110
2 changed files with 67 additions and 71 deletions

View file

@ -72,14 +72,22 @@ function DrawerBreadcrumb({ spaceId }) {
const noti = notifications.getNoti(roomId); const noti = notifications.getNoti(roomId);
if (!notifications.hasNoti(childId)) return noti; if (!notifications.hasNoti(childId)) return noti;
if (noti.from === null) return noti; if (noti.from === null) return noti;
if (noti.from.has(childId) && noti.from.size === 1) return null;
const childNoti = notifications.getNoti(childId); const childNoti = notifications.getNoti(childId);
return { let noOther = true;
total: noti.total - childNoti.total, let total = 0;
highlight: noti.highlight - childNoti.highlight, let highlight = 0;
}; noti.from.forEach((fromId) => {
if (childNoti.from.has(fromId)) return;
noOther = false;
const fromNoti = notifications.getNoti(fromId);
total += fromNoti.total;
highlight += fromNoti.highlight;
});
if (noOther) return null;
return { total, highlight };
} }
return ( return (

View file

@ -42,8 +42,7 @@ class Notifications extends EventEmitter {
if (this.doesRoomHaveUnread(room) === false) return; if (this.doesRoomHaveUnread(room) === false) return;
const total = room.getUnreadNotificationCount('total'); const total = room.getUnreadNotificationCount('total');
const highlight = room.getUnreadNotificationCount('highlight'); const highlight = room.getUnreadNotificationCount('highlight');
const noti = this.getNoti(room.roomId); this._setNoti(room.roomId, total ?? 0, highlight ?? 0);
this._setNoti(room.roomId, total - noti.total, highlight - noti.highlight);
}; };
[...this.roomList.rooms].forEach(addNoti); [...this.roomList.rooms].forEach(addNoti);
[...this.roomList.directs].forEach(addNoti); [...this.roomList.directs].forEach(addNoti);
@ -96,74 +95,64 @@ class Notifications extends EventEmitter {
} }
} }
_getAllParentIds(roomId) { _setNoti(roomId, total, highlight) {
let allParentIds = this.roomList.roomIdToParents.get(roomId); const addNoti = (id, t, h, fromId) => {
if (allParentIds === undefined) return new Set(); const prevTotal = this.roomIdToNoti.get(id)?.total ?? null;
const parentIds = [...allParentIds]; const noti = this.getNoti(id);
parentIds.forEach((pId) => { noti.total += t;
allParentIds = new Set( noti.highlight += h;
[...allParentIds, ...this._getAllParentIds(pId)],
); if (fromId) {
if (noti.from === null) noti.from = new Set();
noti.from.add(fromId);
}
this.roomIdToNoti.set(id, noti);
this.emit(cons.events.notifications.NOTI_CHANGED, id, noti.total, prevTotal);
};
const noti = this.getNoti(roomId);
const addT = total - noti.total;
const addH = highlight - noti.highlight;
if (addT < 0 || addH < 0) return;
addNoti(roomId, addT, addH);
const allParentSpaces = this.roomList.getParentSpaces(roomId);
allParentSpaces.forEach((spaceId) => {
addNoti(spaceId, addT, addH, roomId);
}); });
return allParentIds;
} }
_setNoti(roomId, total, highlight, childId) { _deleteNoti(roomId, total, highlight) {
const prevTotal = this.roomIdToNoti.get(roomId)?.total ?? null; const removeNoti = (id, t, h, fromId) => {
const noti = this.getNoti(roomId); if (this.roomIdToNoti.has(id) === false) return;
if (!childId || this._remainingParentIds?.has(roomId)) { const noti = this.getNoti(id);
noti.total += total; const prevTotal = noti.total;
noti.highlight += highlight; noti.total -= t;
} noti.highlight -= h;
if (childId) { if (noti.total < 0) {
if (noti.from === null) noti.from = new Set(); noti.total = 0;
noti.from.add(childId); noti.highlight = 0;
} }
if (fromId && noti.from !== null) {
if (!this.hasNoti(fromId)) noti.from.delete(fromId);
}
if (noti.from === null || noti.from.size === 0) {
this.roomIdToNoti.delete(id);
this.emit(cons.events.notifications.FULL_READ, id);
this.emit(cons.events.notifications.NOTI_CHANGED, id, null, prevTotal);
} else {
this.roomIdToNoti.set(id, noti);
this.emit(cons.events.notifications.NOTI_CHANGED, id, noti.total, prevTotal);
}
};
this.roomIdToNoti.set(roomId, noti); removeNoti(roomId, total, highlight);
this.emit(cons.events.notifications.NOTI_CHANGED, roomId, noti.total, prevTotal); const allParentSpaces = this.roomList.getParentSpaces(roomId);
allParentSpaces.forEach((spaceId) => {
if (!childId) this._remainingParentIds = this._getAllParentIds(roomId); removeNoti(spaceId, total, highlight, roomId);
else this._remainingParentIds.delete(roomId); });
const parentIds = this.roomList.roomIdToParents.get(roomId);
if (typeof parentIds === 'undefined') {
if (!childId) this._remainingParentIds = undefined;
return;
}
[...parentIds].forEach((parentId) => this._setNoti(parentId, total, highlight, roomId));
if (!childId) this._remainingParentIds = undefined;
}
_deleteNoti(roomId, total, highlight, childId) {
if (this.roomIdToNoti.has(roomId) === false) return;
const noti = this.getNoti(roomId);
const prevTotal = noti.total;
noti.total -= total;
noti.highlight -= highlight;
if (noti.total < 0) {
noti.total = 0;
noti.highlight = 0;
}
if (childId && noti.from !== null) {
if (!this.hasNoti(childId)) noti.from.delete(childId);
}
if (noti.from === null || noti.from.size === 0) {
this.roomIdToNoti.delete(roomId);
this.emit(cons.events.notifications.FULL_READ, roomId);
this.emit(cons.events.notifications.NOTI_CHANGED, roomId, null, prevTotal);
} else {
this.roomIdToNoti.set(roomId, noti);
this.emit(cons.events.notifications.NOTI_CHANGED, roomId, noti.total, prevTotal);
}
const parentIds = this.roomList.roomIdToParents.get(roomId);
if (typeof parentIds === 'undefined') return;
[...parentIds].forEach((parentId) => this._deleteNoti(parentId, total, highlight, roomId));
} }
async _displayPopupNoti(mEvent, room) { async _displayPopupNoti(mEvent, room) {
@ -214,8 +203,7 @@ class Notifications extends EventEmitter {
const total = room.getUnreadNotificationCount('total'); const total = room.getUnreadNotificationCount('total');
const highlight = room.getUnreadNotificationCount('highlight'); const highlight = room.getUnreadNotificationCount('highlight');
const noti = this.getNoti(room.roomId); this._setNoti(room.roomId, total ?? 0, highlight ?? 0);
this._setNoti(room.roomId, total - noti.total, highlight - noti.highlight);
if (this.matrixClient.getSyncState() === 'SYNCING') { if (this.matrixClient.getSyncState() === 'SYNCING') {
this._displayPopupNoti(mEvent, room); this._displayPopupNoti(mEvent, room);