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)],
);
});
return allParentIds; if (fromId) {
}
_setNoti(roomId, total, highlight, childId) {
const prevTotal = this.roomIdToNoti.get(roomId)?.total ?? null;
const noti = this.getNoti(roomId);
if (!childId || this._remainingParentIds?.has(roomId)) {
noti.total += total;
noti.highlight += highlight;
}
if (childId) {
if (noti.from === null) noti.from = new Set(); if (noti.from === null) noti.from = new Set();
noti.from.add(childId); noti.from.add(fromId);
} }
this.roomIdToNoti.set(id, noti);
this.roomIdToNoti.set(roomId, noti); this.emit(cons.events.notifications.NOTI_CHANGED, id, noti.total, prevTotal);
this.emit(cons.events.notifications.NOTI_CHANGED, roomId, noti.total, prevTotal); };
if (!childId) this._remainingParentIds = this._getAllParentIds(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 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);
});
}
_deleteNoti(roomId, total, highlight) {
const removeNoti = (id, t, h, fromId) => {
if (this.roomIdToNoti.has(id) === false) return;
const noti = this.getNoti(id);
const prevTotal = noti.total; const prevTotal = noti.total;
noti.total -= total; noti.total -= t;
noti.highlight -= highlight; noti.highlight -= h;
if (noti.total < 0) { if (noti.total < 0) {
noti.total = 0; noti.total = 0;
noti.highlight = 0; noti.highlight = 0;
} }
if (childId && noti.from !== null) { if (fromId && noti.from !== null) {
if (!this.hasNoti(childId)) noti.from.delete(childId); if (!this.hasNoti(fromId)) noti.from.delete(fromId);
} }
if (noti.from === null || noti.from.size === 0) { if (noti.from === null || noti.from.size === 0) {
this.roomIdToNoti.delete(roomId); this.roomIdToNoti.delete(id);
this.emit(cons.events.notifications.FULL_READ, roomId); this.emit(cons.events.notifications.FULL_READ, id);
this.emit(cons.events.notifications.NOTI_CHANGED, roomId, null, prevTotal); this.emit(cons.events.notifications.NOTI_CHANGED, id, null, prevTotal);
} else { } else {
this.roomIdToNoti.set(roomId, noti); this.roomIdToNoti.set(id, noti);
this.emit(cons.events.notifications.NOTI_CHANGED, roomId, noti.total, prevTotal); this.emit(cons.events.notifications.NOTI_CHANGED, id, noti.total, prevTotal);
} }
};
const parentIds = this.roomList.roomIdToParents.get(roomId); removeNoti(roomId, total, highlight);
if (typeof parentIds === 'undefined') return; const allParentSpaces = this.roomList.getParentSpaces(roomId);
[...parentIds].forEach((parentId) => this._deleteNoti(parentId, total, highlight, roomId)); allParentSpaces.forEach((spaceId) => {
removeNoti(spaceId, 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);