refactor Pad.js; remove joose dependency

This commit is contained in:
booo 2011-12-23 22:06:04 +01:00
parent 8ac872bea8
commit f05fc7ba31
5 changed files with 431 additions and 466 deletions

View file

@ -29,7 +29,7 @@ var sessionManager = require("./SessionManager");
var async = require("async"); var async = require("async");
var exportHtml = require("../utils/ExportHtml"); var exportHtml = require("../utils/ExportHtml");
var importHtml = require("../utils/ImportHtml"); var importHtml = require("../utils/ImportHtml");
var cleanText = require("./Pad").cleanText; var cleanText = require("../utils/cleantext").cleanText;
/**********************/ /**********************/
/**GROUP FUNCTIONS*****/ /**GROUP FUNCTIONS*****/

View file

@ -2,8 +2,6 @@
* The pad object, defined with joose * The pad object, defined with joose
*/ */
require('joose');
var ERR = require("async-stacktrace"); var ERR = require("async-stacktrace");
var Changeset = require("../utils/Changeset"); var Changeset = require("../utils/Changeset");
var AttributePoolFactory = require("../utils/AttributePoolFactory"); var AttributePoolFactory = require("../utils/AttributePoolFactory");
@ -15,69 +13,38 @@ var padManager = require("./PadManager");
var padMessageHandler = require("../handler/PadMessageHandler"); var padMessageHandler = require("../handler/PadMessageHandler");
var readOnlyManager = require("./ReadOnlyManager"); var readOnlyManager = require("./ReadOnlyManager");
var crypto = require("crypto"); var crypto = require("crypto");
var cleanText = require("../utils/cleantext").cleanText;
/** var Pad = function Pad(id) {
* Copied from the Etherpad source code. It converts Windows line breaks to Unix line breaks and convert Tabs to spaces this.atext = Changeset.makeAText("\n");
* @param txt this.pool = AttributePoolFactory.createAttributePool();
*/ this.head = -1;
exports.cleanText = function (txt) { this.chatHead = -1;
return txt.replace(/\r\n/g,'\n').replace(/\r/g,'\n').replace(/\t/g, ' ').replace(/\xa0/g, ' '); this.publicStatus = false;
this.passwordHash = null;
this.id = id;
}; };
Class('Pad', { exports.Pad = Pad;
// these are the properties Pad.prototype.apool = function apool() {
has : { return this.pool;
atext : {
is : 'rw', // readwrite
init : function() { return Changeset.makeAText("\n"); } // first value
}, // atext
pool : {
is: 'rw',
init : function() { return AttributePoolFactory.createAttributePool(); },
getterName : 'apool' // legacy
}, // pool
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)
{
return {
'id' : id
}; };
},
appendRevision : function(aChangeset, author) Pad.prototype.getHeadRevisionNumber = function getHeadRevisionNumber() {
{ return this.head;
};
Pad.prototype.getPublicStatus = function getPublicStatus() {
return this.publicStatus;
};
Pad.prototype.appendRevision = function appendRevision(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);
@ -92,7 +59,9 @@ Class('Pad', {
//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)
{ {
@ -106,25 +75,23 @@ Class('Pad', {
chatHead: this.chatHead, chatHead: this.chatHead,
publicStatus: this.publicStatus, publicStatus: this.publicStatus,
passwordHash: this.passwordHash}); passwordHash: this.passwordHash});
}, //appendRevision
getRevisionChangeset : function(revNum, callback)
{ };
Pad.prototype.getRevisionChangeset = function getRevisionChangeset(revNum, callback) {
db.getSub("pad:"+this.id+":revs:"+revNum, ["changeset"], callback); db.getSub("pad:"+this.id+":revs:"+revNum, ["changeset"], callback);
}, // getRevisionChangeset };
getRevisionAuthor : function(revNum, callback) Pad.prototype.getRevisionAuthor = function getRevisionAuthor(revNum, callback) {
{
db.getSub("pad:"+this.id+":revs:"+revNum, ["meta", "author"], callback); db.getSub("pad:"+this.id+":revs:"+revNum, ["meta", "author"], callback);
}, // getRevisionAuthor };
getRevisionDate : function(revNum, callback) Pad.prototype.getRevisionDate = function getRevisionDate(revNum, callback) {
{
db.getSub("pad:"+this.id+":revs:"+revNum, ["meta", "timestamp"], callback); db.getSub("pad:"+this.id+":revs:"+revNum, ["meta", "timestamp"], callback);
}, // getRevisionAuthor };
getAllAuthors : function() Pad.prototype.getAllAuthors = function getAllAuthors() {
{
var authors = []; var authors = [];
for(var key in this.pool.numToAttrib) for(var key in this.pool.numToAttrib)
@ -136,10 +103,9 @@ Class('Pad', {
} }
return authors; return authors;
}, };
getInternalRevisionAText : function(targetRev, callback) Pad.prototype.getInternalRevisionAText = function getInternalRevisionAText(targetRev, callback) {
{
var _this = this; var _this = this;
var keyRev = this.getKeyRevisionNumber(targetRev); var keyRev = this.getKeyRevisionNumber(targetRev);
@ -205,22 +171,20 @@ Class('Pad', {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
callback(null, atext); callback(null, atext);
}); });
},
getKeyRevisionNumber : function(revNum) };
{
Pad.prototype.getKeyRevisionNumber = function getKeyRevisionNumber(revNum) {
return Math.floor(revNum / 100) * 100; return Math.floor(revNum / 100) * 100;
}, };
text : function() Pad.prototype.text = function text() {
{
return this.atext.text; return this.atext.text;
}, };
setText : function(newText) Pad.prototype.setText = function setText(newText) {
{
//clean the new text //clean the new text
newText = exports.cleanText(newText); newText = cleanText(newText);
var oldText = this.text(); var oldText = this.text();
@ -229,19 +193,17 @@ Class('Pad', {
//append the changeset //append the changeset
this.appendRevision(changeset); this.appendRevision(changeset);
}, };
appendChatMessage: function(text, userId, time) Pad.prototype.appendChatMessage = function appendChatMessage(text, userId, time) {
{
this.chatHead++; this.chatHead++;
//save the chat entry in the database //save the chat entry in the database
db.set("pad:"+this.id+":chat:"+this.chatHead, {"text": text, "userId": userId, "time": time}); db.set("pad:"+this.id+":chat:"+this.chatHead, {"text": text, "userId": userId, "time": time});
//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) Pad.prototype.getChatMessage = function getChatMessage(entryNum, callback) {
{
var _this = this; var _this = this;
var entry; var entry;
@ -279,10 +241,9 @@ Class('Pad', {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
callback(null, entry); callback(null, entry);
}); });
}, };
getLastChatMessages: function(count, callback) Pad.prototype.getLastChatMessages = function getLastChatMessages(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)
{ {
@ -341,10 +302,9 @@ Class('Pad', {
callback(null, cleanedEntries); callback(null, cleanedEntries);
}); });
}, };
init : function (text, callback) Pad.prototype.init = function init(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
@ -390,16 +350,16 @@ Class('Pad', {
//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, cleanText(text));
_this.appendRevision(firstChangeset, ''); _this.appendRevision(firstChangeset, '');
} }
callback(null); callback(null);
}); });
}, };
remove: function(callback)
{ Pad.prototype.remove = function remove(callback) {
var padID = this.id; var padID = this.id;
var _this = this; var _this = this;
@ -489,28 +449,27 @@ Class('Pad', {
if(ERR(err, callback)) return; if(ERR(err, callback)) return;
callback(); callback();
}); });
}, };
//set in db //set in db
setPublicStatus: function(publicStatus) Pad.prototype.setPublicStatus = function setPublicStatus(publicStatus) {
{
this.publicStatus = publicStatus; this.publicStatus = publicStatus;
db.setSub("pad:"+this.id, ["publicStatus"], this.publicStatus); db.setSub("pad:"+this.id, ["publicStatus"], this.publicStatus);
}, };
setPassword: function(password)
{ Pad.prototype.setPassword = function setPassword(password) {
this.passwordHash = password ? hash(password, generateSalt()) : null; 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)
{ Pad.isCorrectPassword = function isCorrectPassword(password) {
return compare(this.passwordHash, password); return compare(this.passwordHash, password);
}, };
isPasswordProtected: function()
{ Pad.isPasswordProtected = function isPasswordProtected() {
return this.passwordHash ? true: false; return this.passwordHash ? true: false;
} };
} // methods
});
/* Crypto helper methods */ /* Crypto helper methods */

View file

@ -20,7 +20,7 @@
var ERR = require("async-stacktrace"); var ERR = require("async-stacktrace");
var customError = require("../utils/customError"); var customError = require("../utils/customError");
require("../db/Pad"); var Pad = require("../db/Pad").Pad;
var db = require("./DB").db; var db = require("./DB").db;
/** /**

7
node/utils/cleantext.js Normal file
View file

@ -0,0 +1,7 @@
/**
* Copied from the Etherpad source code. It converts Windows line breaks to Unix line breaks and convert Tabs to spaces
* @param txt
*/
exports.cleanText = function (txt) {
return txt.replace(/\r\n/g,'\n').replace(/\r/g,'\n').replace(/\t/g, ' ').replace(/\xa0/g, ' ');
};

View file

@ -13,7 +13,6 @@
"socket.io" : "0.8.7", "socket.io" : "0.8.7",
"ueberDB" : "0.1.3", "ueberDB" : "0.1.3",
"async" : "0.1.15", "async" : "0.1.15",
"joose" : "3.50.0",
"express" : "2.5.0", "express" : "2.5.0",
"clean-css" : "0.2.4", "clean-css" : "0.2.4",
"uglify-js" : "1.1.1", "uglify-js" : "1.1.1",