added createPad

This commit is contained in:
Peter 'Pita' Martischka 2011-08-04 19:20:14 +01:00
parent 8dabdc8e95
commit 9fa52c0e66
4 changed files with 102 additions and 32 deletions

View file

@ -78,7 +78,7 @@ exports.listPads = function(groupID, callback)
}
/**
createPad(groupID, padName [, text]) creates a new pad in this group
createGroupPad(groupID, padName [, text]) creates a new pad in this group
Example returns:
@ -86,7 +86,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.createPad = function(groupID, padName, text, callback)
exports.createGroupPad = function(groupID, padName, text, callback)
{
}
@ -268,7 +268,7 @@ exports.getText = function(padID, rev, callback)
}
//get the pad
getPadSafe(padID, function(err, pad)
getPadSafe(padID, true, function(err, pad)
{
if(err)
{
@ -316,22 +316,16 @@ Example returns:
*/
exports.setText = function(padID, text, callback)
{
//check if text is a string
if(typeof text != "string")
//check the text
var textCheck = checkPadText(text);
if(textCheck != null)
{
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"})
callback(textCheck);
return;
}
//get the pad
getPadSafe(padID, function(err, pad)
getPadSafe(padID, true, function(err, pad)
{
if(err)
{
@ -362,7 +356,7 @@ Example returns:
exports.getRevisionsCount = function(padID, callback)
{
//get the pad
getPadSafe(padID, function(err, pad)
getPadSafe(padID, true, function(err, pad)
{
if(err)
{
@ -374,6 +368,41 @@ exports.getRevisionsCount = function(padID, callback)
});
}
/**
createPad(padName [, text]) creates a new pad in this group
Example returns:
{code: 0, message:"ok", data: null}
{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)
{
callback({stop: "createPad can't create group pads"});
return;
}
//create pad
getPadSafe(padID, false, text, function(err)
{
callback(err);
});
}
/**
deletePad(padID) deletes a pad
@ -385,7 +414,7 @@ Example returns:
exports.deletePad = function(padID, callback)
{
//get the pad
getPadSafe(padID, function(err, pad)
getPadSafe(padID, true, function(err, pad)
{
if(err)
{
@ -408,7 +437,7 @@ Example returns:
exports.getReadOnlyID = function(padID, callback)
{
//we don't need the pad object, but this function does all the security stuff for us
getPadSafe(padID, function(err)
getPadSafe(padID, true, function(err)
{
if(err)
{
@ -435,7 +464,7 @@ Example returns:
exports.setPublicStatus = function(padID, publicStatus, callback)
{
//get the pad
getPadSafe(padID, function(err, pad)
getPadSafe(padID, true, function(err, pad)
{
if(err)
{
@ -458,7 +487,7 @@ Example returns:
exports.getPublicStatus = function(padID, callback)
{
//get the pad
getPadSafe(padID, function(err, pad)
getPadSafe(padID, true, function(err, pad)
{
if(err)
{
@ -481,7 +510,7 @@ Example returns:
exports.setPassword = function(padID, password, callback)
{
//get the pad
getPadSafe(padID, function(err, pad)
getPadSafe(padID, true, function(err, pad)
{
if(err)
{
@ -504,7 +533,7 @@ Example returns:
exports.isPasswordProtected = function(padID, callback)
{
//get the pad
getPadSafe(padID, function(err, pad)
getPadSafe(padID, true, function(err, pad)
{
if(err)
{
@ -526,9 +555,32 @@ function is_int(value)
return (parseFloat(value) == parseInt(value)) && !isNaN(value)
}
//gets a pad safe
function getPadSafe(padID, callback)
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)
{
if(typeof text == "function")
{
callback = text;
text = null;
}
//check if padID is a string
if(typeof padID != "string")
{
@ -551,15 +603,20 @@ function getPadSafe(padID, callback)
{
callback(err);
}
//does not exists
else if(exists == false)
//does not exist, but should
else if(exists == false && shouldExist == true)
{
callback({stop: "padID does not exist"});
}
//does exists, but shouldn't
else if(exists == true && shouldExist == false)
{
callback({stop: "padID does already exist"});
}
//pad exists, let's get it
else
{
padManager.getPad(padID, callback);
padManager.getPad(padID, text, callback);
}
});
}

View file

@ -310,10 +310,16 @@ Class('Pad', {
});
},
init : function (callback)
init : function (text, callback)
{
var _this = this;
//replace text with default text if text isn't set
if(text == null)
{
text = settings.defaultPadText;
}
//try to load the pad
db.get("pad:"+this.id, function(err, value)
{
@ -338,7 +344,7 @@ Class('Pad', {
//this pad doesn't exist, so create it
else
{
var firstChangeset = Changeset.makeSplice("\n", 0, 0, exports.cleanText(settings.defaultPadText));
var firstChangeset = Changeset.makeSplice("\n", 0, 0, exports.cleanText(text));
_this.appendRevision(firstChangeset, '');
}

View file

@ -31,11 +31,18 @@ globalPads = [];
* @param id A String with the id of the pad
* @param {Function} callback
*/
exports.getPad = function(id, callback)
exports.getPad = function(id, text, callback)
{
if(!exports.isValidPadId(id))
throw new Error(id + " is not a valid padId");
//make text an optional parameter
if(typeof text == "function")
{
callback = text;
text = null;
}
var pad = globalPads[id];
//return pad if its already loaded
@ -49,7 +56,7 @@ exports.getPad = function(id, callback)
pad = new Pad(id);
//initalize the pad
pad.init(function(err)
pad.init(text, function(err)
{
if(err)
{

View file

@ -39,7 +39,7 @@ var functions = {
// "getMappedGroup4" : ["groupMapper"],
// "deleteGroup" : ["groupID"],
// "listPads" : ["groupID"],
// "createPad" : ["padName", "text"],
"createPad" : ["padID", "text"],
// "createGroupPad" : ["groupID", "padName", "text"],
// "createAuthor" : ["name"],
// "getMappedAuthor4" : ["authorMapper" , "name"],
@ -54,7 +54,7 @@ var functions = {
"setText" : ["padID", "text"],
"getRevisionsCount" : ["padID"],
// "deletePad" : ["padID"],
"getReadOnlyID" : ["padID"],
"getReadOnlyID" : ["padID"]
// "setPublicStatus" : ["padID", "publicStatus"],
// "getPublicStatus" : ["padID"],
// "setPassword" : ["padID", "password"],