From 6f9d7a5db79076747584ebd289bf380b2bb95038 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 27 Jun 2012 18:23:17 +0200 Subject: [PATCH] Add 2 new APIs: listPadsOfAuthor and listAuthorsOfPad Return all pads that a given author has contributed to (not just created) and return all authors who has contributed to a given pad. --- src/node/db/API.js | 21 ++++++++ src/node/db/AuthorManager.js | 91 ++++++++++++++++++++++++++++++++++ src/node/db/Pad.js | 16 ++++++ src/node/handler/APIHandler.js | 6 ++- 4 files changed, 132 insertions(+), 2 deletions(-) diff --git a/src/node/db/API.js b/src/node/db/API.js index 37fd3f16..c766c80a 100644 --- a/src/node/db/API.js +++ b/src/node/db/API.js @@ -47,6 +47,7 @@ exports.createGroupPad = groupManager.createGroupPad; exports.createAuthor = authorManager.createAuthor; exports.createAuthorIfNotExistsFor = authorManager.createAuthorIfNotExistsFor; +exports.listPadsOfAuthor = authorManager.listPadsOfAuthor; /**********************/ /**SESSION FUNCTIONS***/ @@ -463,6 +464,26 @@ exports.isPasswordProtected = function(padID, callback) }); } +/** +listAuthorsOfPad(padID) returns an array of authors who contributed to this pad + +Example returns: + +{code: 0, message:"ok", data: {authorIDs : ["a.s8oes9dhwrvt0zif", "a.akf8finncvomlqva"]} +{code: 1, message:"padID does not exist", data: null} +*/ +exports.listAuthorsOfPad = function(padID, callback) +{ + //get the pad + getPadSafe(padID, true, function(err, pad) + { + if(ERR(err, callback)) return; + + callback(null, {authorIDs: pad.getAllAuthors()}); + }); +} + + /******************************/ /** INTERNAL HELPER FUNCTIONS */ /******************************/ diff --git a/src/node/db/AuthorManager.js b/src/node/db/AuthorManager.js index f644de12..99f9444b 100644 --- a/src/node/db/AuthorManager.js +++ b/src/node/db/AuthorManager.js @@ -55,6 +55,7 @@ exports.getAuthor4Token = function (token, callback) /** * Returns the AuthorID for a mapper. * @param {String} token The mapper + * @param {String} name The name of the author (optional) * @param {Function} callback callback (err, author) */ exports.createAuthorIfNotExistsFor = function (authorMapper, name, callback) @@ -153,6 +154,7 @@ exports.getAuthorColorId = function (author, callback) /** * Sets the color Id of the author * @param {String} author The id of the author + * @param {String} colorId The color id of the author * @param {Function} callback (optional) */ exports.setAuthorColorId = function (author, colorId, callback) @@ -163,6 +165,7 @@ exports.setAuthorColorId = function (author, colorId, callback) /** * Returns the name of the author * @param {String} author The id of the author + * @param {String} name The name of the author * @param {Function} callback callback(err, name) */ exports.getAuthorName = function (author, callback) @@ -179,3 +182,91 @@ exports.setAuthorName = function (author, name, callback) { db.setSub("globalAuthor:" + author, ["name"], name, callback); } + +/** + * Returns an array of all pads this author contributed to + * @param {String} author The id of the author + * @param {String} name The name of the author + * @param {Function} callback (optional) + */ +exports.listPadsOfAuthor = function (authorID, callback) +{ + /* There are two other places where this array is manipulated: + * (1) When the author is added to a pad, the author object is also updated + * (2) When a pad is deleted, each author of that pad is also updated + */ + //get the globalAuthor + db.get("globalAuthor:" + authorID, function(err, author) + { + if(ERR(err, callback)) return; + + //author does not exists + if(author == null) + { + callback(new customError("authorID does not exist","apierror")) + } + //everything is fine, return the pad IDs + else + { + var pads = []; + if(author.padIDs != null) + { + for (var padId in author.padIDs) + { + pads.push(padId); + } + } + callback(null, {padIDs: pads}); + } + }); +} + +/** + * Adds a new pad to the list of contributions + * @param {String} author The id of the author + * @param {String} padID The id of the pad the author contributes to + * @param {Function} callback (optional) + */ +exports.addPad = function (authorID, padID) +{ + //get the entry + db.get("globalAuthor:" + authorID, function(err, author) + { + if(ERR(err)) return; + if(author == null) return; + + //the entry doesn't exist so far, let's create it + if(author.padIDs == null) + { + author.padIDs = {padIDs : {}}; + } + + //add the entry for this pad + author.padIDs[padID] = 1; + + //save the new element back + db.set("globalAuthor:" + authorID, author); + }); +} + +/** + * Removes a pad from the list of contributions + * @param {String} author The id of the author + * @param {String} padID The id of the pad the author contributes to + * @param {Function} callback (optional) + */ +exports.removePad = function (authorID, padID) +{ + db.get("globalAuthor:" + authorID, function (err, author) + { + if(ERR(err)) return; + if(author == null) return; + + if(author.padIDs != null) + { + //remove pad from author + delete author.padIDs[padID]; + db.set("globalAuthor:" + authorID, author); + } + }); +} \ No newline at end of file diff --git a/src/node/db/Pad.js b/src/node/db/Pad.js index b4a39c17..5e005606 100644 --- a/src/node/db/Pad.js +++ b/src/node/db/Pad.js @@ -82,6 +82,10 @@ Pad.prototype.appendRevision = function appendRevision(aChangeset, author) { db.set("pad:"+this.id+":revs:"+newRev, newRevData); this.saveToDatabase(); + + // set the author to pad + if(author != '') + authorManager.addPad(author, this.id); }; //save all attributes to the database @@ -436,6 +440,18 @@ Pad.prototype.remove = function remove(callback) { db.remove("pad:"+padID+":revs:"+i); } + callback(); + }, + //remove pad from all authors who contributed + function(callback) + { + var authorIDs = _this.getAllAuthors(); + + authorIDs.forEach(function (authorID) + { + authorManager.removePad(authorID, padID); + }); + callback(); } ], callback); diff --git a/src/node/handler/APIHandler.js b/src/node/handler/APIHandler.js index 98b1ed16..b6bb93bd 100644 --- a/src/node/handler/APIHandler.js +++ b/src/node/handler/APIHandler.js @@ -40,13 +40,14 @@ catch(e) //a list of all functions var functions = { "createGroup" : [], - "createGroupIfNotExistsFor" : ["groupMapper"], + "createGroupIfNotExistsFor" : ["groupMapper"], "deleteGroup" : ["groupID"], "listPads" : ["groupID"], "createPad" : ["padID", "text"], "createGroupPad" : ["groupID", "padName", "text"], "createAuthor" : ["name"], "createAuthorIfNotExistsFor": ["authorMapper" , "name"], + "listPadsOfAuthor" : ["authorID"], "createSession" : ["groupID", "authorID", "validUntil"], "deleteSession" : ["sessionID"], "getSessionInfo" : ["sessionID"], @@ -62,7 +63,8 @@ var functions = { "setPublicStatus" : ["padID", "publicStatus"], "getPublicStatus" : ["padID"], "setPassword" : ["padID", "password"], - "isPasswordProtected" : ["padID"] + "isPasswordProtected" : ["padID"], + "listAuthorsOfPad" : ["padID"] }; /**