Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/node/db/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ exports.createGroupPad = groupManager.createGroupPad;

exports.createAuthor = authorManager.createAuthor;
exports.createAuthorIfNotExistsFor = authorManager.createAuthorIfNotExistsFor;
exports.listPadsOfAuthor = authorManager.listPadsOfAuthor;

/**********************/
/**SESSION FUNCTIONS***/
Expand Down Expand Up @@ -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 */
/******************************/
Expand Down
91 changes: 91 additions & 0 deletions src/node/db/AuthorManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpicking here, but in the actual function I don't see name as a param.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yeah, that was one too many...

I'll collect the suggested changes and apply them in the next commit

* @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);
}
});
}
16 changes: 16 additions & 0 deletions src/node/db/Pad.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/node/handler/APIHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Expand All @@ -62,7 +63,8 @@ var functions = {
"setPublicStatus" : ["padID", "publicStatus"],
"getPublicStatus" : ["padID"],
"setPassword" : ["padID", "password"],
"isPasswordProtected" : ["padID"]
"isPasswordProtected" : ["padID"],
"listAuthorsOfPad" : ["padID"]
};

/**
Expand Down