You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: server.js
+53-42Lines changed: 53 additions & 42 deletions
Original file line number
Diff line number
Diff line change
@@ -95,7 +95,6 @@ const db = new sqlite3.Database(DB_PATH, (err) => {
95
95
db.run(`CREATE TABLE IF NOT EXISTS notifications (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL, channelId TEXT NOT NULL, channelName TEXT NOT NULL, channelLogo TEXT, programTitle TEXT NOT NULL, programDesc TEXT, programStart TEXT NOT NULL, programStop TEXT NOT NULL, notificationTime TEXT NOT NULL, programId TEXT NOT NULL, status TEXT DEFAULT 'pending', triggeredAt TEXT, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE)`);
96
96
db.run(`CREATE TABLE IF NOT EXISTS push_subscriptions (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER NOT NULL, endpoint TEXT UNIQUE NOT NULL, p256dh TEXT NOT NULL, auth TEXT NOT NULL, FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE)`);
97
97
98
-
// --- **FIX 1: New table for per-device notification tracking** ---
99
98
db.run(`CREATE TABLE IF NOT EXISTS notification_deliveries (
@@ -1465,9 +1477,12 @@ async function checkAndSendNotifications() {
1465
1477
if(dueDeliveries.length>0){
1466
1478
console.log(`[PUSH_CHECKER] Found ${dueDeliveries.length} due notification deliveries to process.`);
1467
1479
}else{
1468
-
return;// No work to do
1480
+
return;
1469
1481
}
1470
1482
1483
+
// --- **FIX: Decouple main notification status from delivery status** ---
1484
+
// We will only update the `notification_deliveries` table. The main `notifications`
1485
+
// table status will be derived on-the-fly when requested by the client.
1471
1486
for(constdeliveryofdueDeliveries){
1472
1487
console.log(`[PUSH_CHECKER] Processing delivery ID ${delivery.delivery_id} for program "${delivery.programTitle}" to subscription ${delivery.subscription_id}.`);
1473
1488
@@ -1488,26 +1503,22 @@ async function checkAndSendNotifications() {
console.log(`[PUSH_CHECKER] Subscription ${delivery.subscription_id} is invalid (410/404). Deleting subscription and marking deliveries as failed.`);
1502
-
// Delete the invalid subscription
1503
-
db.run("DELETE FROM push_subscriptions WHERE id = ?",[delivery.subscription_id]);
1504
-
// Mark all pending deliveries for this subscription as failed to prevent retries
1505
-
db.run("UPDATE notification_deliveries SET status = 'failed', updatedAt = ? WHERE subscription_id = ? AND status = 'pending'",[nowIso,delivery.subscription_id]);
1506
-
}else{
1507
-
// For other errors (e.g., network issues), just mark this attempt as failed. It will be retried.
1508
-
db.run("UPDATE notification_deliveries SET status = 'failed', updatedAt = ? WHERE id = ?",[nowIso,delivery.delivery_id]);
console.log(`[PUSH_CHECKER] Subscription ${delivery.subscription_id} is invalid (410/404). Deleting subscription and failing deliveries.`);
1516
+
db.run("DELETE FROM push_subscriptions WHERE id = ?",[delivery.subscription_id]);
1517
+
db.run("UPDATE notification_deliveries SET status = 'failed', updatedAt = ? WHERE subscription_id = ? AND status = 'pending'",[nowIso,delivery.subscription_id]);
1518
+
}else{
1519
+
db.run("UPDATE notification_deliveries SET status = 'failed', updatedAt = ? WHERE id = ?",[nowIso,delivery.delivery_id]);
1520
+
}
1521
+
});
1511
1522
}
1512
1523
}catch(error){
1513
1524
console.error('[PUSH_CHECKER] Unhandled error in checkAndSendNotifications:',error);
0 commit comments