2020-04-11 08:32:53 -04:00
|
|
|
import { ChildProcess } from 'child_process'
|
2019-04-12 00:05:32 -04:00
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
import Sequelize from 'sequelize'
|
2020-04-13 15:36:09 -04:00
|
|
|
import { BelongsTo, Column, DataType, Default, ForeignKey, Model, PrimaryKey, Table } from 'sequelize-typescript'
|
2020-04-12 07:11:06 -04:00
|
|
|
// core
|
|
|
|
import { logger } from '../logger'
|
2020-04-13 13:47:08 -04:00
|
|
|
import { processData, stripNullByte } from '../utils'
|
|
|
|
import { Note } from './note'
|
2020-04-12 07:52:50 -04:00
|
|
|
import async = require('async')
|
|
|
|
import childProcess = require('child_process')
|
2020-04-13 13:47:08 -04:00
|
|
|
import moment = require('moment')
|
2020-04-12 07:52:50 -04:00
|
|
|
import path = require('path')
|
2020-04-13 13:47:08 -04:00
|
|
|
import shortId = require('shortid')
|
2020-04-11 05:27:29 -04:00
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
const Op = Sequelize.Op
|
2016-06-17 04:09:33 -04:00
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
const dmpCallbackCache = {}
|
2016-11-17 23:09:58 -05:00
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
class Data {
|
2020-04-12 07:52:50 -04:00
|
|
|
msg
|
|
|
|
cacheKey
|
|
|
|
error
|
|
|
|
result
|
2020-04-11 08:32:53 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function createDmpWorker (): ChildProcess {
|
2020-04-13 10:21:00 -04:00
|
|
|
const worker = childProcess.fork(path.resolve(__dirname, '../workers/dmpWorker.js'), ['ignore'])
|
2019-06-08 14:51:24 -04:00
|
|
|
logger.debug('dmp worker process started')
|
2020-04-11 08:32:53 -04:00
|
|
|
worker.on('message', function (data: Data) {
|
2017-03-08 05:45:51 -05:00
|
|
|
if (!data || !data.msg || !data.cacheKey) {
|
2020-04-11 08:32:53 -04:00
|
|
|
logger.error('dmp worker error: not enough data on message')
|
2020-04-12 08:26:32 -04:00
|
|
|
return
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2020-04-11 08:32:53 -04:00
|
|
|
const cacheKey = data.cacheKey
|
2017-03-08 05:45:51 -05:00
|
|
|
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) {
|
2019-06-08 14:51:24 -04:00
|
|
|
logger.debug(`dmp worker process exited with code ${code}`)
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
|
|
|
return worker
|
2016-11-17 23:09:58 -05:00
|
|
|
}
|
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
let dmpWorker: ChildProcess = createDmpWorker()
|
|
|
|
|
|
|
|
function sendDmpWorker (data, callback): void {
|
2020-04-13 15:36:09 -04:00
|
|
|
if (!dmpWorker) {
|
2020-04-11 08:32:53 -04:00
|
|
|
dmpWorker = createDmpWorker()
|
|
|
|
}
|
|
|
|
const cacheKey = Date.now() + '_' + shortId.generate()
|
2017-03-08 05:45:51 -05:00
|
|
|
dmpCallbackCache[cacheKey] = callback
|
|
|
|
data = Object.assign(data, {
|
|
|
|
cacheKey: cacheKey
|
|
|
|
})
|
|
|
|
dmpWorker.send(data)
|
2016-11-17 23:09:58 -05:00
|
|
|
}
|
|
|
|
|
2020-04-10 16:06:12 -04:00
|
|
|
@Table
|
|
|
|
export class Revision extends Model<Revision> {
|
2020-04-13 13:47:08 -04:00
|
|
|
@Default(Sequelize.UUIDV4)
|
2020-04-10 16:06:12 -04:00
|
|
|
@PrimaryKey
|
2020-04-13 13:47:08 -04:00
|
|
|
@Column(DataType.UUID)
|
2020-04-12 07:52:50 -04:00
|
|
|
id: string
|
|
|
|
|
|
|
|
@Column(DataType.INTEGER)
|
|
|
|
length: number
|
|
|
|
|
|
|
|
@ForeignKey(() => Note)
|
|
|
|
@Column(DataType.UUID)
|
|
|
|
noteId: string
|
|
|
|
|
|
|
|
@BelongsTo(() => Note, { foreignKey: 'noteId', constraints: false, onDelete: 'CASCADE', hooks: true })
|
|
|
|
note: Note
|
2019-04-12 00:05:32 -04:00
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
@Column(DataType.TEXT({ length: 'long' }))
|
|
|
|
get patch (): string {
|
2020-04-12 15:02:41 -04:00
|
|
|
return processData(this.getDataValue('patch'), '')
|
2020-04-10 16:06:12 -04:00
|
|
|
}
|
2020-04-11 05:27:29 -04:00
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
set patch (value: string) {
|
2020-04-12 15:02:41 -04:00
|
|
|
this.setDataValue('patch', stripNullByte(value))
|
2020-04-10 16:06:12 -04:00
|
|
|
}
|
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
@Column(DataType.TEXT({ length: 'long' }))
|
|
|
|
get lastContent (): string {
|
2020-04-12 15:02:41 -04:00
|
|
|
return processData(this.getDataValue('lastContent'), '')
|
2019-04-12 00:05:32 -04:00
|
|
|
}
|
2020-04-11 05:27:29 -04:00
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
set lastContent (value: string) {
|
2020-04-12 15:02:41 -04:00
|
|
|
this.setDataValue('lastContent', stripNullByte(value))
|
2020-04-10 16:06:12 -04:00
|
|
|
}
|
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
@Column(DataType.TEXT({ length: 'long' }))
|
|
|
|
get content (): string {
|
2020-04-12 15:02:41 -04:00
|
|
|
return processData(this.getDataValue('content'), '')
|
2020-04-10 16:06:12 -04:00
|
|
|
}
|
2020-04-11 05:27:29 -04:00
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
set content (value: string) {
|
2020-04-12 15:02:41 -04:00
|
|
|
this.setDataValue('content', stripNullByte(value))
|
2020-04-10 16:06:12 -04:00
|
|
|
}
|
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
@Column(DataType.TEXT({ length: 'long' }))
|
|
|
|
get authorship (): string {
|
2020-04-12 15:02:41 -04:00
|
|
|
return processData(this.getDataValue('authorship'), [], JSON.parse)
|
2020-04-10 16:06:12 -04:00
|
|
|
}
|
2020-04-11 05:27:29 -04:00
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
set authorship (value: string) {
|
2020-04-10 16:06:12 -04:00
|
|
|
this.setDataValue('authorship', value ? JSON.stringify(value) : value)
|
|
|
|
}
|
|
|
|
|
2020-04-11 10:08:40 -04:00
|
|
|
static getNoteRevisions (note: Note, callback): void {
|
2019-04-12 00:05:32 -04:00
|
|
|
Revision.findAll({
|
|
|
|
where: {
|
|
|
|
noteId: note.id
|
2017-03-08 05:45:51 -05:00
|
|
|
},
|
2019-04-12 00:05:32 -04:00
|
|
|
order: [['createdAt', 'DESC']]
|
2020-04-12 13:31:51 -04:00
|
|
|
}).then(function (revisions: Revision[]) {
|
2020-04-11 08:32:53 -04:00
|
|
|
class RevisionDataActions { // TODO: Fix Type in actions.ts
|
2020-04-12 07:52:50 -04:00
|
|
|
time
|
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
length
|
|
|
|
}
|
2020-04-12 07:52:50 -04:00
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
const data: RevisionDataActions[] = []
|
2020-04-12 13:31:51 -04:00
|
|
|
revisions.forEach(function (revision: Revision) {
|
2019-04-12 00:05:32 -04:00
|
|
|
data.push({
|
2020-04-11 05:27:29 -04:00
|
|
|
time: moment(revision.createdAt).valueOf(),
|
2019-04-12 00:05:32 -04:00
|
|
|
length: revision.length
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
2020-04-11 06:29:41 -04:00
|
|
|
})
|
2019-04-12 00:05:32 -04:00
|
|
|
callback(null, data)
|
|
|
|
}).catch(function (err) {
|
|
|
|
callback(err, null)
|
|
|
|
})
|
|
|
|
}
|
2020-04-11 05:27:29 -04:00
|
|
|
|
2020-04-11 10:08:40 -04:00
|
|
|
static getPatchedNoteRevisionByTime (note: Note, time, errorCallback): void {
|
2019-04-12 00:05:32 -04:00
|
|
|
// find all revisions to prepare for all possible calculation
|
|
|
|
Revision.findAll({
|
|
|
|
where: {
|
|
|
|
noteId: note.id
|
2017-03-08 05:45:51 -05:00
|
|
|
},
|
2019-04-12 00:05:32 -04:00
|
|
|
order: [['createdAt', 'DESC']]
|
2020-04-12 13:31:51 -04:00
|
|
|
}).then(function (revisions: Revision[]) {
|
2020-04-11 08:32:53 -04:00
|
|
|
if (revisions.length <= 0) {
|
|
|
|
errorCallback(null, null)
|
2020-04-12 08:26:32 -04:00
|
|
|
return
|
2020-04-11 08:32:53 -04:00
|
|
|
}
|
2019-04-12 00:05:32 -04:00
|
|
|
// measure target revision position
|
|
|
|
Revision.count({
|
|
|
|
where: {
|
|
|
|
noteId: note.id,
|
|
|
|
createdAt: {
|
|
|
|
[Op.gte]: time
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
2020-04-11 08:32:53 -04:00
|
|
|
}
|
2020-04-12 13:31:51 -04:00
|
|
|
}).then(function (count: number) {
|
2020-04-11 08:32:53 -04:00
|
|
|
if (count <= 0) {
|
|
|
|
errorCallback(null, null)
|
2020-04-12 08:26:32 -04:00
|
|
|
return
|
2020-04-11 08:32:53 -04:00
|
|
|
}
|
2019-04-12 00:05:32 -04:00
|
|
|
sendDmpWorker({
|
|
|
|
msg: 'get revision',
|
|
|
|
revisions: revisions,
|
|
|
|
count: count
|
2020-04-11 08:32:53 -04:00
|
|
|
}, errorCallback)
|
2019-04-12 00:05:32 -04:00
|
|
|
}).catch(function (err) {
|
2020-04-11 08:32:53 -04:00
|
|
|
errorCallback(err, null)
|
2019-04-12 00:05:32 -04:00
|
|
|
})
|
|
|
|
}).catch(function (err) {
|
2020-04-11 08:32:53 -04:00
|
|
|
errorCallback(err, null)
|
2019-04-12 00:05:32 -04:00
|
|
|
})
|
|
|
|
}
|
2020-04-11 05:27:29 -04:00
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
static checkAllNotesRevision (callback): void {
|
|
|
|
Revision.saveAllNotesRevision(function (err, notes: Note[]) {
|
|
|
|
if (err) {
|
|
|
|
callback(err, null)
|
2020-04-12 08:26:32 -04:00
|
|
|
return
|
2020-04-11 08:32:53 -04:00
|
|
|
}
|
2019-04-12 00:05:32 -04:00
|
|
|
if (!notes || notes.length <= 0) {
|
2020-04-11 08:32:53 -04:00
|
|
|
callback(null, notes)
|
2019-04-12 00:05:32 -04:00
|
|
|
} else {
|
2020-04-11 05:27:29 -04:00
|
|
|
Revision.checkAllNotesRevision(callback)
|
2019-04-12 00:05:32 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2020-04-11 05:27:29 -04:00
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
static saveAllNotesRevision (callback): void {
|
2020-04-10 18:34:18 -04:00
|
|
|
Note.findAll({
|
2019-04-12 00:05:32 -04:00
|
|
|
// query all notes that need to save for revision
|
|
|
|
where: {
|
|
|
|
[Op.and]: [
|
|
|
|
{
|
|
|
|
lastchangeAt: {
|
|
|
|
[Op.or]: {
|
|
|
|
[Op.eq]: null,
|
|
|
|
[Op.and]: {
|
|
|
|
[Op.ne]: null,
|
2020-04-10 16:06:12 -04:00
|
|
|
[Op.gt]: Sequelize.col('createdAt')
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|
|
|
|
}
|
2017-01-01 22:05:05 -05:00
|
|
|
}
|
2019-04-12 00:05:32 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
savedAt: {
|
|
|
|
[Op.or]: {
|
|
|
|
[Op.eq]: null,
|
2020-04-10 16:06:12 -04:00
|
|
|
[Op.lt]: Sequelize.col('lastchangeAt')
|
2019-04-12 00:05:32 -04:00
|
|
|
}
|
2017-01-01 22:05:05 -05:00
|
|
|
}
|
2019-04-12 00:05:32 -04:00
|
|
|
}
|
|
|
|
]
|
|
|
|
}
|
2020-04-11 07:14:20 -04:00
|
|
|
}).then(function (notes: Note[]) {
|
2020-04-11 08:32:53 -04:00
|
|
|
if (notes.length <= 0) {
|
|
|
|
callback(null, notes)
|
2020-04-12 08:26:32 -04:00
|
|
|
return
|
2020-04-11 08:32:53 -04:00
|
|
|
}
|
|
|
|
const savedNotes: Note[] = []
|
2020-04-11 07:14:20 -04:00
|
|
|
async.each(notes, function (note: Note, _callback) {
|
2019-04-12 00:05:32 -04:00
|
|
|
// revision saving policy: note not been modified for 5 mins or not save for 10 mins
|
|
|
|
if (note.lastchangeAt && note.savedAt) {
|
2020-04-11 08:32:53 -04:00
|
|
|
const lastchangeAt = moment(note.lastchangeAt)
|
|
|
|
const savedAt = moment(note.savedAt)
|
2019-04-12 00:05:32 -04:00
|
|
|
if (moment().isAfter(lastchangeAt.add(5, 'minutes'))) {
|
|
|
|
savedNotes.push(note)
|
2020-04-11 05:27:29 -04:00
|
|
|
Revision.saveNoteRevision(note, _callback)
|
2019-04-12 00:05:32 -04:00
|
|
|
} else if (lastchangeAt.isAfter(savedAt.add(10, 'minutes'))) {
|
|
|
|
savedNotes.push(note)
|
2020-04-11 05:27:29 -04:00
|
|
|
Revision.saveNoteRevision(note, _callback)
|
2019-04-12 00:05:32 -04:00
|
|
|
} else {
|
2020-04-11 08:32:53 -04:00
|
|
|
_callback(null, null)
|
2019-04-12 00:05:32 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
savedNotes.push(note)
|
2020-04-11 05:27:29 -04:00
|
|
|
Revision.saveNoteRevision(note, _callback)
|
2019-04-12 00:05:32 -04:00
|
|
|
}
|
|
|
|
}, function (err) {
|
|
|
|
if (err) {
|
2020-04-11 08:32:53 -04:00
|
|
|
callback(err, null)
|
2020-04-12 08:26:32 -04:00
|
|
|
return
|
2019-04-12 00:05:32 -04:00
|
|
|
}
|
|
|
|
// return null when no notes need saving at this moment but have delayed tasks to be done
|
2020-04-11 08:32:53 -04:00
|
|
|
const result = ((savedNotes.length === 0) && (notes.length > 0)) ? null : savedNotes
|
|
|
|
callback(null, result)
|
2019-04-12 00:05:32 -04:00
|
|
|
})
|
|
|
|
}).catch(function (err) {
|
2020-04-11 08:32:53 -04:00
|
|
|
callback(err, null)
|
2019-04-12 00:05:32 -04:00
|
|
|
})
|
|
|
|
}
|
2020-04-11 05:27:29 -04:00
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
static saveNoteRevision (note: Note, callback): void {
|
2019-04-12 00:05:32 -04:00
|
|
|
Revision.findAll({
|
|
|
|
where: {
|
|
|
|
noteId: note.id
|
|
|
|
},
|
|
|
|
order: [['createdAt', 'DESC']]
|
2020-04-11 08:32:53 -04:00
|
|
|
}).then(function (revisions: Revision[]) {
|
2019-04-12 00:05:32 -04:00
|
|
|
if (revisions.length <= 0) {
|
|
|
|
// if no revision available
|
2020-04-11 08:32:53 -04:00
|
|
|
let noteContent = note.content
|
2020-04-11 07:14:20 -04:00
|
|
|
if (noteContent.length === 0) {
|
2020-04-11 08:32:53 -04:00
|
|
|
noteContent = ''
|
2020-04-11 07:14:20 -04:00
|
|
|
}
|
2020-04-11 05:27:29 -04:00
|
|
|
Revision.create({
|
2019-04-12 00:05:32 -04:00
|
|
|
noteId: note.id,
|
2020-04-11 07:14:20 -04:00
|
|
|
lastContent: noteContent,
|
|
|
|
length: noteContent.length,
|
2019-04-12 00:05:32 -04:00
|
|
|
authorship: note.authorship
|
2020-04-11 08:32:53 -04:00
|
|
|
}).then(function (revision: Revision) {
|
2020-04-11 05:27:29 -04:00
|
|
|
Revision.finishSaveNoteRevision(note, revision, callback)
|
2017-03-08 05:45:51 -05:00
|
|
|
}).catch(function (err) {
|
2020-04-11 08:32:53 -04:00
|
|
|
callback(err, null)
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
2019-04-12 00:05:32 -04:00
|
|
|
} else {
|
2020-04-11 08:32:53 -04:00
|
|
|
const latestRevision = revisions[0]
|
|
|
|
const lastContent = latestRevision.content || latestRevision.lastContent
|
|
|
|
const content = note.content
|
2019-04-12 00:05:32 -04:00
|
|
|
sendDmpWorker({
|
|
|
|
msg: 'create patch',
|
|
|
|
lastDoc: lastContent,
|
|
|
|
currDoc: content
|
|
|
|
}, function (err, patch) {
|
2020-04-11 08:32:53 -04:00
|
|
|
if (err) {
|
|
|
|
logger.error('save note revision error', err)
|
2020-04-12 08:26:32 -04:00
|
|
|
return
|
2020-04-11 08:32:53 -04:00
|
|
|
}
|
2019-04-12 00:05:32 -04:00
|
|
|
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()
|
2020-04-11 08:32:53 -04:00
|
|
|
}).then(function (revision: Revision) {
|
2020-04-11 05:27:29 -04:00
|
|
|
Revision.finishSaveNoteRevision(note, revision, callback)
|
2017-03-08 05:45:51 -05:00
|
|
|
}).catch(function (err) {
|
2020-04-11 08:32:53 -04:00
|
|
|
callback(err, null)
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
|
|
|
} else {
|
2020-04-11 05:27:29 -04:00
|
|
|
Revision.create({
|
2019-04-12 00:05:32 -04:00
|
|
|
noteId: note.id,
|
|
|
|
patch: patch,
|
|
|
|
content: note.content,
|
|
|
|
length: note.content.length,
|
|
|
|
authorship: note.authorship
|
2020-04-11 08:32:53 -04:00
|
|
|
}).then(function (revision: Revision) {
|
2019-04-12 00:05:32 -04:00
|
|
|
// clear last revision content to reduce db size
|
|
|
|
latestRevision.update({
|
|
|
|
content: null
|
|
|
|
}).then(function () {
|
2020-04-11 05:27:29 -04:00
|
|
|
Revision.finishSaveNoteRevision(note, revision, callback)
|
2019-04-12 00:05:32 -04:00
|
|
|
}).catch(function (err) {
|
2020-04-11 08:32:53 -04:00
|
|
|
callback(err, null)
|
2019-04-12 00:05:32 -04:00
|
|
|
})
|
|
|
|
}).catch(function (err) {
|
2020-04-11 08:32:53 -04:00
|
|
|
callback(err, null)
|
2017-03-08 05:45:51 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2019-04-12 00:05:32 -04:00
|
|
|
}).catch(function (err) {
|
2020-04-11 08:32:53 -04:00
|
|
|
callback(err, null)
|
2019-04-12 00:05:32 -04:00
|
|
|
})
|
|
|
|
}
|
2020-04-11 05:27:29 -04:00
|
|
|
|
2020-04-11 08:32:53 -04:00
|
|
|
static finishSaveNoteRevision (note: Note, revision: Revision, callback): void {
|
2019-04-12 00:05:32 -04:00
|
|
|
note.update({
|
|
|
|
savedAt: revision.updatedAt
|
|
|
|
}).then(function () {
|
2020-04-11 08:32:53 -04:00
|
|
|
callback(null, revision)
|
2019-04-12 00:05:32 -04:00
|
|
|
}).catch(function (err) {
|
2020-04-11 08:32:53 -04:00
|
|
|
callback(err, null)
|
2019-04-12 00:05:32 -04:00
|
|
|
})
|
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
}
|