mirror of
https://github.com/binwiederhier/ntfy.git
synced 2026-01-18 16:17:26 +01:00
Update polling
This commit is contained in:
@@ -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();
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user