diff --git a/node/db/API.js b/node/db/API.js index 611826d3..c8f1b9ca 100644 --- a/node/db/API.js +++ b/node/db/API.js @@ -21,6 +21,7 @@ var padManager = require("./PadManager"); var padMessageHandler = require("../handler/PadMessageHandler"); var readOnlyManager = require("./ReadOnlyManager"); +var groupManager = require("./GroupManager"); var async = require("async"); /**********************/ @@ -34,10 +35,7 @@ Example returns: {code: 0, message:"ok", data: {groupID: 5}} */ -exports.createGroup = function (callback) -{ - -} +exports.createGroup = groupManager.createGroup; /** getMappedGroup4(groupMapper) this functions helps you to map your application group ids to etherpad lite group ids @@ -46,10 +44,7 @@ Example returns: {code: 0, message:"ok", data: {groupID: 7}} */ -exports.getMappedGroup4 = function (groupMapper, callback) -{ - -} +exports.getMappedGroup4 = groupManager.getMappedGroup4; /** deleteGroup(groupID) deletes a group diff --git a/node/db/GroupManager.js b/node/db/GroupManager.js index 2ceb1b65..2fcd31c8 100644 --- a/node/db/GroupManager.js +++ b/node/db/GroupManager.js @@ -18,4 +18,100 @@ * limitations under the License. */ +var db = require("./DB").db; +var async = require("async"); + +exports.doesGroupExist = function(groupID, callback) +{ + //try to get the group entry + db.get("group:" + groupID, function (err, group) + { + callback(err, group != null); + }); +} + +exports.createGroup = function(callback) +{ + //search for non existing groupID + var groupID; + var foundNonExistingGroupID = false; + async.whilst( + function () { return !foundNonExistingGroupID; }, + function (callback) + { + //generate a random 10 digit groupID + groupID = ""; + for(var i=0;i<10;i++) + { + groupID+=Math.floor(Math.random()*10); + } + + //check if this groupID already exisits + exports.doesGroupExist(groupID, function(err, exists) + { + foundNonExistingGroupID = !exists; + callback(err); + }) + }, + //we found a non existing groupID or an error happend + function (err) + { + //check for errors + if(err) + { + callback(err); + return; + } + + //create the group + db.set("group:" + groupID, {pads: {}}); + callback(null, {groupID: groupID}); + } + ); +} + +exports.getMappedGroup4 = function(groupMapper, callback) +{ + //ensure mapper is optional + if(typeof groupMapper != "string") + { + callback({stop: "groupMapper is no string"}); + return; + } + + //try to get a group for this mapper + db.get("mapper2group:"+groupMapper, function(err, groupID) + { + if(err) + { + callback(err); + return; + } + + //there is no group for this mapper, let's create a group + if(groupID == null) + { + exports.createGroup(function(err, responseObj) + { + //check for errors + if(err) + { + callback(err); + return; + } + + //create the mapper entry for this group + db.set("mapper2group:"+groupMapper, responseObj.groupID); + callback(null, responseObj); + }); + } + //there is a group for this mapper, let's return it + else + { + callback(err, {groupID: groupID}); + } + }); +} + + diff --git a/node/handler/APIHandler.js b/node/handler/APIHandler.js index 383930c7..f91e92ab 100644 --- a/node/handler/APIHandler.js +++ b/node/handler/APIHandler.js @@ -35,8 +35,8 @@ catch(e) //a list of all functions var functions = { -// "createGroup" : [], -// "getMappedGroup4" : ["groupMapper"], + "createGroup" : [], + "getMappedGroup4" : ["groupMapper"], // "deleteGroup" : ["groupID"], // "listPads" : ["groupID"], "createPad" : ["padID", "text"], diff --git a/static/favicon.ico b/static/favicon.ico new file mode 100644 index 00000000..2529c923 Binary files /dev/null and b/static/favicon.ico differ