use jshint

This commit is contained in:
booo 2011-12-22 11:59:04 +01:00
parent a07563b922
commit ac74ba10d0

View file

@ -22,139 +22,139 @@ var crypto = require("crypto");
*/ */
exports.cleanText = function (txt) { exports.cleanText = function (txt) {
return txt.replace(/\r\n/g,'\n').replace(/\r/g,'\n').replace(/\t/g, ' ').replace(/\xa0/g, ' '); return txt.replace(/\r\n/g,'\n').replace(/\r/g,'\n').replace(/\t/g, ' ').replace(/\xa0/g, ' ');
} };
Class('Pad', { Class('Pad', {
// these are the properties // these are the properties
has : { has : {
atext : { atext : {
is : 'rw', // readwrite is : 'rw', // readwrite
init : function() { return Changeset.makeAText("\n"); } // first value init : function() { return Changeset.makeAText("\n"); } // first value
}, // atext }, // atext
pool : { pool : {
is: 'rw', is: 'rw',
init : function() { return AttributePoolFactory.createAttributePool(); }, init : function() { return AttributePoolFactory.createAttributePool(); },
getterName : 'apool' // legacy getterName : 'apool' // legacy
}, // pool }, // pool
head : { head : {
is : 'rw', is : 'rw',
init : -1, init : -1,
getterName : 'getHeadRevisionNumber' getterName : 'getHeadRevisionNumber'
}, // head }, // head
chatHead : { chatHead : {
is: 'rw', is: 'rw',
init: -1 init: -1
}, // chatHead }, // chatHead
publicStatus : { publicStatus : {
is: 'rw', is: 'rw',
init: false, init: false,
getterName : 'getPublicStatus' getterName : 'getPublicStatus'
}, //publicStatus }, //publicStatus
passwordHash : { passwordHash : {
is: 'rw', is: 'rw',
init: null init: null
}, // passwordHash }, // passwordHash
id : { is : 'r' } id : { is : 'r' }
}, },
methods : { methods : {
BUILD : function (id) BUILD : function (id)
{ {
return { return {
'id' : id, 'id' : id
} };
}, },
appendRevision : function(aChangeset, author) appendRevision : function(aChangeset, author)
{ {
if(!author) if(!author)
author = ''; author = '';
var newAText = Changeset.applyToAText(aChangeset, this.atext, this.pool); var newAText = Changeset.applyToAText(aChangeset, this.atext, this.pool);
Changeset.copyAText(newAText, this.atext); Changeset.copyAText(newAText, this.atext);
var newRev = ++this.head; var newRev = ++this.head;
var newRevData = {}; var newRevData = {};
newRevData.changeset = aChangeset; newRevData.changeset = aChangeset;
newRevData.meta = {}; newRevData.meta = {};
newRevData.meta.author = author; newRevData.meta.author = author;
newRevData.meta.timestamp = new Date().getTime(); newRevData.meta.timestamp = new Date().getTime();
//ex. getNumForAuthor //ex. getNumForAuthor
if(author != '') if(author !== '')
this.pool.putAttrib(['author', author || '']); this.pool.putAttrib(['author', author || '']);
if(newRev % 100 == 0) if(newRev % 100 === 0)
{ {
newRevData.meta.atext = this.atext; newRevData.meta.atext = this.atext;
} }
db.set("pad:"+this.id+":revs:"+newRev, newRevData); db.set("pad:"+this.id+":revs:"+newRev, newRevData);
db.set("pad:"+this.id, {atext: this.atext, db.set("pad:"+this.id, {atext: this.atext,
pool: this.pool.toJsonable(), pool: this.pool.toJsonable(),
head: this.head, head: this.head,
chatHead: this.chatHead, chatHead: this.chatHead,
publicStatus: this.publicStatus, publicStatus: this.publicStatus,
passwordHash: this.passwordHash}); passwordHash: this.passwordHash});
}, //appendRevision }, //appendRevision
getRevisionChangeset : function(revNum, callback) getRevisionChangeset : function(revNum, callback)
{ {
db.getSub("pad:"+this.id+":revs:"+revNum, ["changeset"], callback); db.getSub("pad:"+this.id+":revs:"+revNum, ["changeset"], callback);
}, // getRevisionChangeset }, // getRevisionChangeset
getRevisionAuthor : function(revNum, callback) getRevisionAuthor : function(revNum, callback)
{ {
db.getSub("pad:"+this.id+":revs:"+revNum, ["meta", "author"], callback); db.getSub("pad:"+this.id+":revs:"+revNum, ["meta", "author"], callback);
}, // getRevisionAuthor }, // getRevisionAuthor
getRevisionDate : function(revNum, callback) getRevisionDate : function(revNum, callback)
{ {
db.getSub("pad:"+this.id+":revs:"+revNum, ["meta", "timestamp"], callback); db.getSub("pad:"+this.id+":revs:"+revNum, ["meta", "timestamp"], callback);
}, // getRevisionAuthor }, // getRevisionAuthor
getAllAuthors : function() getAllAuthors : function()
{ {
var authors = []; 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]); authors.push(this.pool.numToAttrib[key][1]);
} }
} }
return authors; return authors;
}, },
getInternalRevisionAText : function(targetRev, callback) getInternalRevisionAText : function(targetRev, callback)
{ {
var _this = this; var _this = this;
var keyRev = this.getKeyRevisionNumber(targetRev); var keyRev = this.getKeyRevisionNumber(targetRev);
var atext; var atext;
var changesets = []; var changesets = [];
//find out which changesets are needed //find out which changesets are needed
var neededChangesets = []; var neededChangesets = [];
var curRev = keyRev; var curRev = keyRev;
while (curRev < targetRev) while (curRev < targetRev)
{ {
curRev++; curRev++;
neededChangesets.push(curRev); neededChangesets.push(curRev);
} }
async.series([ async.series([
//get all needed data out of the database //get all needed data out of the database
function(callback) function(callback)
@ -187,50 +187,50 @@ Class('Pad', {
}, },
//apply all changesets to the key changeset //apply all changesets to the key changeset
function(callback) function(callback)
{ {
var apool = _this.apool(); var apool = _this.apool();
var curRev = keyRev; var curRev = keyRev;
while (curRev < targetRev) while (curRev < targetRev)
{ {
curRev++; curRev++;
var cs = changesets[curRev]; var cs = changesets[curRev];
atext = Changeset.applyToAText(cs, atext, apool); atext = Changeset.applyToAText(cs, atext, apool);
} }
callback(null); callback(null);
} }
], function(err) ], function(err)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
callback(null, atext); callback(null, atext);
}); });
}, },
getKeyRevisionNumber : function(revNum) getKeyRevisionNumber : function(revNum)
{ {
return Math.floor(revNum / 100) * 100; return Math.floor(revNum / 100) * 100;
}, },
text : function() text : function()
{ {
return this.atext.text; return this.atext.text;
}, },
setText : function(newText) setText : function(newText)
{ {
//clean the new text //clean the new text
newText = exports.cleanText(newText); newText = exports.cleanText(newText);
var oldText = this.text(); var oldText = this.text();
//create the changeset //create the changeset
var changeset = Changeset.makeSplice(oldText, 0, oldText.length-1, newText); var changeset = Changeset.makeSplice(oldText, 0, oldText.length-1, newText);
//append the changeset //append the changeset
this.appendRevision(changeset); this.appendRevision(changeset);
}, },
appendChatMessage: function(text, userId, time) appendChatMessage: function(text, userId, time)
{ {
this.chatHead++; this.chatHead++;
@ -239,14 +239,14 @@ Class('Pad', {
//save the new chat head //save the new chat head
db.setSub("pad:"+this.id, ["chatHead"], this.chatHead); db.setSub("pad:"+this.id, ["chatHead"], this.chatHead);
}, },
getChatMessage: function(entryNum, callback) getChatMessage: function(entryNum, callback)
{ {
var _this = this; var _this = this;
var entry; var entry;
async.series([ async.series([
//get the chat entry //get the chat entry
function(callback) function(callback)
{ {
db.get("pad:"+_this.id+":chat:"+entryNum, function(err, _entry) db.get("pad:"+_this.id+":chat:"+entryNum, function(err, _entry)
@ -258,14 +258,14 @@ Class('Pad', {
}, },
//add the authorName //add the authorName
function(callback) function(callback)
{ {
//this chat message doesn't exist, return null //this chat message doesn't exist, return null
if(entry == null) if(!entry)
{ {
callback(); callback();
return; return;
} }
//get the authorName //get the authorName
authorManager.getAuthorName(entry.userId, function(err, authorName) authorManager.getAuthorName(entry.userId, function(err, authorName)
{ {
@ -280,18 +280,18 @@ Class('Pad', {
callback(null, entry); callback(null, entry);
}); });
}, },
getLastChatMessages: function(count, callback) 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) if(this.chatHead == -1)
{ {
callback(null, []); callback(null, []);
return; return;
} }
var _this = this; var _this = this;
//works only if we decrement the amount, for some reason //works only if we decrement the amount, for some reason
count--; count--;
@ -299,10 +299,10 @@ Class('Pad', {
var start = this.chatHead-count; var start = this.chatHead-count;
if(start < 0) if(start < 0)
start = 0; start = 0;
//set the endpoint //set the endpoint
var end = this.chatHead; var end = this.chatHead;
//collect the numbers of chat entries and in which order we need them //collect the numbers of chat entries and in which order we need them
var neededEntries = []; var neededEntries = [];
var order = 0; var order = 0;
@ -311,7 +311,7 @@ Class('Pad', {
neededEntries.push({entryNum:i, order: order}); neededEntries.push({entryNum:i, order: order});
order++; order++;
} }
//get all entries out of the database //get all entries out of the database
var entries = []; var entries = [];
async.forEach(neededEntries, function(entryObject, callback) async.forEach(neededEntries, function(entryObject, callback)
@ -325,71 +325,76 @@ Class('Pad', {
}, function(err) }, function(err)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//sort out broken chat entries //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 //incremented, but the chat message wasn't added
var cleanedEntries = []; var cleanedEntries = [];
for(var i=0;i<entries.length;i++) for(var i=0;i<entries.length;i++)
{ {
if(entries[i]!=null) if(entries[i]) {
cleanedEntries.push(entries[i]); cleanedEntries.push(entries[i]);
}
else else
console.warn("WARNING: Found broken chat entry in pad " + _this.id); console.warn("WARNING: Found broken chat entry in pad " + _this.id);
} }
callback(null, cleanedEntries); callback(null, cleanedEntries);
}); });
}, },
init : function (text, callback) init : function (text, callback)
{ {
var _this = this; var _this = this;
//replace text with default text if text isn't set //replace text with default text if text isn't set
if(text == null) if(!text)
{ {
text = settings.defaultPadText; text = settings.defaultPadText;
} }
//try to load the pad //try to load the pad
db.get("pad:"+this.id, function(err, value) db.get("pad:"+this.id, function(err, value)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//if this pad exists, load it //if this pad exists, load it
if(value != null) if(value)
{ {
_this.head = value.head; _this.head = value.head;
_this.atext = value.atext; _this.atext = value.atext;
_this.pool = _this.pool.fromJsonable(value.pool); _this.pool = _this.pool.fromJsonable(value.pool);
//ensure we have a local chatHead variable //ensure we have a local chatHead variable
if(value.chatHead != null) if(value.chatHead) {
_this.chatHead = value.chatHead; _this.chatHead = value.chatHead;
else }
else {
_this.chatHead = -1; _this.chatHead = -1;
}
//ensure we have a local publicStatus variable //ensure we have a local publicStatus variable
if(value.publicStatus != null) if(value.publicStatus) {
_this.publicStatus = value.publicStatus; _this.publicStatus = value.publicStatus;
else }
_this.publicStatus = false; else {
_this.publicStatus = false;
}
//ensure we have a local passwordHash variable //ensure we have a local passwordHash variable
if(value.passwordHash != null) if(value.passwordHash) {
_this.passwordHash = value.passwordHash; _this.passwordHash = value.passwordHash;
else }
else {
_this.passwordHash = null; _this.passwordHash = null;
}
} }
//this pad doesn't exist, so create it //this pad doesn't exist, so create it
else else
{ {
var firstChangeset = Changeset.makeSplice("\n", 0, 0, exports.cleanText(text)); var firstChangeset = Changeset.makeSplice("\n", 0, 0, exports.cleanText(text));
_this.appendRevision(firstChangeset, ''); _this.appendRevision(firstChangeset, '');
} }
callback(null); callback(null);
}); });
}, },
@ -397,10 +402,10 @@ Class('Pad', {
{ {
var padID = this.id; var padID = this.id;
var _this = this; var _this = this;
//kick everyone from this pad //kick everyone from this pad
padMessageHandler.kickSessionsFromPad(padID); padMessageHandler.kickSessionsFromPad(padID);
async.series([ async.series([
//delete all relations //delete all relations
function(callback) function(callback)
@ -413,17 +418,17 @@ Class('Pad', {
if(padID.indexOf("$")!=-1) if(padID.indexOf("$")!=-1)
{ {
var groupID = padID.substring(0,padID.indexOf("$")); var groupID = padID.substring(0,padID.indexOf("$"));
db.get("group:" + groupID, function (err, group) db.get("group:" + groupID, function (err, group)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
//remove the pad entry //remove the pad entry
delete group.pads[padID]; delete group.pads[padID];
//set the new value //set the new value
db.set("group:" + groupID, group); db.set("group:" + groupID, group);
callback(); callback();
}); });
} }
@ -439,10 +444,10 @@ Class('Pad', {
readOnlyManager.getReadOnlyId(padID, function(err, readonlyID) readOnlyManager.getReadOnlyId(padID, function(err, readonlyID)
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
db.remove("pad2readonly:" + padID); db.remove("pad2readonly:" + padID);
db.remove("readonly2pad:" + readonlyID); db.remove("readonly2pad:" + readonlyID);
callback(); callback();
}); });
}, },
@ -450,24 +455,24 @@ Class('Pad', {
function(callback) function(callback)
{ {
var chatHead = _this.chatHead; var chatHead = _this.chatHead;
for(var i=0;i<=chatHead;i++) for(var i=0;i<=chatHead;i++)
{ {
db.remove("pad:"+padID+":chat:"+i); db.remove("pad:"+padID+":chat:"+i);
} }
callback(); callback();
}, },
//delete all revisions //delete all revisions
function(callback) function(callback)
{ {
var revHead = _this.head; var revHead = _this.head;
for(var i=0;i<=revHead;i++) for(var i=0;i<=revHead;i++)
{ {
db.remove("pad:"+padID+":revs:"+i); db.remove("pad:"+padID+":revs:"+i);
} }
callback(); callback();
} }
], callback); ], callback);
@ -483,7 +488,7 @@ Class('Pad', {
{ {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
callback(); callback();
}) });
}, },
//set in db //set in db
setPublicStatus: function(publicStatus) setPublicStatus: function(publicStatus)
@ -493,18 +498,18 @@ Class('Pad', {
}, },
setPassword: function(password) setPassword: function(password)
{ {
this.passwordHash = password == null ? null : hash(password, generateSalt()); this.passwordHash = password ? hash(password, generateSalt()) : null;
db.setSub("pad:"+this.id, ["passwordHash"], this.passwordHash); db.setSub("pad:"+this.id, ["passwordHash"], this.passwordHash);
}, },
isCorrectPassword: function(password) isCorrectPassword: function(password)
{ {
return compare(this.passwordHash, password) return compare(this.passwordHash, password);
}, },
isPasswordProtected: function() isPasswordProtected: function()
{ {
return this.passwordHash != null; return this.passwordHash ? true : false;
} }
}, // methods } // methods
}); });
/* Crypto helper methods */ /* Crypto helper methods */
@ -531,5 +536,5 @@ function generateSalt()
function compare(hashStr, password) function compare(hashStr, password)
{ {
return hash(password, hashStr.split("$")[1]) === hashStr; return hash(password, hashStr.split("$")[1]) === hashStr;
} }