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.
This commit is contained in:
Marcel Klehr 2012-06-27 18:23:17 +02:00
parent 0bc01feb72
commit 6f9d7a5db7
4 changed files with 132 additions and 2 deletions

View file

@ -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 */
/******************************/

View file

@ -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);
}
});
}

View file

@ -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);

View file

@ -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"]
};
/**