From 42362ccf658093205294c359c8387b58c94700ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Tue, 17 Feb 2026 22:38:57 -0500 Subject: [PATCH] fix: closes #13999, delay cache creation --- src/notifications.js | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/notifications.js b/src/notifications.js index 12ed99f7d5..45696988ff 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -21,16 +21,21 @@ const ttlCache = require('./cache/ttl'); const Notifications = module.exports; -// ttlcache for email-only chat notifications -const notificationCache = ttlCache({ - name: 'notification-email-cache', - max: 1000, - ttl: (meta.config.notificationSendDelay || 60) * 1000, - noDisposeOnSet: true, - dispose: sendEmail, -}); - -Notifications.delayCache = notificationCache; +// used to delay email notifications, +// and cancel them if the notification is already read +let notificationCache = null; +function getOrCreateCache() { + if (!notificationCache) { + notificationCache = ttlCache({ + name: 'notification-email-cache', + max: 1000, + ttl: (meta.config.notificationSendDelay || 60) * 1000, + noDisposeOnSet: true, + dispose: sendEmail, + }); + } + return notificationCache; +} Notifications.baseTypes = [ 'notificationType_upvote', @@ -276,19 +281,20 @@ async function pushToUids(uids, notification) { if (results.uidsToEmail.length) { const delayNotificationTypes = ['new-chat', 'new-group-chat', 'new-public-chat']; + const delayCache = getOrCreateCache(); if (delayNotificationTypes.includes(notification.type)) { const cacheKey = `${notification.mergeId}|${results.uidsToEmail.join(',')}`; - const payload = notificationCache.get(cacheKey); + const payload = delayCache.get(cacheKey); let { bodyLong } = notification; if (payload !== undefined) { bodyLong = [payload.notification.bodyLong, bodyLong].join('\n'); } - notificationCache.set(cacheKey, { uids: results.uidsToEmail, notification: { ...notification, bodyLong } }); + delayCache.set(cacheKey, { uids: results.uidsToEmail, notification: { ...notification, bodyLong } }); if (notification.bodyLong.length >= 1000) { - notificationCache.delete(cacheKey); + delayCache.delete(cacheKey); } } else { - notificationCache.set(`delayed:nid:${notification.nid}`, { + delayCache.set(`delayed:nid:${notification.nid}`, { uids: results.uidsToEmail, notification, });