From ae8837b44a2db49d387abb4069f0d3f8cffe55a0 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 27 Jan 2016 20:12:44 +0200 Subject: [PATCH] generate notification path on demand --- public/src/modules/notifications.js | 36 +++++++++++++++-------------- src/notifications.js | 9 +++----- src/socket.io/notifications.js | 32 ++++++++++++++++++++++--- 3 files changed, 51 insertions(+), 26 deletions(-) diff --git a/public/src/modules/notifications.js b/public/src/modules/notifications.js index faa6ec9700..ee006cc3f9 100644 --- a/public/src/modules/notifications.js +++ b/public/src/modules/notifications.js @@ -66,26 +66,28 @@ define('notifications', ['sounds', 'translator', 'components'], function(sound, socket.on('event:new_notification', function(notifData) { // If a path is defined, show notif data, otherwise show generic data - var payload; + var payload = { + alert_id: 'new_notif', + title: '[[notifications:new_notification]]', + timeout: 2000 + }; + if (notifData.path) { - payload = { - alert_id: 'new_notif', - title: '[[notifications:new_notification]]', - message: notifData.bodyShort, - type: 'info', - timeout: 2000, - clickfn: function() { - ajaxify.go(notifData.path); - } + payload.message = notifData.bodyShort; + payload.type = 'info'; + payload.clickfn = function() { + socket.emit('notifications.generatePath', notifData.nid, function(err, path) { + if (err) { + return app.alertError(err.message); + } + if (path) { + ajaxify.go(path); + } + }); }; } else { - payload = { - alert_id: 'new_notif', - title: '[[notifications:new_notification]]', - message: '[[notifications:you_have_unread_notifications]]', - type: 'warning', - timeout: 2000 - }; + payload.message: '[[notifications:you_have_unread_notifications]]'; + payload.type = 'warning'; } app.alert(payload); diff --git a/src/notifications.js b/src/notifications.js index 0cce0693a9..7b6ea32b5b 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -195,11 +195,8 @@ var async = require('async'), var websockets = require('./socket.io'); if (websockets.server) { - // Add notification paths to sent notification object as well - async.eachLimit(uids, 50, function(uid, next) { - User.notifications.generateNotificationPaths([notification], uid, function(err, notifications) { - websockets.in('uid_' + uid).emit('event:new_notification', notifications[0]); - }); + uids.forEach(function(uid) { + websockets.in('uid_' + uid).emit('event:new_notification', notification); }); } @@ -386,7 +383,7 @@ var async = require('async'), var usernames = set.map(function(notifObj) { return notifObj.user.username; }).filter(function(username, idx, array) { - return array.indexOf(username) === idx + return array.indexOf(username) === idx; }); var numUsers = usernames.length; diff --git a/src/socket.io/notifications.js b/src/socket.io/notifications.js index c540c338bb..76a1e1aeda 100644 --- a/src/socket.io/notifications.js +++ b/src/socket.io/notifications.js @@ -1,8 +1,10 @@ "use strict"; -var user = require('../user'), - notifications = require('../notifications'), - SocketNotifs = {}; +var async = require('async'); +var user = require('../user'); +var notifications = require('../notifications'); + +var SocketNotifs = {}; SocketNotifs.get = function(socket, data, callback) { if (data && Array.isArray(data.nids) && socket.uid) { @@ -53,4 +55,28 @@ SocketNotifs.markAllRead = function(socket, data, callback) { notifications.markAllRead(socket.uid, callback); }; +SocketNotifs.generatePath = function(socket, nid, callback) { + if (!socket.uid) { + return; + } + async.waterfall([ + function (next) { + notifications.get(nid, next); + }, + function (notification, next) { + if (!notification) { + return next(null, ''); + } + user.notifications.generateNotificationPaths([notification], socket.uid, next); + }, + function (notificationsData, next) { + if (notificationsData && notificationsData.length) { + next(null, notificationsData[0].path); + } else { + next(); + } + } + ], callback); +}; + module.exports = SocketNotifs;