summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJohn McLear <john@mclear.co.uk>2012-06-27 11:31:25 -0700
committerJohn McLear <john@mclear.co.uk>2012-06-27 11:31:25 -0700
commita717c11ab363e2e37778a0b13ad4f70b83aef8d6 (patch)
treef679f45f6d53b61448d617b2b404ba3cf0639a7c /src
parentb466e37917aa710aba0f6bbb244355cd6d930b46 (diff)
parent6f9d7a5db79076747584ebd289bf380b2bb95038 (diff)
downloadetherpad-lite-a717c11ab363e2e37778a0b13ad4f70b83aef8d6.zip
Merge pull request #819 from marcelklehr/contributor-api-methods
Contributor api methods (listPadsOfAuthor & listAuthorsOfPad )
Diffstat (limited to 'src')
-rw-r--r--src/node/db/API.js21
-rw-r--r--src/node/db/AuthorManager.js91
-rw-r--r--src/node/db/Pad.js16
-rw-r--r--src/node/handler/APIHandler.js6
4 files changed, 132 insertions, 2 deletions
diff --git a/src/node/db/API.js b/src/node/db/API.js
index b3cffbe3..e2b6f6f8 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***/
@@ -481,6 +482,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 b486a44d..82159676 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
@@ -443,6 +447,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);
},
diff --git a/src/node/handler/APIHandler.js b/src/node/handler/APIHandler.js
index a7b1a8ab..567a90d2 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"],
@@ -63,7 +64,8 @@ var functions = {
"setPublicStatus" : ["padID", "publicStatus"],
"getPublicStatus" : ["padID"],
"setPassword" : ["padID", "password"],
- "isPasswordProtected" : ["padID"]
+ "isPasswordProtected" : ["padID"],
+ "listAuthorsOfPad" : ["padID"]
};
/**