diff --git a/node/db/Pad.js b/node/db/Pad.js index 7807d4643..3a84891bb 100644 --- a/node/db/Pad.js +++ b/node/db/Pad.js @@ -22,139 +22,139 @@ var crypto = require("crypto"); */ exports.cleanText = function (txt) { return txt.replace(/\r\n/g,'\n').replace(/\r/g,'\n').replace(/\t/g, ' ').replace(/\xa0/g, ' '); -} +}; Class('Pad', { // these are the properties has : { - - atext : { + + atext : { is : 'rw', // readwrite init : function() { return Changeset.makeAText("\n"); } // first value }, // atext - - pool : { - is: 'rw', + + pool : { + is: 'rw', init : function() { return AttributePoolFactory.createAttributePool(); }, getterName : 'apool' // legacy }, // pool - - head : { - is : 'rw', - init : -1, - getterName : 'getHeadRevisionNumber' + + head : { + is : 'rw', + init : -1, + getterName : 'getHeadRevisionNumber' }, // head - + chatHead : { is: 'rw', init: -1 }, // chatHead - + publicStatus : { is: 'rw', init: false, getterName : 'getPublicStatus' }, //publicStatus - + passwordHash : { is: 'rw', init: null }, // passwordHash - + id : { is : 'r' } }, methods : { - - BUILD : function (id) + + BUILD : function (id) { return { - 'id' : id, - } + 'id' : id + }; }, - - appendRevision : function(aChangeset, author) + + appendRevision : function(aChangeset, author) { if(!author) author = ''; var newAText = Changeset.applyToAText(aChangeset, this.atext, this.pool); Changeset.copyAText(newAText, this.atext); - + var newRev = ++this.head; - + var newRevData = {}; newRevData.changeset = aChangeset; newRevData.meta = {}; newRevData.meta.author = author; newRevData.meta.timestamp = new Date().getTime(); - + //ex. getNumForAuthor - if(author != '') + if(author !== '') this.pool.putAttrib(['author', author || '']); - - if(newRev % 100 == 0) + + if(newRev % 100 === 0) { newRevData.meta.atext = this.atext; } - + db.set("pad:"+this.id+":revs:"+newRev, newRevData); - db.set("pad:"+this.id, {atext: this.atext, - pool: this.pool.toJsonable(), - head: this.head, - chatHead: this.chatHead, - publicStatus: this.publicStatus, + db.set("pad:"+this.id, {atext: this.atext, + pool: this.pool.toJsonable(), + head: this.head, + chatHead: this.chatHead, + publicStatus: this.publicStatus, passwordHash: this.passwordHash}); }, //appendRevision - - getRevisionChangeset : function(revNum, callback) + + getRevisionChangeset : function(revNum, callback) { db.getSub("pad:"+this.id+":revs:"+revNum, ["changeset"], callback); }, // getRevisionChangeset - - getRevisionAuthor : function(revNum, callback) - { + + getRevisionAuthor : function(revNum, callback) + { db.getSub("pad:"+this.id+":revs:"+revNum, ["meta", "author"], callback); }, // getRevisionAuthor - - getRevisionDate : function(revNum, callback) - { + + getRevisionDate : function(revNum, callback) + { db.getSub("pad:"+this.id+":revs:"+revNum, ["meta", "timestamp"], callback); }, // getRevisionAuthor - - getAllAuthors : function() + + getAllAuthors : function() { var authors = []; - - for(key in this.pool.numToAttrib) + + for(var key in this.pool.numToAttrib) { - if(this.pool.numToAttrib[key][0] == "author" && this.pool.numToAttrib[key][1] != "") + if(this.pool.numToAttrib[key][0] == "author" && this.pool.numToAttrib[key][1] !== "") { authors.push(this.pool.numToAttrib[key][1]); } } - + return authors; }, - - getInternalRevisionAText : function(targetRev, callback) + + getInternalRevisionAText : function(targetRev, callback) { var _this = this; - + var keyRev = this.getKeyRevisionNumber(targetRev); - var atext; + var atext; var changesets = []; - + //find out which changesets are needed var neededChangesets = []; var curRev = keyRev; - while (curRev < targetRev) + while (curRev < targetRev) { curRev++; neededChangesets.push(curRev); } - + async.series([ //get all needed data out of the database function(callback) @@ -187,50 +187,50 @@ Class('Pad', { }, //apply all changesets to the key changeset function(callback) - { + { var apool = _this.apool(); var curRev = keyRev; - - while (curRev < targetRev) + + while (curRev < targetRev) { curRev++; var cs = changesets[curRev]; atext = Changeset.applyToAText(cs, atext, apool); } - + callback(null); - } + } ], function(err) { if(ERR(err, callback)) return; callback(null, atext); }); }, - + getKeyRevisionNumber : function(revNum) { return Math.floor(revNum / 100) * 100; }, - + text : function() { return this.atext.text; }, - + setText : function(newText) { //clean the new text newText = exports.cleanText(newText); - + var oldText = this.text(); - - //create the changeset + + //create the changeset var changeset = Changeset.makeSplice(oldText, 0, oldText.length-1, newText); - + //append the changeset this.appendRevision(changeset); }, - + appendChatMessage: function(text, userId, time) { this.chatHead++; @@ -239,14 +239,14 @@ Class('Pad', { //save the new chat head db.setSub("pad:"+this.id, ["chatHead"], this.chatHead); }, - + getChatMessage: function(entryNum, callback) - { + { var _this = this; var entry; - + async.series([ - //get the chat entry + //get the chat entry function(callback) { db.get("pad:"+_this.id+":chat:"+entryNum, function(err, _entry) @@ -258,14 +258,14 @@ Class('Pad', { }, //add the authorName function(callback) - { + { //this chat message doesn't exist, return null - if(entry == null) + if(!entry) { callback(); return; } - + //get the authorName authorManager.getAuthorName(entry.userId, function(err, authorName) { @@ -280,18 +280,18 @@ Class('Pad', { callback(null, entry); }); }, - + getLastChatMessages: function(count, callback) { - //return an empty array if there are no chat messages + //return an empty array if there are no chat messages if(this.chatHead == -1) { callback(null, []); return; } - + var _this = this; - + //works only if we decrement the amount, for some reason count--; @@ -299,10 +299,10 @@ Class('Pad', { var start = this.chatHead-count; if(start < 0) start = 0; - - //set the endpoint + + //set the endpoint var end = this.chatHead; - + //collect the numbers of chat entries and in which order we need them var neededEntries = []; var order = 0; @@ -311,7 +311,7 @@ Class('Pad', { neededEntries.push({entryNum:i, order: order}); order++; } - + //get all entries out of the database var entries = []; async.forEach(neededEntries, function(entryObject, callback) @@ -325,71 +325,76 @@ Class('Pad', { }, function(err) { if(ERR(err, callback)) return; - + //sort out broken chat entries - //it looks like in happend in the past that the chat head was + //it looks like in happend in the past that the chat head was //incremented, but the chat message wasn't added var cleanedEntries = []; for(var i=0;i