From 178b4a95ecf0a51d411b5a81507640c577e7a87f Mon Sep 17 00:00:00 2001 From: Peter 'Pita' Martischka Date: Mon, 8 Aug 2011 16:21:31 +0100 Subject: [PATCH] added getMappedGroup4 and createGroup --- node/db/API.js | 11 ++--- node/db/GroupManager.js | 96 +++++++++++++++++++++++++++++++++++++ node/handler/APIHandler.js | 4 +- static/favicon.ico | Bin 0 -> 1150 bytes 4 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 static/favicon.ico diff --git a/node/db/API.js b/node/db/API.js index 611826d3f..c8f1b9ca9 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 2ceb1b656..2fcd31c80 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 383930c7c..f91e92ab7 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 0000000000000000000000000000000000000000..2529c923f30e7ad806630c1ec40c4ca9db439d64 GIT binary patch literal 1150 zcmc)J&q@MO6vy$S1hx=!5ki9KvS?#%+GrSr&;ukPRNI#A16OVW-ABDZi(Vj6(kE!u zDk!3;Aczo+Bx?E|?p(O&6ye4rAJ6^cZ^ju%kr1zVTzJpO-jql}MCQQ~D`uVcTWzuN zjV9Wt;0ztux?*tZPr`H$m$-oDyKw72`Me((H?QA8jhio#P5+T^v$um=4A8?JmZ0Z- zgzo9>&cUrkCRrp=7!qT?=BzQCXsvK|y&mg;@q$064V4%5#AWXPC!Z=~7aLf|14hfu z>e%Gp>C0F}1VwzHfn%g#Dmbslxxo|Wp!rv9LyvI}Q^9%tZD*0e3^f0YB&v9Uso=c5 yciQ#;qu0^L7p}1hQ=@siGm(CHByxTN+oyRDxC*QOCelK4U!=|x-9nAA-Twi9>5l{e literal 0 HcmV?d00001