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
This commit is contained in:
Barış Soner Uşaklı
2025-02-02 18:39:24 -05:00
parent c1b630d484
commit 8e9fdb5fd6

View File

@@ -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;
}