From 8e9fdb5fd68944cc5821ace802323dc6fbcd6433 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Sun, 2 Feb 2025 18:39:24 -0500 Subject: [PATCH] fix: #13115, limit bodyLength length send out notification if bodyLong.length exceeds limit add a max item limit to ttl cache get rid of has/get check if item isn't in cache get returns undefined --- src/notifications.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/notifications.js b/src/notifications.js index 1d020e536b..65804da6ab 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -22,6 +22,7 @@ const Notifications = module.exports; // ttlcache for email-only chat notifications const notificationCache = ttlCache({ + max: 1000, ttl: (meta.config.notificationSendDelay || 60) * 1000, noDisposeOnSet: true, dispose: sendEmail, @@ -246,11 +247,14 @@ async function pushToUids(uids, notification) { const delayNotificationTypes = ['new-chat', 'new-group-chat', 'new-public-chat']; if (delayNotificationTypes.includes(notification.type)) { const cacheKey = `${notification.mergeId}|${results.uidsToEmail.join(',')}`; - if (notificationCache.has(cacheKey)) { - const payload = notificationCache.get(cacheKey); + const payload = notificationCache.get(cacheKey); + if (payload !== undefined) { notification.bodyLong = [payload.notification.bodyLong, notification.bodyLong].join('\n'); } notificationCache.set(cacheKey, { uids: results.uidsToEmail, notification }); + if (notification.bodyLong.length >= 1000) { + notificationCache.delete(cacheKey); + } } else { await sendEmail({ uids: results.uidsToEmail, notification }); } @@ -264,8 +268,7 @@ async function pushToUids(uids, notification) { } async function sendEmail({ uids, notification }, mergeId, reason) { - // Only act on cache item expiry - if (reason && reason !== 'stale') { + if (reason && reason === 'set') { return; }