fix bugs, add force option to overwrite destination

This commit is contained in:
s1341 2013-11-18 08:25:46 +02:00
parent 93fcab0461
commit 52a99eb9e5
3 changed files with 60 additions and 24 deletions

View file

@ -553,38 +553,40 @@ exports.deletePad = function(padID, callback)
}
/**
copyPad(sourceID, destinationID) copies a pad
copyPad(sourceID, destinationID[, force=false]) copies a pad. If force is true,
the destination will be overwritten if it exists.
Example returns:
{code: 0, message:"ok", data: null}
{code: 0, message:"ok", data: {padID: destinationID}}
{code: 1, message:"padID does not exist", data: null}
*/
exports.copyPad = function(sourceID, destinationID, callback)
exports.copyPad = function(sourceID, destinationID, force, callback)
{
getPadSafe(sourceID, true, function(err, pad)
{
if(ERR(err, callback)) return;
pad.copy(destinationID, callback);
pad.copy(destinationID, force, callback);
});
}
/**
movePad(padID, newID) moves a pad
movePad(sourceID, destinationID[, force=false]) moves a pad. If force is true,
the destination will be overwritten if it exists.
Example returns:
{code: 0, message:"ok", data: null}
{code: 0, message:"ok", data: {padID: destinationID}}
{code: 1, message:"padID does not exist", data: null}
*/
exports.movePad = function(padID, newID, callback)
exports.movePad = function(sourceID, destinationID, force, callback)
{
getPadSafe(padID, true, function(err, pad)
getPadSafe(sourceID, true, function(err, pad)
{
if(ERR(err, callback)) return;
pad.copy(newID, function(err) {
pad.copy(destinationID, force, function(err) {
if(ERR(err, callback)) return;
pad.remove(callback);
});

View file

@ -406,10 +406,20 @@ Pad.prototype.init = function init(text, callback) {
});
};
Pad.prototype.copy = function copy(destinationID, callback) {
Pad.prototype.copy = function copy(destinationID, force, callback) {
var sourceID = this.id;
var _this = this;
// make force optional
if (typeof force == "function") {
callback = force;
force = false;
}
else if (force == undefined || force.toLowerCase() != "true") {
force = false;
}
else force = true;
//kick everyone from this pad
// TODO: this presents a message on the client saying that the pad was 'deleted'. Fix this?
padMessageHandler.kickSessionsFromPad(sourceID);
@ -430,7 +440,8 @@ Pad.prototype.copy = function copy(destinationID, callback) {
//group does not exist
if(exists == false)
{
callback(new customError("groupID does not exist","apierror"));
callback(new customError("groupID does not exist for destinationID","apierror"));
return;
}
//everything is fine, continue
else
@ -439,30 +450,49 @@ Pad.prototype.copy = function copy(destinationID, callback) {
}
});
}
callback();
else
callback();
},
// if the pad exists, we should abort.
// if the pad exists, we should abort, unless forced.
function(callback)
{
console.log("destinationID", destinationID, force);
padManager.doesPadExists(destinationID, function (err, exists)
{
if(ERR(err, callback)) return;
if(exists == true)
{
callback(new customError("new padID already exists","apierror"));
if (!force)
{
console.log("erroring out without force");
callback(new customError("destinationID already exists","apierror"));
console.log("erroring out without force - after");
return;
}
else // exists and forcing
{
padManager.getPad(destinationID, function(err, pad) {
if (ERR(err, callback)) return;
pad.remove(callback);
});
}
}
//everything is fine, continue
else
else
{
db.get("pad:"+sourceID, function(err, pad) {
db.set("pad:"+destinationID, pad);
callback();
});
callback();
}
});
},
//delete all relations
// copy the 'pad' entry
function(callback)
{
db.get("pad:"+sourceID, function(err, pad) {
db.set("pad:"+destinationID, pad);
});
callback();
},
//copy all relations
function(callback)
{
async.parallel([
@ -474,6 +504,7 @@ Pad.prototype.copy = function copy(destinationID, callback) {
for(var i=0;i<=chatHead;i++)
{
db.get("pad:"+sourceID+":chat:"+i, function (err, chat) {
if (ERR(err, callback)) return;
db.set("pad:"+destinationID+":chat:"+i, chat);
});
}
@ -484,10 +515,12 @@ Pad.prototype.copy = function copy(destinationID, callback) {
function(callback)
{
var revHead = _this.head;
//console.log(revHead);
for(var i=0;i<=revHead;i++)
{
db.get("pad:"+sourceID+":revs:"+i, function (err, rev) {
//console.log("HERE");
if (ERR(err, callback)) return;
db.set("pad:"+destinationID+":revs:"+i, rev);
});
@ -502,6 +535,7 @@ Pad.prototype.copy = function copy(destinationID, callback) {
authorIDs.forEach(function (authorID)
{
console.log("authors");
authorManager.addPad(authorID, destinationID);
});

View file

@ -240,8 +240,8 @@ var version =
, "getRevisionChangeset" : ["padID", "rev"]
, "getLastEdited" : ["padID"]
, "deletePad" : ["padID"]
, "copyPad" : ["sourceID", "destinationID"]
, "movePad" : ["sourceID", "destinationID"]
, "copyPad" : ["sourceID", "destinationID", "force"]
, "movePad" : ["sourceID", "destinationID", "force"]
, "getReadOnlyID" : ["padID"]
, "setPublicStatus" : ["padID", "publicStatus"]
, "getPublicStatus" : ["padID"]