change if author really exist, before we set the author timestamp

pull/55/head
Peter 'Pita' Martischka 2011-07-30 12:50:29 +01:00
parent 5238eeae8a
commit 29dc8fdc31
1 changed files with 44 additions and 20 deletions

View File

@ -32,38 +32,43 @@ exports.getAuthor4Token = function (token, callback)
{
var author;
async.waterfall([
async.series([
//try to get the author for this token
function(callback)
{
db.get("token2author:" + token, callback);
db.get("token2author:" + token, function (err, _author)
{
author = _author;
callback(err);
});
},
function(value, callback)
function(callback)
{
//there is no author with this token, so create one
if(value == null)
if(author == null)
{
//create the new author name
author = "g." + _randomString(16);
author = createAuthor(token);
//set the token2author db entry
db.set("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);
callback(null);
callback();
}
//there is a author with this token
else
{
author = value;
//update the author time
db.setSub("globalAuthor:" + author, ["timestamp"], new Date().getTime());
callback(null);
//check if there is also an author object for this token, if not, create one
db.get("globalAuthor:" + author, function(err, authorObject)
{
if(authorObject == null)
{
createAuthor(token);
}
else
{
//update the author time
db.setSub("globalAuthor:" + author, ["timestamp"], new Date().getTime());
}
callback();
});
}
}
], function(err)
@ -72,6 +77,25 @@ exports.getAuthor4Token = function (token, callback)
});
}
/**
* Internal function that creates the database entry for an author
* @param {String} token The token
*/
function createAuthor (token)
{
//create the new author name
author = "g." + _randomString(16);
//set the token2author db entry
db.set("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);
return author;
}
/**
* Returns the Author Obj of the author
* @param {String} author The id of the author