cleaned up authormanager

pull/85/merge
Peter 'Pita' Martischka 2011-08-09 11:55:12 +01:00
parent a850de5532
commit e6ba60bddc
1 changed files with 22 additions and 38 deletions

View File

@ -47,33 +47,29 @@ exports.getAuthor4Token = function (token, callback)
//there is no author with this token, so create one //there is no author with this token, so create one
if(author == null) if(author == null)
{ {
createAuthor(token, function(err, _author) createAuthor(null, function(err, _author)
{ {
//error?
if(err)
{
callback(err);
return;
}
author = _author; author = _author;
callback(err);
//create the token2author relation
db.set("token2author:" + token, author);
callback();
}); });
} }
//there is a author with this token //there is a author with this token
else else
{ {
//check if there is also an author object for this token, if not, create one //update the timestamp of this author
db.get("globalAuthor:" + author, function(err, authorObject) db.setSub("globalAuthor:" + author, ["timestamp"], new Date().getTime());
{ callback();
if(authorObject == null)
{
createAuthor(token, function(err, _author)
{
author = _author;
callback(err);
});
}
//the author exists, update the timestamp of this author
else
{
db.setSub("globalAuthor:" + author, ["timestamp"], new Date().getTime());
callback();
}
});
} }
} }
], function(err) ], function(err)
@ -86,30 +82,18 @@ exports.getAuthor4Token = function (token, callback)
* Internal function that creates the database entry for an author * Internal function that creates the database entry for an author
* @param {String} token The token * @param {String} token The token
*/ */
function createAuthor (token, callback) function createAuthor (name, callback)
{ {
//create the new author name //create the new author name
var author = "g." + _randomString(16); var author = "g." + _randomString(16);
//create the globalAuthors db entry //create the globalAuthors db entry
var authorObj = {colorId : Math.floor(Math.random()*32), name: null, timestamp: new Date().getTime()}; var authorObj = {"colorId" : Math.floor(Math.random()*32), "name": name, "timestamp": new Date().getTime()};
//we do this in series to ensure this db entries are written in the correct order //set the global author db entry
async.series([ db.set("globalAuthor:" + author, authorObj);
//set the global author db entry
function(callback) callback(null, author);
{
db.set("globalAuthor:" + author, authorObj, callback);
},
//set the token2author db entry
function(callback)
{
db.set("token2author:" + token, author, callback);
}
], function(err)
{
callback(err, author);
});
} }
/** /**