added the listPads and createGroupPad

pull/85/merge
Peter 'Pita' Martischka 2011-08-08 17:35:40 +01:00
parent 8d28fcbf23
commit 4670cbc60a
4 changed files with 128 additions and 49 deletions

View File

@ -67,10 +67,7 @@ Example returns:
{code: 0, message:"ok", data: {padIDs : ["3$test", "3$test2"]}
{code: 1, message:"There is no group for this groupID", data: null}
*/
exports.listPads = function(groupID, callback)
{
}
exports.listPads = groupManager.listPads;
/**
createGroupPad(groupID, padName [, text]) creates a new pad in this group
@ -81,10 +78,7 @@ Example returns:
{code: 1, message:"pad does already exist", data: null}
{code: 1, message:"There is no group for this groupID", data: null}
*/
exports.createGroupPad = function(groupID, padName, text, callback)
{
}
exports.createGroupPad = groupManager.createGroupPad;
/**********************/
/**AUTHOR FUNCTIONS****/
@ -310,15 +304,7 @@ Example returns:
{code: 1, message:"text too long", data: null}
*/
exports.setText = function(padID, text, callback)
{
//check the text
var textCheck = checkPadText(text);
if(textCheck != null)
{
callback(textCheck);
return;
}
{
//get the pad
getPadSafe(padID, true, function(err, pad)
{
@ -372,18 +358,7 @@ Example returns:
{code: 1, message:"pad does already exist", data: null}
*/
exports.createPad = function(padID, text, callback)
{
if(text)
{
//check the text
var textCheck = checkPadText(text);
if(textCheck != null)
{
callback(textCheck);
return;
}
}
{
//ensure there is no $ in the padID
if(padID.indexOf("$") != -1)
{
@ -550,23 +525,6 @@ function is_int(value)
return (parseFloat(value) == parseInt(value)) && !isNaN(value)
}
function checkPadText(text)
{
//check if text is a string
if(typeof text != "string")
{
return {stop: "text is not a string"};
}
//check if text is less than 100k chars
if(text.length > 100000)
{
return {stop: "text must be less than 100k chars"};
}
return null;
}
//gets a pad safe
function getPadSafe(padID, shouldExist, text, callback)
{

View File

@ -20,6 +20,7 @@
var db = require("./DB").db;
var async = require("async");
var padManager = require("./PadManager");
exports.doesGroupExist = function(groupID, callback)
{
@ -102,6 +103,7 @@ exports.getMappedGroup4 = function(groupMapper, callback)
//create the mapper entry for this group
db.set("mapper2group:"+groupMapper, responseObj.groupID);
callback(null, responseObj);
});
}
@ -113,5 +115,102 @@ exports.getMappedGroup4 = function(groupMapper, callback)
});
}
exports.createGroupPad = function(groupID, padName, text, callback)
{
//create the padID
var padID = groupID + "$" + padName;
async.series([
//ensure group exists
function (callback)
{
exports.doesGroupExist(groupID, function(err, exists)
{
//error
if(err)
{
callback(err);
}
//group does not exist
else if(exists == false)
{
callback({stop: "groupID does not exist"});
}
//group exists, everything is fine
else
{
callback();
}
});
},
//ensure pad does not exists
function (callback)
{
padManager.doesPadExists(padID, function(err, exists)
{
//error
if(err)
{
callback(err);
}
//pad exists already
else if(exists == true)
{
callback({stop: "padName does already exist"});
}
//pad does not exist, everything is fine
else
{
callback();
}
});
},
//create the pad
function (callback)
{
padManager.getPad(padID, text, function(err)
{
callback(err);
});
},
//create an entry in the group for this pad
function (callback)
{
db.setSub("group:" + groupID, ["pads", padID], 1);
callback();
}
], function(err)
{
callback(err, {padID: padID});
});
//check if groupID exists
//check if pad already exists
//create the pad
//create the subentry in the padobject
}
exports.listPads = function(groupID, callback)
{
exports.doesGroupExist(groupID, function(err, exists)
{
//error
if(err)
{
callback(err);
}
//group does not exist
else if(exists == false)
{
callback({stop: "groupID does not exist"});
}
//group exists, let's get the pads
else
{
db.getSub("group:" + groupID, ["pads"], function(err, pads)
{
callback(err, {padIDs: pads});
});
}
});
}

View File

@ -33,8 +33,12 @@ globalPads = [];
*/
exports.getPad = function(id, text, callback)
{
//check if this is a valid padId
if(!exports.isValidPadId(id))
throw new Error(id + " is not a valid padId");
{
callback({stop: id + " is not a valid padId"});
return;
}
//make text an optional parameter
if(typeof text == "function")
@ -43,6 +47,24 @@ exports.getPad = function(id, text, callback)
text = null;
}
//check if this is a valid text
if(text != null)
{
//check if text is a string
if(typeof text != "string")
{
callback({stop: "text is not a string"});
return;
}
//check if text is less than 100k chars
if(text.length > 100000)
{
callback({stop: "text must be less than 100k chars"});
return;
}
}
var pad = globalPads[id];
//return pad if its already loaded

View File

@ -38,9 +38,9 @@ var functions = {
"createGroup" : [],
"getMappedGroup4" : ["groupMapper"],
// "deleteGroup" : ["groupID"],
// "listPads" : ["groupID"],
"listPads" : ["groupID"],
"createPad" : ["padID", "text"],
// "createGroupPad" : ["groupID", "padName", "text"],
"createGroupPad" : ["groupID", "padName", "text"],
// "createAuthor" : ["name"],
// "getMappedAuthor4" : ["authorMapper" , "name"],
// "createSession" : ["groupID", "authorID", "validUntil"],