Skip to content

Commit

Permalink
Fixed bug: Weird chat behavior
Browse files Browse the repository at this point in the history
Probably caused by a merge issue - there were 2 handlers for each message. Anyway - added another check (to != current user).

#617
  • Loading branch information
OrAbramovich committed Jun 22, 2018
1 parent 624968c commit 6bfecaa
Showing 1 changed file with 5 additions and 35 deletions.
40 changes: 5 additions & 35 deletions server/socketsServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,50 +104,20 @@ const initServer = (server) => {
const message = buildPrivateMessageJSON(new ObjectID(socket.decoded_token._id), new Date().getTime(), messageData.content, false);
message['_id'] = new ObjectID(); //the message id should be the same for both sides! (the sender and the reciever.)
handleNewPrivateMessage(socket.decoded_token._id, messageData.to, message).then(() => {
sendUserRealTimePrivateMessage(messageData.to, message);
//should be sent only if we are not the target!
if(messageData.to != socket.decoded_token._id)
sendUserRealTimePrivateMessage(messageData.to, message);
});
});
//Updates that the message was read in the users (sender & receiver) documents and send it to the other user
socket.on(SocketMsgTypes.CHAT_MSG_READ, (messageData) => {
handleReadPrivateMessage(socket.decoded_token._id, messageData.to, messageData._id).then(() => {
sendUserRealTimeReadPrivateMessage(messageData.to, messageData);
if(messageData.to != socket.decoded_token._id)
sendUserRealTimeReadPrivateMessage(messageData.to, messageData);
});
});
});
};
io.sockets
.on(
'connection',
socketioJwt.authorize({
secret: process.env.JWT_SECRET,
timeout: 15000
})
)
.on('authenticated', function(socket) {
//Establishes a new dedicated room which serves as a communication channel available only to the user
socket.on(SocketMsgTypes.JOIN, function(data) {
establishRoomForUser(socket.decoded_token._id, socket);
});
//Marks the notification as read and saves it in the user document
socket.on(SocketMsgTypes.NOTIFICATION_READ, function(notification) {
markNotificationAsRead(socket.decoded_token._id, notification);
});
//Sends a private message to the receiver and stores it in the DB.
socket.on(SocketMsgTypes.CHAT_MSG, function (messageData) {
const message = buildPrivateMessageJSON(new ObjectID(socket.decoded_token._id), new Date().getTime(), messageData.content, false);
message._id = new ObjectID(); //the message id should be the same for both sides! (the sender and the reciever.)
handleNewPrivateMessage(socket.decoded_token._id, messageData.to, message).then((res)=>{
sendUserRealTimePrivateMessage(messageData.to, message);
})
});
//Updates that the message was read in the users (sender & receiver) documents and send it to the other user
socket.on(SocketMsgTypes.CHAT_MSG_READ, function (messageData) {
handleReadPrivateMessage(socket.decoded_token._id, messageData.to, messageData._id).then((res)=>{
sendUserRealTimeReadPrivateMessage(messageData.to, messageData);
})
});
});

/**
* @author: Or Abramovich
* @date: 04/18
Expand Down

0 comments on commit 6bfecaa

Please sign in to comment.