Browse files

add listPadsOfAuthor API method

  • Loading branch information...
1 parent 48ad88b commit 08a08d6f9a30a7b89a536182e44bc9cb41834eba @johndbritton committed Jul 21, 2012
Showing with 104 additions and 1 deletion.
  1. +1 −0 node/db/API.js
  2. +85 −0 node/db/AuthorManager.js
  3. +16 −0 node/db/Pad.js
  4. +2 −1 node/handler/APIHandler.js
View
1 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***/
View
85 node/db/AuthorManager.js
@@ -179,3 +179,88 @@ 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
+ */
+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 = {};
+ }
+
+ //add the entry for this pad
+ author.padIDs[padID] = 1;// anything, because value is not used
+
+ //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
+ */
+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
16 node/db/Pad.js
@@ -82,6 +82,10 @@ 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) {
@@ -433,6 +437,18 @@ Pad.prototype.remove = function remove(callback) {
}
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
3 node/handler/APIHandler.js
@@ -53,7 +53,8 @@ var functions = {
// "setPublicStatus" : ["padID", "publicStatus"],
// "getPublicStatus" : ["padID"],
// "setPassword" : ["padID", "password"],
-// "isPasswordProtected" : ["padID"]
+// "isPasswordProtected" : ["padID"],
+ "listPadsOfAuthor" : ["authorID"]
};
/**

0 comments on commit 08a08d6

Please sign in to comment.