2016-06-17 04:09:33 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
// external modules
|
|
|
|
var Sequelize = require("sequelize");
|
|
|
|
var async = require('async');
|
|
|
|
var moment = require('moment');
|
2016-11-17 23:09:58 -05:00
|
|
|
var childProcess = require('child_process');
|
|
|
|
var shortId = require('shortid');
|
2016-06-17 04:09:33 -04:00
|
|
|
|
|
|
|
// core
|
|
|
|
var config = require("../config.js");
|
|
|
|
var logger = require("../logger.js");
|
|
|
|
|
2016-11-17 23:09:58 -05:00
|
|
|
var dmpWorker = createDmpWorker();
|
|
|
|
var dmpCallbackCache = {};
|
|
|
|
|
|
|
|
function createDmpWorker() {
|
|
|
|
var worker = childProcess.fork("./lib/workers/dmpWorker.js", {
|
|
|
|
stdio: 'ignore'
|
|
|
|
});
|
|
|
|
if (config.debug) logger.info('dmp worker process started');
|
|
|
|
worker.on('message', function (data) {
|
|
|
|
if (!data || !data.msg || !data.cacheKey) {
|
|
|
|
return logger.error('dmp worker error: not enough data on message');
|
|
|
|
}
|
|
|
|
var cacheKey = data.cacheKey;
|
|
|
|
switch(data.msg) {
|
|
|
|
case 'error':
|
|
|
|
dmpCallbackCache[cacheKey](data.error, null);
|
|
|
|
break;
|
|
|
|
case 'check':
|
|
|
|
dmpCallbackCache[cacheKey](null, data.result);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
delete dmpCallbackCache[cacheKey];
|
|
|
|
});
|
|
|
|
worker.on('close', function (code) {
|
|
|
|
dmpWorker = null;
|
|
|
|
if (config.debug) logger.info('dmp worker process exited with code ' + code);
|
|
|
|
});
|
|
|
|
return worker;
|
|
|
|
}
|
|
|
|
|
|
|
|
function sendDmpWorker(data, callback) {
|
|
|
|
if (!dmpWorker) dmpWorker = createDmpWorker();
|
|
|
|
var cacheKey = Date.now() + '_' + shortId.generate();
|
|
|
|
dmpCallbackCache[cacheKey] = callback;
|
|
|
|
data = Object.assign(data, {
|
|
|
|
cacheKey: cacheKey
|
|
|
|
});
|
|
|
|
dmpWorker.send(data);
|
|
|
|
}
|
|
|
|
|
2016-06-17 04:09:33 -04:00
|
|
|
module.exports = function (sequelize, DataTypes) {
|
|
|
|
var Revision = sequelize.define("Revision", {
|
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
primaryKey: true,
|
|
|
|
defaultValue: Sequelize.UUIDV4
|
|
|
|
},
|
|
|
|
patch: {
|
2017-01-01 22:05:05 -05:00
|
|
|
type: DataTypes.TEXT,
|
2017-01-01 22:05:36 -05:00
|
|
|
get: function () {
|
|
|
|
return sequelize.processData(this.getDataValue('patch'), "");
|
|
|
|
},
|
2017-01-01 22:05:05 -05:00
|
|
|
set: function (value) {
|
|
|
|
this.setDataValue('patch', sequelize.stripNullByte(value));
|
|
|
|
}
|
2016-06-17 04:09:33 -04:00
|
|
|
},
|
|
|
|
lastContent: {
|
2017-01-01 22:05:05 -05:00
|
|
|
type: DataTypes.TEXT,
|
2017-01-01 22:05:36 -05:00
|
|
|
get: function () {
|
|
|
|
return sequelize.processData(this.getDataValue('lastContent'), "");
|
|
|
|
},
|
2017-01-01 22:05:05 -05:00
|
|
|
set: function (value) {
|
|
|
|
this.setDataValue('lastContent', sequelize.stripNullByte(value));
|
|
|
|
}
|
2016-06-17 04:09:33 -04:00
|
|
|
},
|
|
|
|
content: {
|
2017-01-01 22:05:05 -05:00
|
|
|
type: DataTypes.TEXT,
|
2017-01-01 22:05:36 -05:00
|
|
|
get: function () {
|
|
|
|
return sequelize.processData(this.getDataValue('content'), "");
|
|
|
|
},
|
2017-01-01 22:05:05 -05:00
|
|
|
set: function (value) {
|
|
|
|
this.setDataValue('content', sequelize.stripNullByte(value));
|
|
|
|
}
|
2016-06-17 04:09:33 -04:00
|
|
|
},
|
|
|
|
length: {
|
|
|
|
type: DataTypes.INTEGER
|
2016-07-29 23:21:38 -04:00
|
|
|
},
|
|
|
|
authorship: {
|
2017-01-01 22:05:05 -05:00
|
|
|
type: DataTypes.TEXT,
|
2017-01-01 22:05:36 -05:00
|
|
|
get: function () {
|
|
|
|
return sequelize.processData(this.getDataValue('authorship'), [], JSON.parse);
|
|
|
|
},
|
2017-01-01 22:05:05 -05:00
|
|
|
set: function (value) {
|
|
|
|
this.setDataValue('authorship', value ? JSON.stringify(value) : value);
|
|
|
|
}
|
2016-06-17 04:09:33 -04:00
|
|
|
}
|
|
|
|
}, {
|
|
|
|
classMethods: {
|
|
|
|
associate: function (models) {
|
2016-07-29 23:15:44 -04:00
|
|
|
Revision.belongsTo(models.Note, {
|
2016-06-17 04:09:33 -04:00
|
|
|
foreignKey: "noteId",
|
|
|
|
as: "note",
|
|
|
|
constraints: false
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getNoteRevisions: function (note, callback) {
|
|
|
|
Revision.findAll({
|
|
|
|
where: {
|
|
|
|
noteId: note.id
|
|
|
|
},
|
|
|
|
order: '"createdAt" DESC'
|
|
|
|
}).then(function (revisions) {
|
|
|
|
var data = [];
|
|
|
|
for (var i = 0, l = revisions.length; i < l; i++) {
|
|
|
|
var revision = revisions[i];
|
|
|
|
data.push({
|
|
|
|
time: moment(revision.createdAt).valueOf(),
|
|
|
|
length: revision.length
|
|
|
|
});
|
|
|
|
}
|
|
|
|
callback(null, data);
|
|
|
|
}).catch(function (err) {
|
|
|
|
callback(err, null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
getPatchedNoteRevisionByTime: function (note, time, callback) {
|
|
|
|
// find all revisions to prepare for all possible calculation
|
|
|
|
Revision.findAll({
|
|
|
|
where: {
|
|
|
|
noteId: note.id
|
|
|
|
},
|
|
|
|
order: '"createdAt" DESC'
|
|
|
|
}).then(function (revisions) {
|
|
|
|
if (revisions.length <= 0) return callback(null, null);
|
|
|
|
// measure target revision position
|
|
|
|
Revision.count({
|
|
|
|
where: {
|
|
|
|
noteId: note.id,
|
|
|
|
createdAt: {
|
|
|
|
$gte: time
|
|
|
|
}
|
|
|
|
},
|
|
|
|
order: '"createdAt" DESC'
|
|
|
|
}).then(function (count) {
|
|
|
|
if (count <= 0) return callback(null, null);
|
2016-11-17 23:09:58 -05:00
|
|
|
sendDmpWorker({
|
|
|
|
msg: 'get revision',
|
|
|
|
revisions: revisions,
|
|
|
|
count: count
|
|
|
|
}, callback);
|
2016-06-17 04:09:33 -04:00
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null);
|
|
|
|
});
|
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
checkAllNotesRevision: function (callback) {
|
|
|
|
Revision.saveAllNotesRevision(function (err, notes) {
|
|
|
|
if (err) return callback(err, null);
|
2016-09-18 04:50:20 -04:00
|
|
|
if (!notes || notes.length <= 0) {
|
2016-06-17 04:09:33 -04:00
|
|
|
return callback(null, notes);
|
|
|
|
} else {
|
|
|
|
Revision.checkAllNotesRevision(callback);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
},
|
|
|
|
saveAllNotesRevision: function (callback) {
|
|
|
|
sequelize.models.Note.findAll({
|
2016-09-18 04:50:20 -04:00
|
|
|
// query all notes that need to save for revision
|
2016-06-17 04:09:33 -04:00
|
|
|
where: {
|
|
|
|
$and: [
|
|
|
|
{
|
|
|
|
lastchangeAt: {
|
|
|
|
$or: {
|
|
|
|
$eq: null,
|
|
|
|
$and: {
|
|
|
|
$ne: null,
|
|
|
|
$gt: sequelize.col('createdAt')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
savedAt: {
|
|
|
|
$or: {
|
|
|
|
$eq: null,
|
|
|
|
$lt: sequelize.col('lastchangeAt')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}).then(function (notes) {
|
|
|
|
if (notes.length <= 0) return callback(null, notes);
|
2016-09-18 04:50:20 -04:00
|
|
|
var savedNotes = [];
|
2016-06-17 04:09:33 -04:00
|
|
|
async.each(notes, function (note, _callback) {
|
2016-09-18 04:50:20 -04:00
|
|
|
// revision saving policy: note not been modified for 5 mins or not save for 10 mins
|
|
|
|
if (note.lastchangeAt && note.savedAt) {
|
|
|
|
var lastchangeAt = moment(note.lastchangeAt);
|
|
|
|
var savedAt = moment(note.savedAt);
|
2016-10-15 01:54:16 -04:00
|
|
|
if (moment().isAfter(lastchangeAt.add(5, 'minutes'))) {
|
2016-09-18 04:50:20 -04:00
|
|
|
savedNotes.push(note);
|
|
|
|
Revision.saveNoteRevision(note, _callback);
|
2016-10-15 01:54:16 -04:00
|
|
|
} else if (lastchangeAt.isAfter(savedAt.add(10, 'minutes'))) {
|
2016-09-18 04:50:20 -04:00
|
|
|
savedNotes.push(note);
|
|
|
|
Revision.saveNoteRevision(note, _callback);
|
|
|
|
} else {
|
|
|
|
return _callback(null, null);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
savedNotes.push(note);
|
|
|
|
Revision.saveNoteRevision(note, _callback);
|
|
|
|
}
|
2016-06-17 04:09:33 -04:00
|
|
|
}, function (err) {
|
|
|
|
if (err) return callback(err, null);
|
2016-09-18 04:50:20 -04:00
|
|
|
// return null when no notes need saving at this moment but have delayed tasks to be done
|
|
|
|
var result = ((savedNotes.length == 0) && (notes.length > savedNotes.length)) ? null : savedNotes;
|
|
|
|
return callback(null, result);
|
2016-06-17 04:09:33 -04:00
|
|
|
});
|
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
saveNoteRevision: function (note, callback) {
|
|
|
|
Revision.findAll({
|
|
|
|
where: {
|
|
|
|
noteId: note.id
|
|
|
|
},
|
|
|
|
order: '"createdAt" DESC'
|
|
|
|
}).then(function (revisions) {
|
|
|
|
if (revisions.length <= 0) {
|
|
|
|
// if no revision available
|
|
|
|
Revision.create({
|
|
|
|
noteId: note.id,
|
|
|
|
lastContent: note.content,
|
2017-01-01 21:59:53 -05:00
|
|
|
length: note.content.length,
|
2016-07-29 23:24:20 -04:00
|
|
|
authorship: note.authorship
|
2016-06-17 04:09:33 -04:00
|
|
|
}).then(function (revision) {
|
|
|
|
Revision.finishSaveNoteRevision(note, revision, callback);
|
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
var latestRevision = revisions[0];
|
2017-01-01 21:59:53 -05:00
|
|
|
var lastContent = latestRevision.content || latestRevision.lastContent;
|
|
|
|
var content = note.content;
|
2016-11-17 23:09:58 -05:00
|
|
|
sendDmpWorker({
|
|
|
|
msg: 'create patch',
|
|
|
|
lastDoc: lastContent,
|
|
|
|
currDoc: content,
|
|
|
|
}, function (err, patch) {
|
|
|
|
if (err) logger.error('save note revision error', err);
|
|
|
|
if (!patch) {
|
|
|
|
// if patch is empty (means no difference) then just update the latest revision updated time
|
|
|
|
latestRevision.changed('updatedAt', true);
|
2016-06-17 04:09:33 -04:00
|
|
|
latestRevision.update({
|
2016-11-17 23:09:58 -05:00
|
|
|
updatedAt: Date.now()
|
|
|
|
}).then(function (revision) {
|
2016-06-17 04:09:33 -04:00
|
|
|
Revision.finishSaveNoteRevision(note, revision, callback);
|
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null);
|
|
|
|
});
|
2016-11-17 23:09:58 -05:00
|
|
|
} else {
|
|
|
|
Revision.create({
|
|
|
|
noteId: note.id,
|
2017-01-01 21:59:53 -05:00
|
|
|
patch: patch,
|
2016-11-17 23:09:58 -05:00
|
|
|
content: note.content,
|
2017-01-01 21:59:53 -05:00
|
|
|
length: note.content.length,
|
2016-11-17 23:09:58 -05:00
|
|
|
authorship: note.authorship
|
|
|
|
}).then(function (revision) {
|
|
|
|
// clear last revision content to reduce db size
|
|
|
|
latestRevision.update({
|
|
|
|
content: null
|
|
|
|
}).then(function () {
|
|
|
|
Revision.finishSaveNoteRevision(note, revision, callback);
|
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null);
|
|
|
|
});
|
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
2016-06-17 04:09:33 -04:00
|
|
|
}
|
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
finishSaveNoteRevision: function (note, revision, callback) {
|
|
|
|
note.update({
|
|
|
|
savedAt: revision.updatedAt
|
|
|
|
}).then(function () {
|
|
|
|
return callback(null, revision);
|
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return Revision;
|
|
|
|
};
|