From c4f9828825f812de7521edbd2654b0f02299510b Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 11 Jun 2014 22:23:43 +0200 Subject: [PATCH] Fix #2136: update cache and group list when copying pads --- src/node/db/Pad.js | 48 ++++++++++++++++++++++++++++------------------ 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/node/db/Pad.js b/src/node/db/Pad.js index c7e0d50ab..1589e8c5c 100644 --- a/src/node/db/Pad.js +++ b/src/node/db/Pad.js @@ -84,33 +84,33 @@ Pad.prototype.appendRevision = function appendRevision(aChangeset, author) { db.set("pad:"+this.id+":revs:"+newRev, newRevData); this.saveToDatabase(); - + // set the author to pad if(author) authorManager.addPad(author, this.id); - + if (this.head == 0) { hooks.callAll("padCreate", {'pad':this}); } else { hooks.callAll("padUpdate", {'pad':this}); - } + } }; //save all attributes to the database Pad.prototype.saveToDatabase = function saveToDatabase(){ var dbObject = {}; - + for(var attr in this){ if(typeof this[attr] === "function") continue; if(attributeBlackList.indexOf(attr) !== -1) continue; - + dbObject[attr] = this[attr]; - + if(jsonableList.indexOf(attr) !== -1){ dbObject[attr] = dbObject[attr].toJsonable(); } } - + db.set("pad:"+this.id, dbObject); } @@ -337,9 +337,9 @@ Pad.prototype.getChatMessages = function getChatMessages(start, end, callback) { neededEntries.push({entryNum:i, order: order}); order++; } - + var _this = this; - + //get all entries out of the database var entries = []; async.forEach(neededEntries, function(entryObject, callback) @@ -412,6 +412,7 @@ Pad.prototype.init = function init(text, callback) { Pad.prototype.copy = function copy(destinationID, force, callback) { var sourceID = this.id; var _this = this; + var destGroupID; // make force optional if (typeof force == "function") { @@ -434,12 +435,13 @@ Pad.prototype.copy = function copy(destinationID, force, callback) { // if it's a group pad, let's make sure the group exists. function(callback) { - if (destinationID.indexOf("$") != -1) - { - groupManager.doesGroupExist(destinationID.split("$")[0], function (err, exists) + if (destinationID.indexOf("$") != -1) + { + destGroupID = destinationID.split("$")[0] + groupManager.doesGroupExist(destGroupID, function (err, exists) { if(ERR(err, callback)) return; - + //group does not exist if(exists == false) { @@ -460,10 +462,10 @@ Pad.prototype.copy = function copy(destinationID, force, callback) { function(callback) { console.log("destinationID", destinationID, force); - padManager.doesPadExists(destinationID, function (err, exists) + padManager.doesPadExists(destinationID, function (err, exists) { if(ERR(err, callback)) return; - + if(exists == true) { if (!force) @@ -472,7 +474,7 @@ Pad.prototype.copy = function copy(destinationID, force, callback) { 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) { @@ -481,7 +483,7 @@ Pad.prototype.copy = function copy(destinationID, force, callback) { }); } } - else + else { callback(); } @@ -493,6 +495,7 @@ Pad.prototype.copy = function copy(destinationID, force, callback) { db.get("pad:"+sourceID, function(err, pad) { db.set("pad:"+destinationID, pad); }); + callback(); }, //copy all relations @@ -547,6 +550,13 @@ Pad.prototype.copy = function copy(destinationID, force, callback) { // parallel ], callback); }, + function(callback) { + // Group pad? Add it to the group's list + if(destGroupID) db.setSub("group:" + destGroupID, ["pads", padID], 1); + + // Initialize the new pad (will update the listAllPads cache) + padManager.getPad(destinationID, null, callback) + } // series ], function(err) { @@ -684,7 +694,7 @@ Pad.prototype.addSavedRevision = function addSavedRevision(revNum, savedById, la return; } } - + //build the saved revision object var savedRevision = {}; savedRevision.revNum = revNum; @@ -692,7 +702,7 @@ Pad.prototype.addSavedRevision = function addSavedRevision(revNum, savedById, la savedRevision.label = label || "Revision " + revNum; savedRevision.timestamp = new Date().getTime(); savedRevision.id = randomString(10); - + //save this new saved revision this.savedRevisions.push(savedRevision); this.saveToDatabase();