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

This commit is contained in:
Peter 'Pita' Martischka 2011-07-30 12:50:29 +01:00
parent 5238eeae8a
commit 29dc8fdc31

View file

@ -32,17 +32,57 @@ 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)
{
author = createAuthor(token);
callback();
}
//there is a author with this token
else
{
//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)
{
callback(err, author);
});
}
/**
* 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);
@ -53,23 +93,7 @@ exports.getAuthor4Token = function (token, callback)
var authorObj = {colorId : Math.floor(Math.random()*32), name: null, timestamp: new Date().getTime()};
db.set("globalAuthor:" + author, authorObj);
callback(null);
}
//there is a author with this token
else
{
author = value;
//update the author time
db.setSub("globalAuthor:" + author, ["timestamp"], new Date().getTime());
callback(null);
}
}
], function(err)
{
callback(err, author);
});
return author;
}
/**