diff --git a/src/messaging.js b/src/messaging.js index 339ec7798c..86ecf7014d 100644 --- a/src/messaging.js +++ b/src/messaging.js @@ -320,33 +320,34 @@ var db = require('./database'), }; function sendNotifications(fromuid, touid, messageObj, callback) { - if (!websockets.isUserOnline(touid)) { - notifications.create({ - bodyShort: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]', - bodyLong: messageObj.content, - path: nconf.get('relative_path') + '/chats/' + utils.slugify(messageObj.fromUser.username), - nid: 'chat_' + fromuid + '_' + touid, - from: fromuid - }, function(err, notification) { - if (!err && notification) { - notifications.push(notification, [touid], callback); - } - }); - - user.getSettings(messageObj.toUser.uid, function(err, settings) { - if (settings.sendChatNotifications && !parseInt(meta.config.disableEmailSubscriptions, 10)) { - emailer.send('notif_chat', touid, { - subject: '[[email:notif.chat.subject, ' + messageObj.fromUser.username + ']]', - username: messageObj.toUser.username, - summary: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]', - message: messageObj, - site_title: meta.config.title || 'NodeBB', - url: nconf.get('url'), - fromUserslug: utils.slugify(messageObj.fromUser.username) - }); - } - }); + if (websockets.isUserOnline(touid)) { + return callback(); } + + notifications.create({ + bodyShort: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]', + bodyLong: messageObj.content, + nid: 'chat_' + fromuid + '_' + touid, + from: fromuid + }, function(err, notification) { + if (!err && notification) { + notifications.push(notification, [touid], callback); + } + }); + + user.getSettings(messageObj.toUser.uid, function(err, settings) { + if (settings.sendChatNotifications && !parseInt(meta.config.disableEmailSubscriptions, 10)) { + emailer.send('notif_chat', touid, { + subject: '[[email:notif.chat.subject, ' + messageObj.fromUser.username + ']]', + username: messageObj.toUser.username, + summary: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]', + message: messageObj, + site_title: meta.config.title || 'NodeBB', + url: nconf.get('url'), + fromUserslug: utils.slugify(messageObj.fromUser.username) + }); + } + }); } }(exports)); diff --git a/src/notifications.js b/src/notifications.js index 0008fb1f69..5beed15f9c 100644 --- a/src/notifications.js +++ b/src/notifications.js @@ -55,11 +55,12 @@ var async = require('async'), } if (notification.from && !notification.image) { - User.getUserField(notification.from, 'picture', function(err, picture) { + User.getUserFields(notification.from, ['username', 'userslug', 'picture'], function(err, userData) { if (err) { return next(err); } - notification.image = picture; + notification.image = userData.picture; + notification.user = userData; next(null, notification); }); return; diff --git a/src/socket.io/user.js b/src/socket.io/user.js index fd23405758..15aa0e2ba6 100644 --- a/src/socket.io/user.js +++ b/src/socket.io/user.js @@ -294,7 +294,6 @@ SocketUser.follow = function(socket, data, callback) { notifications.create({ bodyShort: '[[notifications:user_started_following_you, ' + userData.username + ']]', - path: nconf.get('relative_path') + '/user/' + userData.userslug, nid: 'follow:' + data.uid + ':uid:' + socket.uid, from: socket.uid }, function(err, notification) { diff --git a/src/user/notifications.js b/src/user/notifications.js index 523b9e4903..706c227097 100644 --- a/src/user/notifications.js +++ b/src/user/notifications.js @@ -124,6 +124,13 @@ var async = require('async'), } notification.path = pidToPaths[notification.pid] || notification.path || ''; + + if (notification.nid.startsWith('chat')) { + notification.path = nconf.get('relative_path') + '/chats/' + notification.user.userslug; + } else if (notification.nid.startsWith('follow')) { + notification.path = nconf.get('relative_path') + '/user/' + notification.user.userslug; + } + notification.datetimeISO = utils.toISOString(notification.datetime); return notification; });