binwiederhier 1 месяц назад
Родитель
Сommit
bfbe73aea3
2 измененных файлов с 31 добавлено и 5 удалено
  1. 30 4
      web/src/app/Poller.js
  2. 1 1
      web/src/app/SubscriptionManager.js

+ 30 - 4
web/src/app/Poller.js

@@ -42,12 +42,22 @@ class Poller {
 
     const since = subscription.last;
     const notifications = await api.poll(subscription.baseUrl, subscription.topic, since);
-    if (!notifications || notifications.length === 0) {
+    const deletedSids = this.deletedSids(notifications);
+    const newOrUpdatedNotifications = this.newOrUpdatedNotifications(notifications, deletedSids);
+
+    // Delete all existing notifications with a deleted sequence ID
+    if (deletedSids.length > 0) {
+      console.log(`[Poller] Deleting notifications with deleted sequence IDs for ${subscription.id}`);
+      await Promise.all(deletedSids.map((sid) => subscriptionManager.deleteNotificationBySid(subscription.id, sid)));
+    }
+
+    // Add new or updated notifications
+    if (newOrUpdatedNotifications.length > 0) {
+      console.log(`[Poller] Adding ${notifications.length} notification(s) for ${subscription.id}`);
+      await subscriptionManager.addNotifications(subscription.id, notifications);
+    } else {
       console.log(`[Poller] No new notifications found for ${subscription.id}`);
-      return;
     }
-    console.log(`[Poller] Adding ${notifications.length} notification(s) for ${subscription.id}`);
-    await subscriptionManager.addNotifications(subscription.id, notifications);
   }
 
   pollInBackground(subscription) {
@@ -59,6 +69,22 @@ class Poller {
       }
     })();
   }
+
+  deletedSids(notifications) {
+    return new Set(
+      notifications
+        .filter(n => n.sid && n.deleted)
+        .map(n => n.sid)
+    );
+  }
+
+  newOrUpdatedNotifications(notifications, deletedSids) {
+    return notifications
+      .filter((notification) => {
+        const sid = notification.sid || notification.id;
+        return !deletedSids.has(notification.id) && !deletedSids.has(sid) && !notification.deleted;
+      });
+  }
 }
 
 const poller = new Poller();

+ 1 - 1
web/src/app/SubscriptionManager.js

@@ -193,7 +193,7 @@ class SubscriptionManager {
   /** Adds notification, or returns false if it already exists */
   async addNotification(subscriptionId, notification) {
     const exists = await this.db.notifications.get(notification.id);
-    if (exists) {
+    if (exists || notification.deleted) {
       return false;
     }
     try {