mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-02-01 03:12:42 +01:00
added setText and simplified getText
This commit is contained in:
parent
820c18c7e9
commit
d5d9830dd3
2 changed files with 146 additions and 71 deletions
213
node/db/API.js
213
node/db/API.js
|
@ -19,6 +19,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var padManager = require("./PadManager");
|
var padManager = require("./PadManager");
|
||||||
|
var padMessageHandler = require("../handler/PadMessageHandler");
|
||||||
var async = require("async");
|
var async = require("async");
|
||||||
|
|
||||||
/**********************/
|
/**********************/
|
||||||
|
@ -236,13 +237,6 @@ exports.getText = function(padID, rev, callback)
|
||||||
rev = undefined;
|
rev = undefined;
|
||||||
}
|
}
|
||||||
|
|
||||||
//check if padID is a string
|
|
||||||
if(typeof padID != "string")
|
|
||||||
{
|
|
||||||
callback({stop: "padID is not a string"});
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
//check if rev is a number
|
//check if rev is a number
|
||||||
if(rev !== undefined && typeof rev != "number")
|
if(rev !== undefined && typeof rev != "number")
|
||||||
{
|
{
|
||||||
|
@ -272,68 +266,41 @@ exports.getText = function(padID, rev, callback)
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var pad;
|
//get the pad
|
||||||
var data;
|
getPadSafe(padID, function(err, pad)
|
||||||
|
{
|
||||||
async.series([
|
if(err)
|
||||||
//check if pad exists
|
|
||||||
function(callback)
|
|
||||||
{
|
{
|
||||||
padManager.doesPadExists(padID, function(err, exists)
|
callback(err);
|
||||||
{
|
return;
|
||||||
if(err)
|
}
|
||||||
{
|
|
||||||
callback(err);
|
//the client asked for a special revision
|
||||||
}
|
if(rev !== undefined)
|
||||||
else
|
|
||||||
{
|
|
||||||
callback(exists == false ? {stop: "padID does not exist"} : null)
|
|
||||||
}
|
|
||||||
});
|
|
||||||
},
|
|
||||||
//get the pad object
|
|
||||||
function(callback)
|
|
||||||
{
|
{
|
||||||
padManager.getPad(padID, function(err, _pad)
|
//check if this is a valid revision
|
||||||
|
if(rev > pad.getHeadRevisionNumber())
|
||||||
{
|
{
|
||||||
pad=_pad;
|
callback({stop: "rev is higher than the head revision of the pad"});
|
||||||
callback(err);
|
return;
|
||||||
});
|
}
|
||||||
},
|
|
||||||
//return the text
|
//get the text of this revision
|
||||||
function(callback)
|
pad.getInternalRevisionAText(rev, function(err, atext)
|
||||||
{
|
|
||||||
//the client asked for a special revision
|
|
||||||
if(rev !== undefined)
|
|
||||||
{
|
{
|
||||||
//check if this is a valid revision
|
if(!err)
|
||||||
if(rev > pad.getHeadRevisionNumber())
|
|
||||||
{
|
{
|
||||||
callback({stop: "rev is higher than the head revision of the pad"});
|
data = {text: atext.text};
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//get the text of this revision
|
callback(err, data);
|
||||||
pad.getInternalRevisionAText(rev, function(err, atext)
|
})
|
||||||
{
|
}
|
||||||
if(!err)
|
//the client wants the latest text, lets return it to him
|
||||||
{
|
else
|
||||||
data = {text: atext.text};
|
{
|
||||||
}
|
callback(null, {"text": pad.text()});
|
||||||
|
|
||||||
callback(err);
|
|
||||||
})
|
|
||||||
}
|
|
||||||
//the client wants the latest text, lets return it to him
|
|
||||||
else
|
|
||||||
{
|
|
||||||
data = {"text": pad.text()};
|
|
||||||
callback();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
], function(err)
|
|
||||||
{
|
|
||||||
callback(err, data)
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -347,8 +314,36 @@ Example returns:
|
||||||
{code: 1, message:"text too long", data: null}
|
{code: 1, message:"text too long", data: null}
|
||||||
*/
|
*/
|
||||||
exports.setText = function(padID, text, callback)
|
exports.setText = function(padID, text, callback)
|
||||||
{
|
{
|
||||||
|
//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;
|
||||||
|
}
|
||||||
|
|
||||||
|
//get the pad
|
||||||
|
getPadSafe(padID, function(err, pad)
|
||||||
|
{
|
||||||
|
if(err)
|
||||||
|
{
|
||||||
|
callback(err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//set the text
|
||||||
|
pad.setText(text);
|
||||||
|
|
||||||
|
//update the clients on the pad
|
||||||
|
padMessageHandler.updatePadClients(pad, callback);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/*****************/
|
/*****************/
|
||||||
|
@ -365,7 +360,13 @@ Example returns:
|
||||||
*/
|
*/
|
||||||
exports.getRevisionsCount = function(padID, callback)
|
exports.getRevisionsCount = function(padID, callback)
|
||||||
{
|
{
|
||||||
|
//check if this is a valid padID
|
||||||
|
var notValidReason = isValidPadID(padID);
|
||||||
|
if(notValidReason != null)
|
||||||
|
{
|
||||||
|
callback(notValidReason);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -378,7 +379,13 @@ Example returns:
|
||||||
*/
|
*/
|
||||||
exports.deletePad = function(padID, callback)
|
exports.deletePad = function(padID, callback)
|
||||||
{
|
{
|
||||||
|
//check if this is a valid padID
|
||||||
|
var notValidReason = isValidPadID(padID);
|
||||||
|
if(notValidReason != null)
|
||||||
|
{
|
||||||
|
callback(notValidReason);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -391,7 +398,13 @@ Example returns:
|
||||||
*/
|
*/
|
||||||
exports.getReadOnlyLink = function(padID, callback)
|
exports.getReadOnlyLink = function(padID, callback)
|
||||||
{
|
{
|
||||||
|
//check if this is a valid padID
|
||||||
|
var notValidReason = isValidPadID(padID);
|
||||||
|
if(notValidReason != null)
|
||||||
|
{
|
||||||
|
callback(notValidReason);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -404,7 +417,13 @@ Example returns:
|
||||||
*/
|
*/
|
||||||
exports.setPublicStatus = function(padID, publicStatus, callback)
|
exports.setPublicStatus = function(padID, publicStatus, callback)
|
||||||
{
|
{
|
||||||
|
//check if this is a valid padID
|
||||||
|
var notValidReason = isValidPadID(padID);
|
||||||
|
if(notValidReason != null)
|
||||||
|
{
|
||||||
|
callback(notValidReason);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -417,7 +436,13 @@ Example returns:
|
||||||
*/
|
*/
|
||||||
exports.getPublicStatus = function(padID, callback)
|
exports.getPublicStatus = function(padID, callback)
|
||||||
{
|
{
|
||||||
|
//check if this is a valid padID
|
||||||
|
var notValidReason = isValidPadID(padID);
|
||||||
|
if(notValidReason != null)
|
||||||
|
{
|
||||||
|
callback(notValidReason);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -430,7 +455,13 @@ Example returns:
|
||||||
*/
|
*/
|
||||||
exports.setPassword = function(padID, password, callback)
|
exports.setPassword = function(padID, password, callback)
|
||||||
{
|
{
|
||||||
|
//check if this is a valid padID
|
||||||
|
var notValidReason = isValidPadID(padID);
|
||||||
|
if(notValidReason != null)
|
||||||
|
{
|
||||||
|
callback(notValidReason);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -443,7 +474,13 @@ Example returns:
|
||||||
*/
|
*/
|
||||||
exports.isPasswordProtected = function(padID, callback)
|
exports.isPasswordProtected = function(padID, callback)
|
||||||
{
|
{
|
||||||
|
//check if this is a valid padID
|
||||||
|
var notValidReason = isValidPadID(padID);
|
||||||
|
if(notValidReason != null)
|
||||||
|
{
|
||||||
|
callback(notValidReason);
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/******************************/
|
/******************************/
|
||||||
|
@ -455,3 +492,41 @@ function is_int(value)
|
||||||
{
|
{
|
||||||
return (parseFloat(value) == parseInt(value)) && !isNaN(value)
|
return (parseFloat(value) == parseInt(value)) && !isNaN(value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//gets a pad safe
|
||||||
|
function getPadSafe(padID, callback)
|
||||||
|
{
|
||||||
|
//check if padID is a string
|
||||||
|
if(typeof padID != "string")
|
||||||
|
{
|
||||||
|
callback({stop: "padID is not a string"});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//check if the padID maches the requirements
|
||||||
|
if(!padManager.isValidPadId(padID))
|
||||||
|
{
|
||||||
|
callback({stop: "padID did not match requirements"});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
//check if the pad exists
|
||||||
|
padManager.doesPadExists(padID, function(err, exists)
|
||||||
|
{
|
||||||
|
//error
|
||||||
|
if(err)
|
||||||
|
{
|
||||||
|
callback(err);
|
||||||
|
}
|
||||||
|
//does not exists
|
||||||
|
else if(exists == false)
|
||||||
|
{
|
||||||
|
callback({stop: "padID does not exist"});
|
||||||
|
}
|
||||||
|
//pad exists, let's get it
|
||||||
|
else
|
||||||
|
{
|
||||||
|
padManager.getPad(padID, callback);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
|
@ -49,8 +49,8 @@ var functions = {
|
||||||
// "listSessionsOfAuthor" : ["authorID"],
|
// "listSessionsOfAuthor" : ["authorID"],
|
||||||
// "deleteAllSessionsOfGroup" : ["groupID"],
|
// "deleteAllSessionsOfGroup" : ["groupID"],
|
||||||
// "deleteAllSessionsOfAuthor" : ["authorID"],
|
// "deleteAllSessionsOfAuthor" : ["authorID"],
|
||||||
"getText" : ["padID", "rev"]
|
"getText" : ["padID", "rev"],
|
||||||
// "setText" : ["padID", "text"]
|
"setText" : ["padID", "text"]
|
||||||
// "getRevisionsCount" : ["padID"],
|
// "getRevisionsCount" : ["padID"],
|
||||||
// "deletePad" : ["padID"],
|
// "deletePad" : ["padID"],
|
||||||
// "getReadOnlyLink" : ["padID"],
|
// "getReadOnlyLink" : ["padID"],
|
||||||
|
|
Loading…
Reference in a new issue