Skip to content
Closed
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
22 changes: 21 additions & 1 deletion 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 @@ -94,7 +95,7 @@ exports.getText = function(padID, rev, callback)
}
}

//ensure this is not a negativ number
//ensure this is not a negative number
if(rev !== undefined && rev < 0)
{
callback(new customError("rev is a negativ number","apierror"));
Expand Down Expand Up @@ -463,6 +464,25 @@ 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
90 changes: 90 additions & 0 deletions 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 @@ -173,9 +175,97 @@ exports.getAuthorName = function (author, callback)
/**
* Sets the name of the author
* @param {String} author The id of the author
* @param {String} name The name of the author
* @param {Function} callback (optional)
*/
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 {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);
}
});
}
15 changes: 15 additions & 0 deletions node/db/Pad.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ Pad.prototype.appendRevision = function appendRevision(aChangeset, author) {
chatHead: this.chatHead,
publicStatus: this.publicStatus,
passwordHash: this.passwordHash});
// set the author to pad
if(author != '')
authorManager.addPad(author, this.id);
};

Pad.prototype.getRevisionChangeset = function getRevisionChangeset(revNum, callback) {
Expand Down Expand Up @@ -432,6 +435,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 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