diff options
author | Peter 'Pita' Martischka <petermartischka@googlemail.com> | 2011-08-08 16:21:31 +0100 |
---|---|---|
committer | Peter 'Pita' Martischka <petermartischka@googlemail.com> | 2011-08-08 16:21:31 +0100 |
commit | 178b4a95ecf0a51d411b5a81507640c577e7a87f (patch) | |
tree | 588994012e63b6b8390c570789e823b10f822076 /node/db | |
parent | de0e341b4bc7739036c870a101ef5fea1a7673f6 (diff) | |
download | etherpad-lite-178b4a95ecf0a51d411b5a81507640c577e7a87f.zip |
added getMappedGroup4 and createGroup
Diffstat (limited to 'node/db')
-rw-r--r-- | node/db/API.js | 11 | ||||
-rw-r--r-- | node/db/GroupManager.js | 96 |
2 files changed, 99 insertions, 8 deletions
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}); + } + }); +} + + |