From 877820779c38083988aa94cffad7c9bf77a60291 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Mon, 19 Sep 2016 23:24:07 +0300 Subject: [PATCH] optimize notifications.getMultiple --- src/notifications.js | 55 ++++++++++++++++++-------------------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/src/notifications.js b/src/notifications.js index 86a13e3224..4b3111fdad 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -37,50 +37,41 @@ var utils = require('../public/src/utils'); return callback(err); } - if (!Array.isArray(notifications) || !notifications.length) { + notifications = notifications.filter(Boolean); + if (!notifications.length) { return callback(null, []); } - async.map(notifications, function(notification, next) { - if (!notification) { - return next(null, null); - } - - notification.datetimeISO = utils.toISOString(notification.datetime); + var userKeys = notifications.map(function(notification) { + return notification.from; + }); - if (notification.bodyLong) { - notification.bodyLong = S(notification.bodyLong).escapeHTML().s; + User.getUsersFields(userKeys, ['username', 'userslug', 'picture'], function(err, usersData) { + if (err) { + return callback(err); } + notifications.forEach(function(notification, index) { + notification.datetimeISO = utils.toISOString(notification.datetime); - if (notification.from && !notification.image) { - User.getUserFields(notification.from, ['username', 'userslug', 'picture'], function(err, userData) { - if (err) { - return next(err); - } - notification.image = userData.picture || null; - notification.user = userData; + if (notification.bodyLong) { + notification.bodyLong = S(notification.bodyLong).escapeHTML().s; + } - if (userData.username === '[[global:guest]]') { + notification.user = usersData[index]; + if (notification.user) { + notification.image = notification.user.picture || null; + if (notification.user.username === '[[global:guest]]') { notification.bodyShort = notification.bodyShort.replace(/([\s\S]*?),[\s\S]*?,([\s\S]*?)/, '$1, [[global:guest]], $2'); } - - next(null, notification); - }); - return; - } else if (notification.image) { - switch(notification.image) { - case 'brand:logo': - notification.image = meta.config['brand:logo'] || nconf.get('relative_path') + '/logo.png'; - break; } - return next(null, notification); - } else { - notification.image = meta.config['brand:logo'] || nconf.get('relative_path') + '/logo.png'; - return next(null, notification); - } + if (notification.image === 'brand:logo' || !notification.image) { + notification.image = meta.config['brand:logo'] || nconf.get('relative_path') + '/logo.png'; + } + }); - }, callback); + callback(null, notifications); + }); }); };