From f01906d815d9fd8083113e52787aba2b2d8f8054 Mon Sep 17 00:00:00 2001 From: Peter 'Pita' Martischka Date: Tue, 9 Aug 2011 14:42:12 +0100 Subject: [PATCH] added getMappedAuthor4 --- node/db/API.js | 5 +- node/db/AuthorManager.js | 128 +++++++++++++++++++++++-------------- node/handler/APIHandler.js | 2 +- 3 files changed, 82 insertions(+), 53 deletions(-) diff --git a/node/db/API.js b/node/db/API.js index e3690671e..f9d1ec17d 100644 --- a/node/db/API.js +++ b/node/db/API.js @@ -102,10 +102,7 @@ Example returns: {code: 0, message:"ok", data: {authorID: 5}} */ -exports.getMappedAuthor4 = function(authorMapper ,name, callback) -{ - -} +exports.getMappedAuthor4 = authorManager.getMappedAuthor4; /**********************/ /**SESSION FUNCTIONS***/ diff --git a/node/db/AuthorManager.js b/node/db/AuthorManager.js index a6fc37287..abb44151c 100644 --- a/node/db/AuthorManager.js +++ b/node/db/AuthorManager.js @@ -22,59 +22,91 @@ var db = require("./DB").db; var async = require("async"); /** - * Returns the Author Id for a token. If the token is unkown, - * it creates a author for the token + * Returns the AuthorID for a token. * @param {String} token The token * @param {Function} callback callback (err, author) - * The callback function that is called when the result is here */ exports.getAuthor4Token = function (token, callback) -{ - var 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) +{ + mapAuthorWithDBKey("token2author", token, function(err, author) { - 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}); + } }); } diff --git a/node/handler/APIHandler.js b/node/handler/APIHandler.js index 0c12a271d..2368a47ca 100644 --- a/node/handler/APIHandler.js +++ b/node/handler/APIHandler.js @@ -42,7 +42,7 @@ var functions = { "createPad" : ["padID", "text"], "createGroupPad" : ["groupID", "padName", "text"], "createAuthor" : ["name"], -// "getMappedAuthor4" : ["authorMapper" , "name"], + "getMappedAuthor4" : ["authorMapper" , "name"], // "createSession" : ["groupID", "authorID", "validUntil"], // "deleteSession" : ["sessionID"], // "getSessionInfo" : ["sessionID"],