diff --git a/web/src/app/Poller.js b/web/src/app/Poller.js index 2261dddc..8fefc94e 100644 --- a/web/src/app/Poller.js +++ b/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) { - console.log(`[Poller] No new notifications found for ${subscription.id}`); - return; + 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}`); } - 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(); diff --git a/web/src/app/SubscriptionManager.js b/web/src/app/SubscriptionManager.js index f5ea5a53..2b9260f7 100644 --- a/web/src/app/SubscriptionManager.js +++ b/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 {