Skip to content

Node.js based forum software built for the modern web

added some utility functions for #1798, fixed chat notifications bodyLong value

Julian Lam7b3159292f85
diff --git a/src/messaging.js b/src/messaging.js
index 035e038c42..9f59948320 100644
--- a/src/messaging.js
+++ b/src/messaging.js
@@ -151,4 +151,21 @@ var db = require('./database'),
});
};
+ // todo #1798 -- this utility method creates a room name given an array of uids.
+ Messaging.uidsToRoom = function(uids, callback) {
+ uid = parseInt(uid, 10);
+ if (typeof uid === 'number' && Array.isArray(roomUids)) {
+ var room = 'chat_';
+
+ room = room + roomUids.map(function(uid) {
+ return parseInt(uid, 10);
+ }).sort(function(a, b) {
+ return a-b;
+ }).join('_');
+
+ callback(null, room);
+ } else {
+ callback(new Error('invalid-uid-or-participant-uids'));
+ }
+ };
}(exports));
diff --git a/src/socket.io/index.js b/src/socket.io/index.js
index 750b8eeb11..47958a8ebf 100644
--- a/src/socket.io/index.js
+++ b/src/socket.io/index.js
@@ -195,6 +195,22 @@ Sockets.in = function(room) {
return io.sockets.in(room);
};
+Sockets.uidInRoom = function(uid, room) {
+ var clients = io.sockets.clients(room);
+
+ uid = parseInt(uid, 10);
+
+ if (typeof uid === 'number' && uid > 0) {
+ clients = clients.filter(function(socketObj) {
+ return uid === socketObj.uid;
+ });
+
+ return clients.length ? true : false;
+ } else {
+ return false;
+ }
+};
+
Sockets.getConnectedClients = function() {
var uids = [];
if (!io) {
diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js
index a4cab07963..b6cd649264 100644
--- a/src/socket.io/modules.js
+++ b/src/socket.io/modules.js
@@ -8,6 +8,7 @@ var posts = require('../posts'),
user = require('../user'),
notifications = require('../notifications'),
plugins = require('../plugins'),
+ utils = require('../../public/src/utils'),
async = require('async'),
S = require('string'),
@@ -180,11 +181,13 @@ SocketModules.chats.send = function(socket, data, callback) {
return callback(err);
}
- sendChatNotification(socket.uid, touid, message.fromUser.username, message);
+ sendChatNotification(socket.uid, touid, message);
// After-the-fact fixing of the "self" property for the message that goes to the receipient
var recipMessage = JSON.parse(JSON.stringify(message));
recipMessage.self = 0;
+
+ // Recipient
server.getUserSockets(touid).forEach(function(s) {
s.emit('event:chats.receive', {
withUid: socket.uid,
@@ -192,6 +195,7 @@ SocketModules.chats.send = function(socket, data, callback) {
});
});
+ // Sender
server.getUserSockets(socket.uid).forEach(function(s) {
s.emit('event:chats.receive', {
withUid: touid,
@@ -201,13 +205,13 @@ SocketModules.chats.send = function(socket, data, callback) {
});
};
-function sendChatNotification(fromuid, touid, username, message) {
+function sendChatNotification(fromuid, touid, messageObj) {
+ // todo #1798 -- this should check if the user is in room `chat_{uidA}_{uidB}` instead, see `Sockets.uidInRoom(uid, room);`
if (!module.parent.exports.isUserOnline(touid)) {
- var notifText = '[[notifications:new_message_from, ' + username + ']]';
notifications.create({
- bodyShort: notifText,
- bodyLong: message,
- path: 'javascript:app.openChat('' + username + '', ' + fromuid + ');',
+ bodyShort: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]',
+ bodyLong: messageObj.content,
+ path: nconf.get('relative_path') + '/chats/' + utils.slugify(messageObj.fromUser.username),
uniqueId: 'notification_' + fromuid + '_' + touid,
from: fromuid
}, function(nid) {