client loads messages using the new client loads messages using new method, getChatMessages restructured and renamed to getLastChatMessages, added GET_CHAT_MESSAGES, getChatMessages restructured and renamed to getLastChatMessages

This commit is contained in:
mluto 2013-01-06 16:11:48 +01:00
parent 9484b92ae2
commit 5592c4b0fe
5 changed files with 108 additions and 47 deletions

View file

@ -205,6 +205,8 @@ exports.handleMessage = function(client, message)
handleUserInfoUpdate(client, message);
} else if (message.data.type == "CHAT_MESSAGE") {
handleChatMessage(client, message);
} else if (message.data.type == "GET_CHAT_MESSAGES") {
handleGetChatMessages(client, message);
} else if (message.data.type == "SAVE_REVISION") {
handleSaveRevisionMessage(client, message);
} else if (message.data.type == "CLIENT_MESSAGE" &&
@ -362,6 +364,74 @@ function handleChatMessage(client, message)
});
}
/**
* Handles the clients requets for more chat-messages
* @param client the client that send this message
* @param message the message from the client
*/
function handleGetChatMessages(client, message)
{
if(message.data.start == null)
{
messageLogger.warn("Dropped message, GetChatMessages Message has no start!");
return;
}
if(message.data.end == null)
{
messageLogger.warn("Dropped message, GetChatMessages Message has no start!");
return;
}
var start = message.data.start;
var end = message.data.end;
var count = start - count;
if(count < 0 && count > 100)
{
messageLogger.warn("Dropped message, GetChatMessages Message, client requested invalid amout of messages!");
return;
}
var padId = sessioninfos[client.id].padId;
var pad;
async.series([
//get the pad
function(callback)
{
padManager.getPad(padId, function(err, _pad)
{
if(ERR(err, callback)) return;
pad = _pad;
callback();
});
},
function(callback)
{
pad.getChatMessages(start, end, function(err, chatMessages)
{
if(ERR(err, callback)) return;
var infoMsg = {
type: "COLLABROOM",
data: {
type: "CHAT_MESSAGES",
messages: chatMessages
}
};
// send the messages back to the client
for(var i in pad2sessions[padId])
{
if(pad2sessions[padId][i] == client.id)
{
socketio.sockets.sockets[pad2sessions[padId][i]].json.send(infoMsg);
break;
}
}
});
}]);
}
/**
* Handles a handleSuggestUserName, that means a user have suggest a userName for a other user
@ -778,7 +848,6 @@ function handleClientReady(client, message)
var pad;
var historicalAuthorData = {};
var currentTime;
var chatMessages;
var padIds;
async.series([
@ -852,7 +921,7 @@ function handleClientReady(client, message)
}
], callback);
},
//these db requests all need the pad object (timestamp of latest revission, author data, chat messages)
//these db requests all need the pad object (timestamp of latest revission, author data)
function(callback)
{
var authors = pad.getAllAuthors();
@ -881,16 +950,6 @@ function handleClientReady(client, message)
callback();
});
}, callback);
},
//get the latest chat messages
function(callback)
{
pad.getLastChatMessages(100, function(err, _chatMessages)
{
if(ERR(err, callback)) return;
chatMessages = _chatMessages;
callback();
});
}
], callback);
@ -968,7 +1027,9 @@ function handleClientReady(client, message)
"padId": message.padId,
"initialTitle": "Pad: " + message.padId,
"opts": {},
"chatHistory": chatMessages,
// tell the client the number of the latest chat-message, which will be
// used to request the latest 100 chat-messages later (GET_CHAT_MESSAGES)
"chatHead": pad.chatHead,
"numConnectedUsers": pad2sessions[padIds.padId].length,
"isProPad": false,
"readOnlyId": padIds.readOnlyPadId,