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: Example returns:
@ -86,7 +86,7 @@ Example returns:
{code: 1, message:"pad does already exist", data: null} {code: 1, message:"pad does already exist", data: null}
{code: 1, message:"There is no group for this groupID", 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 //get the pad
getPadSafe(padID, function(err, pad) getPadSafe(padID, true, function(err, pad)
{ {
if(err) if(err)
{ {
@ -316,22 +316,16 @@ Example returns:
*/ */
exports.setText = function(padID, text, callback) exports.setText = function(padID, text, callback)
{ {
//check if text is a string //check the text
if(typeof text != "string") var textCheck = checkPadText(text);
if(textCheck != null)
{ {
callback({stop: "text is not a string"}); callback(textCheck);
return;
}
//check if text is less than 100k chars
if(text.length > 100000)
{
callback({stop: "text must be less than 100k chars"})
return; return;
} }
//get the pad //get the pad
getPadSafe(padID, function(err, pad) getPadSafe(padID, true, function(err, pad)
{ {
if(err) if(err)
{ {
@ -362,7 +356,7 @@ Example returns:
exports.getRevisionsCount = function(padID, callback) exports.getRevisionsCount = function(padID, callback)
{ {
//get the pad //get the pad
getPadSafe(padID, function(err, pad) getPadSafe(padID, true, function(err, pad)
{ {
if(err) 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 deletePad(padID) deletes a pad
@ -385,7 +414,7 @@ Example returns:
exports.deletePad = function(padID, callback) exports.deletePad = function(padID, callback)
{ {
//get the pad //get the pad
getPadSafe(padID, function(err, pad) getPadSafe(padID, true, function(err, pad)
{ {
if(err) if(err)
{ {
@ -408,7 +437,7 @@ Example returns:
exports.getReadOnlyID = function(padID, callback) exports.getReadOnlyID = function(padID, callback)
{ {
//we don't need the pad object, but this function does all the security stuff for us //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) if(err)
{ {
@ -435,7 +464,7 @@ Example returns:
exports.setPublicStatus = function(padID, publicStatus, callback) exports.setPublicStatus = function(padID, publicStatus, callback)
{ {
//get the pad //get the pad
getPadSafe(padID, function(err, pad) getPadSafe(padID, true, function(err, pad)
{ {
if(err) if(err)
{ {
@ -458,7 +487,7 @@ Example returns:
exports.getPublicStatus = function(padID, callback) exports.getPublicStatus = function(padID, callback)
{ {
//get the pad //get the pad
getPadSafe(padID, function(err, pad) getPadSafe(padID, true, function(err, pad)
{ {
if(err) if(err)
{ {
@ -481,7 +510,7 @@ Example returns:
exports.setPassword = function(padID, password, callback) exports.setPassword = function(padID, password, callback)
{ {
//get the pad //get the pad
getPadSafe(padID, function(err, pad) getPadSafe(padID, true, function(err, pad)
{ {
if(err) if(err)
{ {
@ -504,7 +533,7 @@ Example returns:
exports.isPasswordProtected = function(padID, callback) exports.isPasswordProtected = function(padID, callback)
{ {
//get the pad //get the pad
getPadSafe(padID, function(err, pad) getPadSafe(padID, true, function(err, pad)
{ {
if(err) if(err)
{ {
@ -526,9 +555,32 @@ function is_int(value)
return (parseFloat(value) == parseInt(value)) && !isNaN(value) return (parseFloat(value) == parseInt(value)) && !isNaN(value)
} }
//gets a pad safe function checkPadText(text)
function getPadSafe(padID, callback)
{ {
//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 //check if padID is a string
if(typeof padID != "string") if(typeof padID != "string")
{ {
@ -551,15 +603,20 @@ function getPadSafe(padID, callback)
{ {
callback(err); callback(err);
} }
//does not exists //does not exist, but should
else if(exists == false) else if(exists == false && shouldExist == true)
{ {
callback({stop: "padID does not exist"}); 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 //pad exists, let's get it
else 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; var _this = this;
//replace text with default text if text isn't set
if(text == null)
{
text = settings.defaultPadText;
}
//try to load the pad //try to load the pad
db.get("pad:"+this.id, function(err, value) db.get("pad:"+this.id, function(err, value)
{ {
@ -338,7 +344,7 @@ Class('Pad', {
//this pad doesn't exist, so create it //this pad doesn't exist, so create it
else 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, ''); _this.appendRevision(firstChangeset, '');
} }

View file

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

View file

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