generate notification path on demand

v1.18.x
barisusakli 9 years ago
parent add4e6ee5b
commit ae8837b44a

@ -66,26 +66,28 @@ define('notifications', ['sounds', 'translator', 'components'], function(sound,
socket.on('event:new_notification', function(notifData) { socket.on('event:new_notification', function(notifData) {
// If a path is defined, show notif data, otherwise show generic data // 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) { if (notifData.path) {
payload = { payload.message = notifData.bodyShort;
alert_id: 'new_notif', payload.type = 'info';
title: '[[notifications:new_notification]]', payload.clickfn = function() {
message: notifData.bodyShort, socket.emit('notifications.generatePath', notifData.nid, function(err, path) {
type: 'info', if (err) {
timeout: 2000, return app.alertError(err.message);
clickfn: function() { }
ajaxify.go(notifData.path); if (path) {
} ajaxify.go(path);
}
});
}; };
} else { } else {
payload = { payload.message: '[[notifications:you_have_unread_notifications]]';
alert_id: 'new_notif', payload.type = 'warning';
title: '[[notifications:new_notification]]',
message: '[[notifications:you_have_unread_notifications]]',
type: 'warning',
timeout: 2000
};
} }
app.alert(payload); app.alert(payload);

@ -195,11 +195,8 @@ var async = require('async'),
var websockets = require('./socket.io'); var websockets = require('./socket.io');
if (websockets.server) { if (websockets.server) {
// Add notification paths to sent notification object as well uids.forEach(function(uid) {
async.eachLimit(uids, 50, function(uid, next) { websockets.in('uid_' + uid).emit('event:new_notification', notification);
User.notifications.generateNotificationPaths([notification], uid, function(err, notifications) {
websockets.in('uid_' + uid).emit('event:new_notification', notifications[0]);
});
}); });
} }
@ -386,7 +383,7 @@ var async = require('async'),
var usernames = set.map(function(notifObj) { var usernames = set.map(function(notifObj) {
return notifObj.user.username; return notifObj.user.username;
}).filter(function(username, idx, array) { }).filter(function(username, idx, array) {
return array.indexOf(username) === idx return array.indexOf(username) === idx;
}); });
var numUsers = usernames.length; var numUsers = usernames.length;

@ -1,8 +1,10 @@
"use strict"; "use strict";
var user = require('../user'), var async = require('async');
notifications = require('../notifications'), var user = require('../user');
SocketNotifs = {}; var notifications = require('../notifications');
var SocketNotifs = {};
SocketNotifs.get = function(socket, data, callback) { SocketNotifs.get = function(socket, data, callback) {
if (data && Array.isArray(data.nids) && socket.uid) { if (data && Array.isArray(data.nids) && socket.uid) {
@ -53,4 +55,28 @@ SocketNotifs.markAllRead = function(socket, data, callback) {
notifications.markAllRead(socket.uid, 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; module.exports = SocketNotifs;

Loading…
Cancel
Save