fix: closes #13999, delay cache creation

This commit is contained in:
Barış Soner Uşaklı
2026-02-17 22:38:57 -05:00
parent 3dfd9a43a4
commit 42362ccf65

View File

@@ -21,16 +21,21 @@ const ttlCache = require('./cache/ttl');
const Notifications = module.exports; const Notifications = module.exports;
// ttlcache for email-only chat notifications // used to delay email notifications,
const notificationCache = ttlCache({ // and cancel them if the notification is already read
name: 'notification-email-cache', let notificationCache = null;
max: 1000, function getOrCreateCache() {
ttl: (meta.config.notificationSendDelay || 60) * 1000, if (!notificationCache) {
noDisposeOnSet: true, notificationCache = ttlCache({
dispose: sendEmail, name: 'notification-email-cache',
}); max: 1000,
ttl: (meta.config.notificationSendDelay || 60) * 1000,
Notifications.delayCache = notificationCache; noDisposeOnSet: true,
dispose: sendEmail,
});
}
return notificationCache;
}
Notifications.baseTypes = [ Notifications.baseTypes = [
'notificationType_upvote', 'notificationType_upvote',
@@ -276,19 +281,20 @@ async function pushToUids(uids, notification) {
if (results.uidsToEmail.length) { if (results.uidsToEmail.length) {
const delayNotificationTypes = ['new-chat', 'new-group-chat', 'new-public-chat']; const delayNotificationTypes = ['new-chat', 'new-group-chat', 'new-public-chat'];
const delayCache = getOrCreateCache();
if (delayNotificationTypes.includes(notification.type)) { if (delayNotificationTypes.includes(notification.type)) {
const cacheKey = `${notification.mergeId}|${results.uidsToEmail.join(',')}`; const cacheKey = `${notification.mergeId}|${results.uidsToEmail.join(',')}`;
const payload = notificationCache.get(cacheKey); const payload = delayCache.get(cacheKey);
let { bodyLong } = notification; let { bodyLong } = notification;
if (payload !== undefined) { if (payload !== undefined) {
bodyLong = [payload.notification.bodyLong, bodyLong].join('\n'); 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) { if (notification.bodyLong.length >= 1000) {
notificationCache.delete(cacheKey); delayCache.delete(cacheKey);
} }
} else { } else {
notificationCache.set(`delayed:nid:${notification.nid}`, { delayCache.set(`delayed:nid:${notification.nid}`, {
uids: results.uidsToEmail, uids: results.uidsToEmail,
notification, notification,
}); });