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

v1.18.x
Julian Lam 11 years ago
parent 2b15f46638
commit 7b3159292f

@ -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)); }(exports));

@ -195,6 +195,22 @@ Sockets.in = function(room) {
return io.sockets.in(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() { Sockets.getConnectedClients = function() {
var uids = []; var uids = [];
if (!io) { if (!io) {

@ -8,6 +8,7 @@ var posts = require('../posts'),
user = require('../user'), user = require('../user'),
notifications = require('../notifications'), notifications = require('../notifications'),
plugins = require('../plugins'), plugins = require('../plugins'),
utils = require('../../public/src/utils'),
async = require('async'), async = require('async'),
S = require('string'), S = require('string'),
@ -180,11 +181,13 @@ SocketModules.chats.send = function(socket, data, callback) {
return callback(err); 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 // After-the-fact fixing of the "self" property for the message that goes to the receipient
var recipMessage = JSON.parse(JSON.stringify(message)); var recipMessage = JSON.parse(JSON.stringify(message));
recipMessage.self = 0; recipMessage.self = 0;
// Recipient
server.getUserSockets(touid).forEach(function(s) { server.getUserSockets(touid).forEach(function(s) {
s.emit('event:chats.receive', { s.emit('event:chats.receive', {
withUid: socket.uid, withUid: socket.uid,
@ -192,6 +195,7 @@ SocketModules.chats.send = function(socket, data, callback) {
}); });
}); });
// Sender
server.getUserSockets(socket.uid).forEach(function(s) { server.getUserSockets(socket.uid).forEach(function(s) {
s.emit('event:chats.receive', { s.emit('event:chats.receive', {
withUid: touid, 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)) { if (!module.parent.exports.isUserOnline(touid)) {
var notifText = '[[notifications:new_message_from, ' + username + ']]';
notifications.create({ notifications.create({
bodyShort: notifText, bodyShort: '[[notifications:new_message_from, ' + messageObj.fromUser.username + ']]',
bodyLong: message, bodyLong: messageObj.content,
path: 'javascript:app.openChat('' + username + '', ' + fromuid + ');', path: nconf.get('relative_path') + '/chats/' + utils.slugify(messageObj.fromUser.username),
uniqueId: 'notification_' + fromuid + '_' + touid, uniqueId: 'notification_' + fromuid + '_' + touid,
from: fromuid from: fromuid
}, function(nid) { }, function(nid) {

Loading…
Cancel
Save