Fix duplicate notification count
authorAjay Bura <ajbura@gmail.com>
Thu, 28 Oct 2021 09:38:26 +0000 (15:08 +0530)
committerAjay Bura <ajbura@gmail.com>
Thu, 28 Oct 2021 09:38:26 +0000 (15:08 +0530)
Signed-off-by: Ajay Bura <ajbura@gmail.com>
src/client/state/Notifications.js

index b5ba4e04cf7e49cc7a05da6eb7544634eb18fff9..1ab9b5b1ba42cdc677bfb9da597d6719a4978abf 100644 (file)
@@ -77,12 +77,28 @@ class Notifications extends EventEmitter {
     return this.roomIdToNoti.has(roomId);
   }
 
+  _getAllParentIds(roomId) {
+    let allParentIds = this.roomList.roomIdToParents.get(roomId);
+    if (allParentIds === undefined) return new Set();
+    const parentIds = [...allParentIds];
+
+    parentIds.forEach((pId) => {
+      allParentIds = new Set(
+        [...allParentIds, ...this._getAllParentIds(pId)],
+      );
+    });
+
+    return allParentIds;
+  }
+
   _setNoti(roomId, total, highlight, childId) {
     const prevTotal = this.roomIdToNoti.get(roomId)?.total ?? null;
     const noti = this.getNoti(roomId);
 
-    noti.total += total;
-    noti.highlight += highlight;
+    if (!childId || this._remainingParentIds?.has(roomId)) {
+      noti.total += total;
+      noti.highlight += highlight;
+    }
     if (childId) {
       if (noti.from === null) noti.from = new Set();
       noti.from.add(childId);
@@ -91,9 +107,16 @@ class Notifications extends EventEmitter {
     this.roomIdToNoti.set(roomId, noti);
     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') return;
+    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) {
@@ -108,7 +131,7 @@ class Notifications extends EventEmitter {
       noti.highlight = 0;
     }
     if (childId && noti.from !== null) {
-      noti.from.delete(childId);
+      if (!this.hasNoti(childId)) noti.from.delete(childId);
     }
     if (noti.from === null || noti.from.size === 0) {
       this.roomIdToNoti.delete(roomId);