summaryrefslogtreecommitdiff
path: root/src/node/db
diff options
context:
space:
mode:
authorCristo <cristo.rabani@gmail.com>2014-11-12 19:46:50 +0100
committerCristo <cristo.rabani@gmail.com>2014-11-12 19:46:50 +0100
commit9d39c9591adab5b6638e16ee54e7f87a9147e015 (patch)
tree2ec1151f3f8260caf85b43157af97ba4ee32ebf6 /src/node/db
parent46bc328896877630169c4cea60431be981859352 (diff)
downloadetherpad-lite-9d39c9591adab5b6638e16ee54e7f87a9147e015.zip
update pad clients
Diffstat (limited to 'src/node/db')
-rw-r--r--src/node/db/API.js196
1 files changed, 101 insertions, 95 deletions
diff --git a/src/node/db/API.js b/src/node/db/API.js
index 8ad6e2eb..b3d46ea1 100644
--- a/src/node/db/API.js
+++ b/src/node/db/API.js
@@ -583,102 +583,108 @@ exports.deletePad = function(padID, callback)
{code:0, message:"ok", data:null}
{code: 1, message:"padID does not exist", data: null}
*/
-exports.restoreRevision = function(padID, rev, callback)
+exports.restoreRevision = function (padID, rev, callback)
{
- var Changeset = require("ep_etherpad-lite/static/js/Changeset");
-
- //check if rev is a number
- if(rev !== undefined && typeof rev != "number")
- {
- //try to parse the number
- if(!isNaN(parseInt(rev)))
- {
- rev = parseInt(rev);
- }
- else
- {
- callback(new customError("rev is not a number", "apierror"));
- return;
- }
- }
-
- //ensure this is not a negativ number
- if(rev !== undefined && rev < 0)
- {
- callback(new customError("rev is a negativ number","apierror"));
- return;
- }
-
- //ensure this is not a float value
- if(rev !== undefined && !is_int(rev))
- {
- callback(new customError("rev is a float value","apierror"));
- return;
- }
-
- //get the pad
- getPadSafe(padID, true, function(err, pad)
- {
- if(ERR(err, callback)) return;
-
-
- //check if this is a valid revision
- if(rev > pad.getHeadRevisionNumber())
- {
- callback(new customError("rev is higher than the head revision of the pad","apierror"));
- return;
- }
-
- pad.getInternalRevisionAText(rev, function(err, atext)
- {
- if(ERR(err, callback)) return;
-
- var oldText = pad.text();
- atext.text += "\n";
- function eachAttribRun(attribs, func)
- {
- var attribsIter = Changeset.opIterator(attribs);
- var textIndex = 0;
- var newTextStart = 0;
- var newTextEnd = atext.text.length;
- while (attribsIter.hasNext())
- {
- var op = attribsIter.next();
- var nextIndex = textIndex + op.chars;
- if (!(nextIndex <= newTextStart || textIndex >= newTextEnd))
- {
- func(Math.max(newTextStart, textIndex), Math.min(newTextEnd, nextIndex), op.attribs);
- }
- textIndex = nextIndex;
- }
- }
-
- // create a new changeset with a helper builder object
- var builder = Changeset.builder(oldText.length);
-
- // assemble each line into the builder
- eachAttribRun(atext.attribs, function(start, end, attribs)
- {
- builder.insert(atext.text.substring(start, end), attribs);
- });
-
- var lastNewlinePos = oldText.lastIndexOf('\n');
- if (lastNewlinePos < 0) {
- builder.remove(oldText.length-1,0);
- } else {
- builder.remove(lastNewlinePos, oldText.match(/\n/g).length-1);
- builder.remove(oldText.length - lastNewlinePos-1,0);
- }
-
- var changeset = builder.toString();
-
- //append the changeset
- pad.appendRevision(changeset);
- //
- callback(null, null);
- });
-
- });
+ var Changeset = require("ep_etherpad-lite/static/js/Changeset");
+ var padMessage = require("ep_etherpad-lite/node/handler/PadMessageHandler.js");
+
+ //check if rev is a number
+ if (rev !== undefined && typeof rev != "number")
+ {
+ //try to parse the number
+ if (!isNaN(parseInt(rev)))
+ {
+ rev = parseInt(rev);
+ }
+ else
+ {
+ callback(new customError("rev is not a number", "apierror"));
+ return;
+ }
+ }
+
+ //ensure this is not a negativ number
+ if (rev !== undefined && rev < 0)
+ {
+ callback(new customError("rev is a negativ number", "apierror"));
+ return;
+ }
+
+ //ensure this is not a float value
+ if (rev !== undefined && !is_int(rev))
+ {
+ callback(new customError("rev is a float value", "apierror"));
+ return;
+ }
+
+ //get the pad
+ getPadSafe(padID, true, function (err, pad)
+ {
+ if (ERR(err, callback)) return;
+
+
+ //check if this is a valid revision
+ if (rev > pad.getHeadRevisionNumber())
+ {
+ callback(new customError("rev is higher than the head revision of the pad", "apierror"));
+ return;
+ }
+
+ pad.getInternalRevisionAText(rev, function (err, atext)
+ {
+ if (ERR(err, callback)) return;
+
+ var oldText = pad.text();
+ atext.text += "\n";
+ function eachAttribRun(attribs, func)
+ {
+ var attribsIter = Changeset.opIterator(attribs);
+ var textIndex = 0;
+ var newTextStart = 0;
+ var newTextEnd = atext.text.length;
+ while (attribsIter.hasNext())
+ {
+ var op = attribsIter.next();
+ var nextIndex = textIndex + op.chars;
+ if (!(nextIndex <= newTextStart || textIndex >= newTextEnd))
+ {
+ func(Math.max(newTextStart, textIndex), Math.min(newTextEnd, nextIndex), op.attribs);
+ }
+ textIndex = nextIndex;
+ }
+ }
+
+ // create a new changeset with a helper builder object
+ var builder = Changeset.builder(oldText.length);
+
+ // assemble each line into the builder
+ eachAttribRun(atext.attribs, function (start, end, attribs)
+ {
+ builder.insert(atext.text.substring(start, end), attribs);
+ });
+
+ var lastNewlinePos = oldText.lastIndexOf('\n');
+ if (lastNewlinePos < 0)
+ {
+ builder.remove(oldText.length - 1, 0);
+ } else
+ {
+ builder.remove(lastNewlinePos, oldText.match(/\n/g).length - 1);
+ builder.remove(oldText.length - lastNewlinePos - 1, 0);
+ }
+
+ var changeset = builder.toString();
+
+ //append the changeset
+ pad.appendRevision(changeset);
+ //
+ padMessage.updatePadClients(pad, function ()
+ {
+ });
+ callback(null, null);
+ });
+
+ });
};
/**