Skip to content

Node.js based forum software built for the modern web

moved some socket notifications to notifyUser instead of in websocket layer (again, @barisusakli)

Julian Lam0e9a096c259b
diff --git a/src/messaging.js b/src/messaging.js
index 3e7f7abf63..eccff45cff 100644
--- a/src/messaging.js
+++ b/src/messaging.js
@@ -10,7 +10,8 @@ var db = require('./database'),
utils = require('../public/src/utils'),
notifications = require('./notifications'),
userNotifications = require('./user/notifications'),
- emailer = require('./emailer');
+ emailer = require('./emailer'),
+ sockets = require('./socket.io');
(function(Messaging) {
Messaging.notifyQueue = {}; // Only used to notify a user of a new chat message, see Messaging.notifyUser
@@ -226,8 +227,6 @@ var db = require('./database'),
};
Messaging.getRecentChats = function(uid, start, stop, callback) {
- var websockets = require('./socket.io');
-
db.getSortedSetRevRange('uid:' + uid + ':chats', start, stop, function(err, uids) {
if (err) {
return callback(err);
@@ -256,7 +255,7 @@ var db = require('./database'),
results.users.forEach(function(user, index) {
if (user) {
user.unread = results.unread[index];
- user.status = require('./socket.io').isUserOnline(user.uid) ? user.status : 'offline';
+ user.status = sockets.isUserOnline(user.uid) ? user.status : 'offline';
}
});
@@ -269,6 +268,15 @@ var db = require('./database'),
db.sortedSetCard('uid:' + uid + ':chats:unread', callback);
};
+ Messaging.pushUnreadCount = function(uid) {
+ Messaging.getUnreadCount(uid, function(err, unreadCount) {
+ if (err) {
+ return;
+ }
+ sockets.in('uid_' + uid).emit('event:unread.updateChatCount', null, unreadCount);
+ });
+ };
+
Messaging.markRead = function(uid, toUid, callback) {
db.sortedSetRemove('uid:' + uid + ':chats:unread', toUid, callback);
};
@@ -278,6 +286,23 @@ var db = require('./database'),
};
Messaging.notifyUser = function(fromuid, touid, messageObj) {
+ // Immediate notifications
+ // Recipient
+ Messaging.pushUnreadCount(touid);
+ sockets.in('uid_' + touid).emit('event:chats.receive', {
+ withUid: fromuid,
+ message: messageObj,
+ self: 0
+ });
+ // Sender
+ Messaging.pushUnreadCount(fromuid);
+ sockets.in('uid_' + fromuid).emit('event:chats.receive', {
+ withUid: touid,
+ message: messageObj,
+ self: 1
+ });
+
+ // Delayed notifications
var queueObj = Messaging.notifyQueue[fromuid + ':' + touid];
if (queueObj) {
queueObj.message.content += '\n' + messageObj.content;
@@ -344,7 +369,7 @@ var db = require('./database'),
};
function sendNotifications(fromuid, touid, messageObj, callback) {
- if (require('./socket.io').isUserOnline(touid)) {
+ if (sockets.isUserOnline(touid)) {
return callback();
}
diff --git a/src/socket.io/modules.js b/src/socket.io/modules.js
index 7e90e8fa38..f160c9765a 100644
--- a/src/socket.io/modules.js
+++ b/src/socket.io/modules.js
@@ -162,22 +162,6 @@ SocketModules.chats.send = function(socket, data, callback) {
Messaging.notifyUser(socket.uid, touid, message);
- // Recipient
- SocketModules.chats.pushUnreadCount(touid);
- server.in('uid_' + touid).emit('event:chats.receive', {
- withUid: socket.uid,
- message: message,
- self: 0
- });
-
- // Sender
- SocketModules.chats.pushUnreadCount(socket.uid);
- server.in('uid_' + socket.uid).emit('event:chats.receive', {
- withUid: touid,
- message: message,
- self: 1
- });
-
callback();
});
});
@@ -189,19 +173,10 @@ SocketModules.chats.canMessage = function(socket, toUid, callback) {
});
};
-SocketModules.chats.pushUnreadCount = function(uid) {
- Messaging.getUnreadCount(uid, function(err, unreadCount) {
- if (err) {
- return;
- }
- server.in('uid_' + uid).emit('event:unread.updateChatCount', null, unreadCount);
- });
-};
-
SocketModules.chats.markRead = function(socket, touid, callback) {
Messaging.markRead(socket.uid, touid, function(err) {
if (!err) {
- SocketModules.chats.pushUnreadCount(socket.uid);
+ Messaging.pushUnreadCount(socket.uid);
}
});
};