2017-03-14 05:02:43 +00:00
|
|
|
'use strict'
|
2016-06-17 08:09:33 +00:00
|
|
|
// external modules
|
2017-03-08 10:45:51 +00:00
|
|
|
var Sequelize = require('sequelize')
|
|
|
|
var async = require('async')
|
|
|
|
var moment = require('moment')
|
|
|
|
var childProcess = require('child_process')
|
|
|
|
var shortId = require('shortid')
|
2018-09-05 17:49:01 +00:00
|
|
|
var path = require('path')
|
2016-06-17 08:09:33 +00:00
|
|
|
|
2019-04-12 04:05:32 +00:00
|
|
|
var Op = Sequelize.Op
|
|
|
|
|
2016-06-17 08:09:33 +00:00
|
|
|
// core
|
2017-04-12 16:20:28 +00:00
|
|
|
var logger = require('../logger')
|
2016-06-17 08:09:33 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
var dmpWorker = createDmpWorker()
|
|
|
|
var dmpCallbackCache = {}
|
2016-11-18 04:09:58 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
function createDmpWorker () {
|
2018-09-05 17:49:01 +00:00
|
|
|
var worker = childProcess.fork(path.resolve(__dirname, '../workers/dmpWorker.js'), {
|
2017-03-08 10:45:51 +00:00
|
|
|
stdio: 'ignore'
|
|
|
|
})
|
2019-06-08 18:51:24 +00:00
|
|
|
logger.debug('dmp worker process started')
|
2017-03-08 10:45:51 +00:00
|
|
|
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
|
2019-06-08 18:51:24 +00:00
|
|
|
logger.debug(`dmp worker process exited with code ${code}`)
|
2017-03-08 10:45:51 +00:00
|
|
|
})
|
|
|
|
return worker
|
2016-11-18 04:09:58 +00:00
|
|
|
}
|
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
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-11-18 04:09:58 +00:00
|
|
|
}
|
|
|
|
|
2016-06-17 08:09:33 +00:00
|
|
|
module.exports = function (sequelize, DataTypes) {
|
2017-03-08 10:45:51 +00:00
|
|
|
var Revision = sequelize.define('Revision', {
|
|
|
|
id: {
|
|
|
|
type: DataTypes.UUID,
|
|
|
|
primaryKey: true,
|
|
|
|
defaultValue: Sequelize.UUIDV4
|
|
|
|
},
|
|
|
|
patch: {
|
2017-10-16 08:12:39 +00:00
|
|
|
type: DataTypes.TEXT('long'),
|
2017-03-08 10:45:51 +00:00
|
|
|
get: function () {
|
|
|
|
return sequelize.processData(this.getDataValue('patch'), '')
|
|
|
|
},
|
|
|
|
set: function (value) {
|
|
|
|
this.setDataValue('patch', sequelize.stripNullByte(value))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
lastContent: {
|
2017-10-16 08:12:39 +00:00
|
|
|
type: DataTypes.TEXT('long'),
|
2017-03-08 10:45:51 +00:00
|
|
|
get: function () {
|
|
|
|
return sequelize.processData(this.getDataValue('lastContent'), '')
|
|
|
|
},
|
|
|
|
set: function (value) {
|
|
|
|
this.setDataValue('lastContent', sequelize.stripNullByte(value))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
content: {
|
2017-10-16 08:12:39 +00:00
|
|
|
type: DataTypes.TEXT('long'),
|
2017-03-08 10:45:51 +00:00
|
|
|
get: function () {
|
|
|
|
return sequelize.processData(this.getDataValue('content'), '')
|
|
|
|
},
|
|
|
|
set: function (value) {
|
|
|
|
this.setDataValue('content', sequelize.stripNullByte(value))
|
|
|
|
}
|
|
|
|
},
|
|
|
|
length: {
|
|
|
|
type: DataTypes.INTEGER
|
|
|
|
},
|
|
|
|
authorship: {
|
2018-02-09 13:27:06 +00:00
|
|
|
type: DataTypes.TEXT('long'),
|
2017-03-08 10:45:51 +00:00
|
|
|
get: function () {
|
|
|
|
return sequelize.processData(this.getDataValue('authorship'), [], JSON.parse)
|
|
|
|
},
|
|
|
|
set: function (value) {
|
|
|
|
this.setDataValue('authorship', value ? JSON.stringify(value) : value)
|
|
|
|
}
|
|
|
|
}
|
2019-04-12 04:05:32 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
Revision.associate = function (models) {
|
|
|
|
Revision.belongsTo(models.Note, {
|
|
|
|
foreignKey: 'noteId',
|
|
|
|
as: 'note',
|
|
|
|
constraints: false,
|
|
|
|
onDelete: 'CASCADE',
|
|
|
|
hooks: true
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Revision.getNoteRevisions = function (note, callback) {
|
|
|
|
Revision.findAll({
|
|
|
|
where: {
|
|
|
|
noteId: note.id
|
2017-03-08 10:45:51 +00:00
|
|
|
},
|
2019-04-12 04:05:32 +00:00
|
|
|
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
|
2017-03-08 10:45:51 +00:00
|
|
|
})
|
2019-04-12 04:05:32 +00:00
|
|
|
}
|
|
|
|
callback(null, data)
|
|
|
|
}).catch(function (err) {
|
|
|
|
callback(err, null)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Revision.getPatchedNoteRevisionByTime = function (note, time, callback) {
|
|
|
|
// find all revisions to prepare for all possible calculation
|
|
|
|
Revision.findAll({
|
|
|
|
where: {
|
|
|
|
noteId: note.id
|
2017-03-08 10:45:51 +00:00
|
|
|
},
|
2019-04-12 04:05:32 +00:00
|
|
|
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: {
|
|
|
|
[Op.gte]: time
|
2017-03-08 10:45:51 +00:00
|
|
|
}
|
2019-04-12 04:05:32 +00:00
|
|
|
},
|
|
|
|
order: [['createdAt', 'DESC']]
|
|
|
|
}).then(function (count) {
|
|
|
|
if (count <= 0) return callback(null, null)
|
|
|
|
sendDmpWorker({
|
|
|
|
msg: 'get revision',
|
|
|
|
revisions: revisions,
|
|
|
|
count: count
|
|
|
|
}, callback)
|
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null)
|
|
|
|
})
|
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Revision.checkAllNotesRevision = function (callback) {
|
|
|
|
Revision.saveAllNotesRevision(function (err, notes) {
|
|
|
|
if (err) return callback(err, null)
|
|
|
|
if (!notes || notes.length <= 0) {
|
|
|
|
return callback(null, notes)
|
|
|
|
} else {
|
|
|
|
Revision.checkAllNotesRevision(callback)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Revision.saveAllNotesRevision = function (callback) {
|
|
|
|
sequelize.models.Note.findAll({
|
|
|
|
// query all notes that need to save for revision
|
|
|
|
where: {
|
|
|
|
[Op.and]: [
|
|
|
|
{
|
|
|
|
lastchangeAt: {
|
|
|
|
[Op.or]: {
|
|
|
|
[Op.eq]: null,
|
|
|
|
[Op.and]: {
|
|
|
|
[Op.ne]: null,
|
|
|
|
[Op.gt]: sequelize.col('createdAt')
|
2017-03-08 10:45:51 +00:00
|
|
|
}
|
|
|
|
}
|
2017-01-02 03:05:05 +00:00
|
|
|
}
|
2019-04-12 04:05:32 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
savedAt: {
|
|
|
|
[Op.or]: {
|
|
|
|
[Op.eq]: null,
|
|
|
|
[Op.lt]: sequelize.col('lastchangeAt')
|
|
|
|
}
|
2017-01-02 03:05:05 +00:00
|
|
|
}
|
2019-04-12 04:05:32 +00:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}).then(function (notes) {
|
|
|
|
if (notes.length <= 0) return callback(null, notes)
|
|
|
|
var savedNotes = []
|
|
|
|
async.each(notes, function (note, _callback) {
|
|
|
|
// 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)
|
|
|
|
if (moment().isAfter(lastchangeAt.add(5, 'minutes'))) {
|
|
|
|
savedNotes.push(note)
|
|
|
|
Revision.saveNoteRevision(note, _callback)
|
|
|
|
} else if (lastchangeAt.isAfter(savedAt.add(10, 'minutes'))) {
|
|
|
|
savedNotes.push(note)
|
|
|
|
Revision.saveNoteRevision(note, _callback)
|
|
|
|
} else {
|
|
|
|
return _callback(null, null)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
savedNotes.push(note)
|
|
|
|
Revision.saveNoteRevision(note, _callback)
|
|
|
|
}
|
|
|
|
}, function (err) {
|
|
|
|
if (err) {
|
|
|
|
return callback(err, null)
|
|
|
|
}
|
|
|
|
// 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)
|
|
|
|
})
|
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Revision.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 ? note.content : '',
|
|
|
|
length: note.content ? note.content.length : 0,
|
|
|
|
authorship: note.authorship
|
|
|
|
}).then(function (revision) {
|
|
|
|
Revision.finishSaveNoteRevision(note, revision, callback)
|
2017-03-08 10:45:51 +00:00
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null)
|
|
|
|
})
|
2019-04-12 04:05:32 +00:00
|
|
|
} else {
|
|
|
|
var latestRevision = revisions[0]
|
|
|
|
var lastContent = latestRevision.content || latestRevision.lastContent
|
|
|
|
var content = note.content
|
|
|
|
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)
|
|
|
|
latestRevision.update({
|
|
|
|
updatedAt: Date.now()
|
2017-03-08 10:45:51 +00:00
|
|
|
}).then(function (revision) {
|
|
|
|
Revision.finishSaveNoteRevision(note, revision, callback)
|
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null)
|
|
|
|
})
|
|
|
|
} else {
|
2019-04-12 04:05:32 +00:00
|
|
|
Revision.create({
|
|
|
|
noteId: note.id,
|
|
|
|
patch: patch,
|
|
|
|
content: note.content,
|
|
|
|
length: note.content.length,
|
|
|
|
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)
|
2017-03-08 10:45:51 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-04-12 04:05:32 +00:00
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
Revision.finishSaveNoteRevision = function (note, revision, callback) {
|
|
|
|
note.update({
|
|
|
|
savedAt: revision.updatedAt
|
|
|
|
}).then(function () {
|
|
|
|
return callback(null, revision)
|
|
|
|
}).catch(function (err) {
|
|
|
|
return callback(err, null)
|
|
|
|
})
|
|
|
|
}
|
2016-06-17 08:09:33 +00:00
|
|
|
|
2017-03-08 10:45:51 +00:00
|
|
|
return Revision
|
|
|
|
}
|