Authordata is now saved to the database

This commit is contained in:
Peter 'Pita' Martischka 2011-05-16 13:10:53 +01:00
parent fa6641a368
commit b1997a11c9
2 changed files with 290 additions and 232 deletions

View file

@ -19,103 +19,88 @@
* The AuthorManager controlls all information about the Pad authors * The AuthorManager controlls all information about the Pad authors
*/ */
/** var db = require("./db").db;
* Saves all Authors as a assoative Array. The Key is the author id. var async = require("async");
* Authors can have the following attributes:
* -name The Name of the Author as shown on the Pad
* -colorId The Id of Usercolor. A number between 0 and 31
* -timestamp The timestamp on which the user was last seen
*/
var globalAuthors = {};
/**
* A easy key value pair. The Key is the token, the value is the authorid
*/
var token2author = {};
/** /**
* Returns the Author Id for a token. If the token is unkown, * Returns the Author Id for a token. If the token is unkown,
* it creates a author for the token * it creates a author for the token
* @param token The token * @param token The token
*/ */
exports.getAuthor4Token = function (token) exports.getAuthor4Token = function (token, callback)
{ {
var author; var author;
if(token2author[token] == null) async.waterfall([
//try to get the author for this token
function(callback)
{ {
db.get("token2author:" + token, callback);
},
function(value, callback)
{
//there is no author with this token, so create one
if(value == null)
{
//create the new author name
author = "g." + _randomString(16); author = "g." + _randomString(16);
while(globalAuthors[author] != null) //set the token2author db entry
{ db.set("token2author:" + token, author);
author = "g." + _randomString(16);
}
token2author[token]=author; //set the globalAuthors db entry
var authorObj = {colorId : Math.floor(Math.random()*32), name: null, timestamp: new Date().getTime()};
db.set("globalAuthor:" + author, authorObj);
globalAuthors[author] = {}; callback(null);
globalAuthors[author].colorId = Math.floor(Math.random()*32);
globalAuthors[author].name = null;
} }
//there is a author with this token
else else
{ {
author = token2author[token]; author = value;
//update the author time
db.setSub("globalAuthor:" + author, ["timestamp"], new Date().getTime());
callback(null);
} }
}
globalAuthors[author].timestamp = new Date().getTime(); ], function(err)
{
return author; callback(err, author);
});
} }
/** /**
* Returns the color Id of the author * Returns the color Id of the author
*/ */
exports.getAuthorColorId = function (author) exports.getAuthorColorId = function (author, callback)
{ {
throwExceptionIfAuthorNotExist(author); db.getSub("globalAuthor:" + author, ["colorId"], callback);
return globalAuthors[author].colorId;
} }
/** /**
* Sets the color Id of the author * Sets the color Id of the author
*/ */
exports.setAuthorColorId = function (author, colorId) exports.setAuthorColorId = function (author, colorId, callback)
{ {
throwExceptionIfAuthorNotExist(author); db.setSub("globalAuthor:" + author, ["colorId"], colorId, callback);
globalAuthors[author].colorId = colorId;
} }
/** /**
* Returns the name of the author * Returns the name of the author
*/ */
exports.getAuthorName = function (author) exports.getAuthorName = function (author, callback)
{ {
throwExceptionIfAuthorNotExist(author); db.getSub("globalAuthor:" + author, ["name"], callback);
return globalAuthors[author].name;
} }
/** /**
* Sets the name of the author * Sets the name of the author
*/ */
exports.setAuthorName = function (author, name) exports.setAuthorName = function (author, name, callback)
{ {
throwExceptionIfAuthorNotExist(author); db.setSub("globalAuthor:" + author, ["name"], name, callback);
globalAuthors[author].name = name;
}
/**
* A internal function that checks if the Author exist and throws a exception if not
*/
function throwExceptionIfAuthorNotExist(author)
{
if(globalAuthors[author] == null)
{
throw "Author '" + author + "' is unkown!";
}
} }
/** /**

View file

@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
var async = require("async");
var padManager = require("./PadManager"); var padManager = require("./PadManager");
var Changeset = require("./Changeset"); var Changeset = require("./Changeset");
var AttributePoolFactory = require("./AttributePoolFactory"); var AttributePoolFactory = require("./AttributePoolFactory");
@ -79,14 +80,19 @@ exports.handleDisconnect = function(client)
var author = sessioninfos[client.sessionId].author; var author = sessioninfos[client.sessionId].author;
//prepare the notification for the other users on the pad, that this user joined //get the author color out of the db
authorManager.getAuthorColorId(author, function(err, color)
{
if(err) throw err;
//prepare the notification for the other users on the pad, that this user left
var messageToTheOtherUsers = { var messageToTheOtherUsers = {
"type": "COLLABROOM", "type": "COLLABROOM",
"data": { "data": {
type: "USER_LEAVE", type: "USER_LEAVE",
userInfo: { userInfo: {
"ip": "127.0.0.1", "ip": "127.0.0.1",
"colorId": authorManager.getAuthorColorId(author), "colorId": color,
"userAgent": "Anonymous", "userAgent": "Anonymous",
"userId": author "userId": author
} }
@ -112,6 +118,7 @@ exports.handleDisconnect = function(client)
//Delete the session2pad and sessioninfos entrys of this session //Delete the session2pad and sessioninfos entrys of this session
delete session2pad[client.sessionId]; delete session2pad[client.sessionId];
delete sessioninfos[client.sessionId]; delete sessioninfos[client.sessionId];
});
} }
/** /**
@ -385,9 +392,43 @@ function handleClientReady(client, message)
throw "CLIENT_READY Message have a unkown protocolVersion '" + protocolVersion + "'!"; throw "CLIENT_READY Message have a unkown protocolVersion '" + protocolVersion + "'!";
} }
//Ask the author Manager for a authorname of this token. var author;
var author = authorManager.getAuthor4Token(message.token); var authorName;
var authorColorId;
async.waterfall([
//get all authordata of this new user
function(callback)
{
//Ask the author Manager for a author of this token.
authorManager.getAuthor4Token(message.token, function(err,value)
{
author = value;
async.parallel([
//get colorId
function(callback)
{
authorManager.getAuthorColorId(author, function(err, value)
{
authorColorId = value;
callback(err);
});
},
//get author name
function(callback)
{
authorManager.getAuthorName(author, function(err, value)
{
authorName = value;
callback(err);
});
}
], callback);
});
},
function(callback)
{
//Check if this author is already on the pad, if yes, kick him! //Check if this author is already on the pad, if yes, kick him!
if(pad2sessions[message.padId]) if(pad2sessions[message.padId])
{ {
@ -447,7 +488,7 @@ function handleClientReady(client, message)
"colorPalette": ["#ffc7c7", "#fff1c7", "#e3ffc7", "#c7ffd5", "#c7ffff", "#c7d5ff", "#e3c7ff", "#ffc7f1", "#ff8f8f", "#ffe38f", "#c7ff8f", "#8fffab", "#8fffff", "#8fabff", "#c78fff", "#ff8fe3", "#d97979", "#d9c179", "#a9d979", "#79d991", "#79d9d9", "#7991d9", "#a979d9", "#d979c1", "#d9a9a9", "#d9cda9", "#c1d9a9", "#a9d9b5", "#a9d9d9", "#a9b5d9", "#c1a9d9", "#d9a9cd"], "colorPalette": ["#ffc7c7", "#fff1c7", "#e3ffc7", "#c7ffd5", "#c7ffff", "#c7d5ff", "#e3c7ff", "#ffc7f1", "#ff8f8f", "#ffe38f", "#c7ff8f", "#8fffab", "#8fffff", "#8fabff", "#c78fff", "#ff8fe3", "#d97979", "#d9c179", "#a9d979", "#79d991", "#79d9d9", "#7991d9", "#a979d9", "#d979c1", "#d9a9a9", "#d9cda9", "#c1d9a9", "#a9d9b5", "#a9d9d9", "#a9b5d9", "#c1a9d9", "#d9a9cd"],
"clientIp": (client.request && client.request.connection) ? client.request.connection.remoteAddress : "127.0.0.1", "clientIp": (client.request && client.request.connection) ? client.request.connection.remoteAddress : "127.0.0.1",
"userIsGuest": true, "userIsGuest": true,
"userColor": authorManager.getAuthorColorId(author), "userColor": authorColorId,
"padId": message.padId, "padId": message.padId,
"initialTitle": "Pad: " + message.padId, "initialTitle": "Pad: " + message.padId,
"opts": {}, "opts": {},
@ -470,9 +511,9 @@ function handleClientReady(client, message)
} }
//Add a username to the clientVars if one avaiable //Add a username to the clientVars if one avaiable
if(authorManager.getAuthorName(author) != null) if(authorName != null)
{ {
clientVars.userName = authorManager.getAuthorName(author); clientVars.userName = authorName;
} }
//Add all authors that worked on this pad, to the historicalAuthorData on clientVars //Add all authors that worked on this pad, to the historicalAuthorData on clientVars
@ -480,9 +521,9 @@ function handleClientReady(client, message)
for(i in allAuthors) for(i in allAuthors)
{ {
clientVars.collab_client_vars.historicalAuthorData[allAuthors[i]] = {}; clientVars.collab_client_vars.historicalAuthorData[allAuthors[i]] = {};
if(authorManager.getAuthorName(author) != null) if(authorName != null)
clientVars.collab_client_vars.historicalAuthorData[allAuthors[i]].name = authorManager.getAuthorName(author); clientVars.collab_client_vars.historicalAuthorData[allAuthors[i]].name = authorName;
clientVars.collab_client_vars.historicalAuthorData[allAuthors[i]].colorId = authorManager.getAuthorColorId(author); clientVars.collab_client_vars.historicalAuthorData[allAuthors[i]].colorId = authorColorId;
} }
//Send the clientVars to the Client //Send the clientVars to the Client
@ -499,7 +540,7 @@ function handleClientReady(client, message)
type: "USER_NEWINFO", type: "USER_NEWINFO",
userInfo: { userInfo: {
"ip": "127.0.0.1", "ip": "127.0.0.1",
"colorId": authorManager.getAuthorColorId(author), "colorId": authorColorId,
"userAgent": "Anonymous", "userAgent": "Anonymous",
"userId": author "userId": author
} }
@ -507,19 +548,46 @@ function handleClientReady(client, message)
}; };
//Add the authorname of this new User, if avaiable //Add the authorname of this new User, if avaiable
if(authorManager.getAuthorName(author) != null) if(authorName != null)
{ {
messageToTheOtherUsers.data.userInfo.name = authorManager.getAuthorName(author); messageToTheOtherUsers.data.userInfo.name = authorName;
} }
//Run trough all sessions of this pad //Run trough all sessions of this pad
for(i in pad2sessions[message.padId]) async.forEach(pad2sessions[message.padId], function(sessionID, callback)
{
var sessionAuthorName, sessionAuthorColorId;
async.series([
//get the authorname & colorId
function(callback)
{
async.parallel([
function(callback)
{
authorManager.getAuthorColorId(sessioninfos[sessionID].author, function(err, value)
{
sessionAuthorColorId = value;
callback(err);
})
},
function(callback)
{
authorManager.getAuthorName(sessioninfos[sessionID].author, function(err, value)
{
sessionAuthorName = value;
callback(err);
})
}
],callback);
},
function (callback)
{ {
//Jump over, if this session is the connection session //Jump over, if this session is the connection session
if(pad2sessions[message.padId][i] != client.sessionId) if(sessionID != client.sessionId)
{ {
//Send this Session the Notification about the new user //Send this Session the Notification about the new user
socketio.clients[pad2sessions[message.padId][i]].send(messageToTheOtherUsers); socketio.clients[sessionID].send(messageToTheOtherUsers);
//Send the new User a Notification about this other user //Send the new User a Notification about this other user
var messageToNotifyTheClientAboutTheOthers = { var messageToNotifyTheClientAboutTheOthers = {
@ -528,18 +596,23 @@ function handleClientReady(client, message)
type: "USER_NEWINFO", type: "USER_NEWINFO",
userInfo: { userInfo: {
"ip": "127.0.0.1", "ip": "127.0.0.1",
"colorId": authorManager.getAuthorColorId(sessioninfos[pad2sessions[message.padId][i]].author), "colorId": sessionAuthorColorId,
"name": authorManager.getAuthorName(sessioninfos[pad2sessions[message.padId][i]].author), "name": sessionAuthorName,
"userAgent": "Anonymous", "userAgent": "Anonymous",
"userId": sessioninfos[pad2sessions[message.padId][i]].author "userId": sessioninfos[sessionID].author
} }
} }
}; };
client.send(messageToNotifyTheClientAboutTheOthers); client.send(messageToNotifyTheClientAboutTheOthers);
} }
} }
], callback);
}, callback);
}
],function(err)
{
if(err) throw err;
});
} }
/** /**