added getMappedAuthor4
This commit is contained in:
parent
e336b4f940
commit
f01906d815
3 changed files with 82 additions and 53 deletions
|
@ -102,10 +102,7 @@ Example returns:
|
||||||
|
|
||||||
{code: 0, message:"ok", data: {authorID: 5}}
|
{code: 0, message:"ok", data: {authorID: 5}}
|
||||||
*/
|
*/
|
||||||
exports.getMappedAuthor4 = function(authorMapper ,name, callback)
|
exports.getMappedAuthor4 = authorManager.getMappedAuthor4;
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**********************/
|
/**********************/
|
||||||
/**SESSION FUNCTIONS***/
|
/**SESSION FUNCTIONS***/
|
||||||
|
|
|
@ -22,59 +22,91 @@ var db = require("./DB").db;
|
||||||
var async = require("async");
|
var async = require("async");
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the Author Id for a token. If the token is unkown,
|
* Returns the AuthorID for a token.
|
||||||
* it creates a author for the token
|
|
||||||
* @param {String} token The token
|
* @param {String} token The token
|
||||||
* @param {Function} callback callback (err, author)
|
* @param {Function} callback callback (err, author)
|
||||||
* The callback function that is called when the result is here
|
|
||||||
*/
|
*/
|
||||||
exports.getAuthor4Token = function (token, callback)
|
exports.getAuthor4Token = function (token, callback)
|
||||||
{
|
{
|
||||||
var author;
|
mapAuthorWithDBKey("token2author", token, function(err, author)
|
||||||
|
|
||||||
async.series([
|
|
||||||
//try to get the author for this token
|
|
||||||
function(callback)
|
|
||||||
{
|
|
||||||
db.get("token2author:" + token, function (err, _author)
|
|
||||||
{
|
|
||||||
author = _author;
|
|
||||||
callback(err);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
function(callback)
|
|
||||||
{
|
|
||||||
//there is no author with this token, so create one
|
|
||||||
if(author == null)
|
|
||||||
{
|
|
||||||
exports.createAuthor(null, function(err, _author)
|
|
||||||
{
|
|
||||||
//error?
|
|
||||||
if(err)
|
|
||||||
{
|
|
||||||
callback(err);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
author = _author.authorID;
|
|
||||||
|
|
||||||
//create the token2author relation
|
|
||||||
db.set("token2author:" + token, author);
|
|
||||||
|
|
||||||
callback();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
//there is a author with this token
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//update the timestamp of this author
|
|
||||||
db.setSub("globalAuthor:" + author, ["timestamp"], new Date().getTime());
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
], function(err)
|
|
||||||
{
|
{
|
||||||
callback(err, author);
|
//return only the sub value authorID
|
||||||
|
callback(err, author ? author.authorID : author);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the AuthorID for a mapper.
|
||||||
|
* @param {String} token The mapper
|
||||||
|
* @param {Function} callback callback (err, author)
|
||||||
|
*/
|
||||||
|
exports.getMappedAuthor4 = function (authorMapper, name, callback)
|
||||||
|
{
|
||||||
|
mapAuthorWithDBKey("mapper2author", authorMapper, function(err, author)
|
||||||
|
{
|
||||||
|
//error?
|
||||||
|
if(err)
|
||||||
|
{
|
||||||
|
callback(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//set the name of this author
|
||||||
|
if(name)
|
||||||
|
exports.setAuthorName(author.authorID, name);
|
||||||
|
|
||||||
|
//return the authorID
|
||||||
|
callback(null, author);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the AuthorID for a mapper. We can map using a mapperkey,
|
||||||
|
* so far this is token2author and mapper2author
|
||||||
|
* @param {String} mapperkey The database key name for this mapper
|
||||||
|
* @param {String} mapper The mapper
|
||||||
|
* @param {Function} callback callback (err, author)
|
||||||
|
*/
|
||||||
|
function mapAuthorWithDBKey (mapperkey, mapper, callback)
|
||||||
|
{
|
||||||
|
//try to map to an author
|
||||||
|
db.get(mapperkey + ":" + mapper, function (err, author)
|
||||||
|
{
|
||||||
|
//error?
|
||||||
|
if(err)
|
||||||
|
{
|
||||||
|
callback(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//there is no author with this mapper, so create one
|
||||||
|
if(author == null)
|
||||||
|
{
|
||||||
|
exports.createAuthor(null, function(err, author)
|
||||||
|
{
|
||||||
|
//error?
|
||||||
|
if(err)
|
||||||
|
{
|
||||||
|
callback(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//create the token2author relation
|
||||||
|
db.set(mapperkey + ":" + mapper, author.authorID);
|
||||||
|
|
||||||
|
//return the author
|
||||||
|
callback(null, author);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
//there is a author with this mapper
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//update the timestamp of this author
|
||||||
|
db.setSub("globalAuthor:" + author, ["timestamp"], new Date().getTime());
|
||||||
|
|
||||||
|
//return the author
|
||||||
|
callback(null, {authorID: author});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -42,7 +42,7 @@ var functions = {
|
||||||
"createPad" : ["padID", "text"],
|
"createPad" : ["padID", "text"],
|
||||||
"createGroupPad" : ["groupID", "padName", "text"],
|
"createGroupPad" : ["groupID", "padName", "text"],
|
||||||
"createAuthor" : ["name"],
|
"createAuthor" : ["name"],
|
||||||
// "getMappedAuthor4" : ["authorMapper" , "name"],
|
"getMappedAuthor4" : ["authorMapper" , "name"],
|
||||||
// "createSession" : ["groupID", "authorID", "validUntil"],
|
// "createSession" : ["groupID", "authorID", "validUntil"],
|
||||||
// "deleteSession" : ["sessionID"],
|
// "deleteSession" : ["sessionID"],
|
||||||
// "getSessionInfo" : ["sessionID"],
|
// "getSessionInfo" : ["sessionID"],
|
||||||
|
|
Loading…
Reference in a new issue