mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-04-21 07:56:16 -04:00
added getMappedGroup4 and createGroup
This commit is contained in:
parent
de0e341b4b
commit
178b4a95ec
4 changed files with 101 additions and 10 deletions
|
@ -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});
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue