pad.pub0.org/node/db/Pad.js

536 lines
14 KiB
JavaScript
Raw Normal View History

2011-05-30 14:53:11 +00:00
/**
* The pad object, defined with joose
*/
require('joose');
var ERR = require("async-stacktrace");
2011-07-27 17:52:23 +00:00
var Changeset = require("../utils/Changeset");
var AttributePoolFactory = require("../utils/AttributePoolFactory");
var db = require("./DB").db;
var async = require("async");
2011-07-27 17:52:23 +00:00
var settings = require('../utils/Settings');
var authorManager = require("./AuthorManager");
2011-08-16 19:02:30 +00:00
var padManager = require("./PadManager");
var padMessageHandler = require("../handler/PadMessageHandler");
var readOnlyManager = require("./ReadOnlyManager");
var crypto = require("crypto");
/**
2011-05-30 14:53:11 +00:00
* 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, ' ');
}
Class('Pad', {
2011-08-01 17:45:28 +00:00
// these are the properties
has : {
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
2011-08-01 17:45:28 +00:00
id : { is : 'r' }
},
2011-08-01 17:45:28 +00:00
methods : {
BUILD : function (id)
{
return {
'id' : id,
}
},
2011-08-01 17:45:28 +00:00
appendRevision : function(aChangeset, author)
{
if(!author)
author = '';
2011-08-01 17:45:28 +00:00
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 != '')
this.pool.putAttrib(['author', author || '']);
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,
passwordHash: this.passwordHash});
2011-08-01 17:45:28 +00:00
}, //appendRevision
getRevisionChangeset : function(revNum, callback)
{
db.getSub("pad:"+this.id+":revs:"+revNum, ["changeset"], callback);
}, // getRevisionChangeset
getRevisionAuthor : function(revNum, callback)
{
db.getSub("pad:"+this.id+":revs:"+revNum, ["meta", "author"], callback);
}, // getRevisionAuthor
getRevisionDate : function(revNum, callback)
{
db.getSub("pad:"+this.id+":revs:"+revNum, ["meta", "timestamp"], callback);
}, // getRevisionAuthor
getAllAuthors : function()
{
var authors = [];
2011-08-01 17:45:28 +00:00
for(key in this.pool.numToAttrib)
{
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)
{
var _this = this;
var keyRev = this.getKeyRevisionNumber(targetRev);
var atext;
var changesets = [];
//find out which changesets are needed
var neededChangesets = [];
var curRev = keyRev;
while (curRev < targetRev)
{
curRev++;
neededChangesets.push(curRev);
}
2011-08-01 17:45:28 +00:00
async.series([
//get all needed data out of the database
function(callback)
{
async.parallel([
//get the atext of the key revision
function (callback)
{
db.getSub("pad:"+_this.id+":revs:"+keyRev, ["meta", "atext"], function(err, _atext)
{
if(ERR(err, callback)) return;
2011-08-01 17:45:28 +00:00
atext = Changeset.cloneAText(_atext);
callback();
2011-08-01 17:45:28 +00:00
});
},
//get all needed changesets
function (callback)
{
async.forEach(neededChangesets, function(item, callback)
{
_this.getRevisionChangeset(item, function(err, changeset)
{
if(ERR(err, callback)) return;
2011-08-01 17:45:28 +00:00
changesets[item] = changeset;
callback();
2011-08-01 17:45:28 +00:00
});
}, callback);
}
], callback);
},
//apply all changesets to the key changeset
function(callback)
{
var apool = _this.apool();
var curRev = keyRev;
while (curRev < targetRev)
{
curRev++;
var cs = changesets[curRev];
atext = Changeset.applyToAText(cs, atext, apool);
}
callback(null);
2011-08-01 17:45:28 +00:00
}
], function(err)
{
if(ERR(err, callback)) return;
callback(null, atext);
2011-08-01 17:45:28 +00:00
});
},
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
var changeset = Changeset.makeSplice(oldText, 0, oldText.length-1, newText);
//append the changeset
this.appendRevision(changeset);
},
appendChatMessage: function(text, userId, time)
{
this.chatHead++;
//save the chat entry in the database
db.set("pad:"+this.id+":chat:"+this.chatHead, {"text": text, "userId": userId, "time": time});
//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
function(callback)
{
db.get("pad:"+_this.id+":chat:"+entryNum, function(err, _entry)
{
if(ERR(err, callback)) return;
2011-08-01 17:45:28 +00:00
entry = _entry;
callback();
2011-08-01 17:45:28 +00:00
});
},
//add the authorName
function(callback)
{
//this chat message doesn't exist, return null
if(entry == null)
{
callback();
return;
}
//get the authorName
authorManager.getAuthorName(entry.userId, function(err, authorName)
{
if(ERR(err, callback)) return;
2011-08-01 17:45:28 +00:00
entry.userName = authorName;
callback();
2011-08-01 17:45:28 +00:00
});
}
], function(err)
{
if(ERR(err, callback)) return;
callback(null, entry);
2011-08-01 17:45:28 +00:00
});
},
getLastChatMessages: function(count, callback)
{
//return an empty array if there are no chat messages
if(this.chatHead == -1)
{
callback(null, []);
return;
}
2011-07-14 15:15:38 +00:00
var _this = this;
//works only if we decrement the amount, for some reason
count--;
2011-08-01 17:45:28 +00:00
//set the startpoint
var start = this.chatHead-count;
if(start < 0)
start = 0;
//set the endpoint
var end = this.chatHead;
2011-07-14 15:15:38 +00:00
2011-08-01 17:45:28 +00:00
//collect the numbers of chat entries and in which order we need them
var neededEntries = [];
var order = 0;
for(var i=start;i<=end; i++)
{
neededEntries.push({entryNum:i, order: order});
order++;
}
//get all entries out of the database
var entries = [];
async.forEach(neededEntries, function(entryObject, callback)
2011-07-14 15:15:38 +00:00
{
2011-07-31 15:03:53 +00:00
_this.getChatMessage(entryObject.entryNum, function(err, entry)
2011-07-14 15:15:38 +00:00
{
if(ERR(err, callback)) return;
2011-07-14 15:15:38 +00:00
entries[entryObject.order] = entry;
callback();
2011-07-14 15:15:38 +00:00
});
}, function(err)
{
if(ERR(err, callback)) return;
2011-07-31 15:03:53 +00:00
//sort out broken chat entries
//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<entries.length;i++)
{
2011-07-31 19:32:46 +00:00
if(entries[i]!=null)
2011-07-31 15:03:53 +00:00
cleanedEntries.push(entries[i]);
else
2011-07-31 17:25:51 +00:00
console.warn("WARNING: Found broken chat entry in pad " + _this.id);
2011-07-31 15:03:53 +00:00
}
callback(null, cleanedEntries);
2011-07-14 15:15:38 +00:00
});
2011-08-01 17:45:28 +00:00
},
2011-08-04 18:20:14 +00:00
init : function (text, callback)
2011-08-01 17:45:28 +00:00
{
var _this = this;
2011-08-04 18:20:14 +00:00
//replace text with default text if text isn't set
if(text == null)
{
text = settings.defaultPadText;
}
2011-08-01 17:45:28 +00:00
//try to load the pad
db.get("pad:"+this.id, function(err, value)
{
if(ERR(err, callback)) return;
2011-08-01 17:45:28 +00:00
//if this pad exists, load it
if(value != null)
{
_this.head = value.head;
_this.atext = value.atext;
_this.pool = _this.pool.fromJsonable(value.pool);
//ensure we have a local chatHead variable
2011-08-01 17:45:28 +00:00
if(value.chatHead != null)
_this.chatHead = value.chatHead;
else
_this.chatHead = -1;
//ensure we have a local publicStatus variable
if(value.publicStatus != null)
_this.publicStatus = value.publicStatus;
else
_this.publicStatus = false;
//ensure we have a local passwordHash variable
if(value.passwordHash != null)
_this.passwordHash = value.passwordHash;
else
_this.passwordHash = null;
2011-08-01 17:45:28 +00:00
}
//this pad doesn't exist, so create it
else
{
2011-08-04 18:20:14 +00:00
var firstChangeset = Changeset.makeSplice("\n", 0, 0, exports.cleanText(text));
2011-08-01 17:45:28 +00:00
_this.appendRevision(firstChangeset, '');
}
callback(null);
});
},
2011-08-16 19:02:30 +00:00
remove: function(callback)
{
var padID = this.id;
var _this = this;
//kick everyone from this pad
padMessageHandler.kickSessionsFromPad(padID);
async.series([
//delete all relations
function(callback)
{
async.parallel([
//is it a group pad? -> delete the entry of this pad in the group
function(callback)
{
//is it a group pad?
if(padID.indexOf("$")!=-1)
{
var groupID = padID.substring(0,padID.indexOf("$"));
db.get("group:" + groupID, function (err, group)
{
if(ERR(err, callback)) return;
2011-08-16 19:02:30 +00:00
//remove the pad entry
delete group.pads[padID];
//set the new value
db.set("group:" + groupID, group);
callback();
});
}
//its no group pad, nothing to do here
else
{
callback();
}
},
//remove the readonly entries
function(callback)
{
readOnlyManager.getReadOnlyId(padID, function(err, readonlyID)
{
if(ERR(err, callback)) return;
2011-08-16 19:02:30 +00:00
db.remove("pad2readonly:" + padID);
db.remove("readonly2pad:" + readonlyID);
callback();
});
},
//delete all chat messages
function(callback)
{
var chatHead = _this.chatHead;
for(var i=0;i<=chatHead;i++)
{
db.remove("pad:"+padID+":chat:"+i);
}
callback();
},
//delete all revisions
function(callback)
{
var revHead = _this.head;
for(var i=0;i<=revHead;i++)
{
db.remove("pad:"+padID+":revs:"+i);
}
callback();
}
], callback);
},
//delete the pad entry and delete pad from padManager
function(callback)
{
db.remove("pad:"+padID);
padManager.unloadPad(padID);
callback();
}
], function(err)
{
if(ERR(err, callback)) return;
callback();
2011-08-16 19:02:30 +00:00
})
},
//set in db
setPublicStatus: function(publicStatus)
{
this.publicStatus = publicStatus;
db.setSub("pad:"+this.id, ["publicStatus"], this.publicStatus);
},
setPassword: function(password)
{
this.passwordHash = password == null ? null : hash(password, generateSalt());
db.setSub("pad:"+this.id, ["passwordHash"], this.passwordHash);
},
isCorrectPassword: function(password)
{
return compare(this.passwordHash, password)
},
isPasswordProtected: function()
{
return this.passwordHash != null;
}
2011-08-01 17:45:28 +00:00
}, // methods
});
/* Crypto helper methods */
function hash(password, salt)
{
var shasum = crypto.createHash('sha512');
shasum.update(password + salt);
return shasum.digest("hex") + "$" + salt;
}
function generateSalt()
{
var len = 86;
var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./";
var randomstring = '';
for (var i = 0; i < len; i++)
{
var rnum = Math.floor(Math.random() * chars.length);
randomstring += chars.substring(rnum, rnum + 1);
}
return randomstring;
}
function compare(hashStr, password)
{
return hash(password, hashStr.split("$")[1]) === hashStr;
}