mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-03-15 08:24:10 +00:00
revision.ts: Formatting, use Utils instead of global Sequelize object
Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
parent
477a37b48c
commit
0ad30a0029
1 changed files with 63 additions and 54 deletions
|
@ -1,22 +1,23 @@
|
||||||
import {Table, Model, Column, DataType, BelongsTo, IsUUID, PrimaryKey, ForeignKey} from 'sequelize-typescript'
|
import {BelongsTo, Column, DataType, ForeignKey, IsUUID, Model, PrimaryKey, Table} from 'sequelize-typescript'
|
||||||
var Sequelize = require('sequelize')
|
|
||||||
import async = require('async')
|
|
||||||
import moment = require('moment')
|
|
||||||
import childProcess = require('child_process')
|
|
||||||
import shortId = require('shortid')
|
|
||||||
import path = require('path')
|
|
||||||
|
|
||||||
var Op = Sequelize.Op
|
|
||||||
|
|
||||||
// core
|
|
||||||
import logger = require('../logger')
|
|
||||||
import {ChildProcess} from "child_process";
|
import {ChildProcess} from "child_process";
|
||||||
import { Note } from './note'
|
import {Note} from './note'
|
||||||
|
import {Utils} from "../utils";
|
||||||
|
|
||||||
var dmpWorker : ChildProcess | null = createDmpWorker()
|
import Sequelize from "sequelize";
|
||||||
|
import async = require('async');
|
||||||
|
import moment = require('moment');
|
||||||
|
import childProcess = require('child_process');
|
||||||
|
import shortId = require('shortid');
|
||||||
|
import path = require('path');
|
||||||
|
// core
|
||||||
|
import logger = require('../logger');
|
||||||
|
|
||||||
|
const Op = Sequelize.Op;
|
||||||
|
|
||||||
|
var dmpWorker: ChildProcess | null = createDmpWorker()
|
||||||
var dmpCallbackCache = {}
|
var dmpCallbackCache = {}
|
||||||
|
|
||||||
function createDmpWorker () {
|
function createDmpWorker() {
|
||||||
var worker = childProcess.fork(path.resolve(__dirname, '../workers/dmpWorker.js'), ['ignore'])
|
var worker = childProcess.fork(path.resolve(__dirname, '../workers/dmpWorker.js'), ['ignore'])
|
||||||
logger.debug('dmp worker process started')
|
logger.debug('dmp worker process started')
|
||||||
worker.on('message', function (data: any) {
|
worker.on('message', function (data: any) {
|
||||||
|
@ -41,7 +42,7 @@ function createDmpWorker () {
|
||||||
return worker
|
return worker
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendDmpWorker (data, callback) {
|
function sendDmpWorker(data, callback) {
|
||||||
if (!dmpWorker) dmpWorker = createDmpWorker()
|
if (!dmpWorker) dmpWorker = createDmpWorker()
|
||||||
var cacheKey = Date.now() + '_' + shortId.generate()
|
var cacheKey = Date.now() + '_' + shortId.generate()
|
||||||
dmpCallbackCache[cacheKey] = callback
|
dmpCallbackCache[cacheKey] = callback
|
||||||
|
@ -56,39 +57,43 @@ export class Revision extends Model<Revision> {
|
||||||
@IsUUID(4)
|
@IsUUID(4)
|
||||||
@PrimaryKey
|
@PrimaryKey
|
||||||
@Column
|
@Column
|
||||||
id: string;
|
id: string;
|
||||||
|
|
||||||
@Column(DataType.TEXT({ length: 'long' }))
|
@Column(DataType.TEXT({length: 'long'}))
|
||||||
get patch(): string {
|
get patch(): string {
|
||||||
return Sequelize.processData(this.getDataValue('patch'), '')
|
return Utils.processData(this.getDataValue('patch'), '')
|
||||||
}
|
}
|
||||||
|
|
||||||
set patch(value: string) {
|
set patch(value: string) {
|
||||||
this.setDataValue('patch', Sequelize.stripNullByte(value))
|
this.setDataValue('patch', Utils.stripNullByte(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Column(DataType.TEXT({ length: 'long' }))
|
@Column(DataType.TEXT({length: 'long'}))
|
||||||
get lastContent(): string {
|
get lastContent(): string {
|
||||||
return Sequelize.processData(this.getDataValue('lastContent'), '')
|
return Utils.processData(this.getDataValue('lastContent'), '')
|
||||||
}
|
|
||||||
set lastContent(value: string) {
|
|
||||||
this.setDataValue('lastContent', Sequelize.stripNullByte(value))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Column(DataType.TEXT({ length: 'long' }))
|
set lastContent(value: string) {
|
||||||
get content(): string {
|
this.setDataValue('lastContent', Utils.stripNullByte(value))
|
||||||
return Sequelize.processData(this.getDataValue('content'), '')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Column(DataType.TEXT({length: 'long'}))
|
||||||
|
get content(): string {
|
||||||
|
return Utils.processData(this.getDataValue('content'), '')
|
||||||
|
}
|
||||||
|
|
||||||
set content(value: string) {
|
set content(value: string) {
|
||||||
this.setDataValue('content', Sequelize.stripNullByte(value))
|
this.setDataValue('content', Utils.stripNullByte(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Column(DataType.INTEGER)
|
@Column(DataType.INTEGER)
|
||||||
length : number
|
length: number
|
||||||
|
|
||||||
@Column(DataType.TEXT({ length: 'long' }))
|
@Column(DataType.TEXT({length: 'long'}))
|
||||||
get authorship(): string {
|
get authorship(): string {
|
||||||
return Sequelize.processData(this.getDataValue('authorship'), [], JSON.parse)
|
return Utils.processData(this.getDataValue('authorship'), [], JSON.parse)
|
||||||
}
|
}
|
||||||
|
|
||||||
set authorship(value: string) {
|
set authorship(value: string) {
|
||||||
this.setDataValue('authorship', value ? JSON.stringify(value) : value)
|
this.setDataValue('authorship', value ? JSON.stringify(value) : value)
|
||||||
}
|
}
|
||||||
|
@ -97,21 +102,21 @@ export class Revision extends Model<Revision> {
|
||||||
@Column(DataType.UUID)
|
@Column(DataType.UUID)
|
||||||
noteId: string
|
noteId: string
|
||||||
|
|
||||||
@BelongsTo(() => Note, { foreignKey: 'noteId', constraints: false, onDelete: 'CASCADE', hooks: true })
|
@BelongsTo(() => Note, {foreignKey: 'noteId', constraints: false, onDelete: 'CASCADE', hooks: true})
|
||||||
note: Note;
|
note: Note;
|
||||||
|
|
||||||
getNoteRevisions (note, callback) {
|
getNoteRevisions(note, callback) {
|
||||||
Revision.findAll({
|
Revision.findAll({
|
||||||
where: {
|
where: {
|
||||||
noteId: note.id
|
noteId: note.id
|
||||||
},
|
},
|
||||||
order: [['createdAt', 'DESC']]
|
order: [['createdAt', 'DESC']]
|
||||||
}).then(function (revisions) {
|
}).then(function (revisions) {
|
||||||
var data : any[] = []
|
var data: any[] = []
|
||||||
for (var i = 0; i < revisions.length; i++) {
|
for (var i = 0; i < revisions.length; i++) {
|
||||||
var revision = revisions[i]
|
var revision = revisions[i]
|
||||||
data.push({
|
data.push({
|
||||||
time : moment(revision.createdAt).valueOf(),
|
time: moment(revision.createdAt).valueOf(),
|
||||||
length: revision.length
|
length: revision.length
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
@ -120,7 +125,8 @@ export class Revision extends Model<Revision> {
|
||||||
callback(err, null)
|
callback(err, null)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
getPatchedNoteRevisionByTime (note, time, callback) {
|
|
||||||
|
getPatchedNoteRevisionByTime(note, time, callback) {
|
||||||
// find all revisions to prepare for all possible calculation
|
// find all revisions to prepare for all possible calculation
|
||||||
Revision.findAll({
|
Revision.findAll({
|
||||||
where: {
|
where: {
|
||||||
|
@ -151,17 +157,19 @@ export class Revision extends Model<Revision> {
|
||||||
return callback(err, null)
|
return callback(err, null)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
static checkAllNotesRevision (callback) {
|
|
||||||
this.saveAllNotesRevision(function (err, notes) {
|
static checkAllNotesRevision(callback) {
|
||||||
|
Revision.saveAllNotesRevision(function (err, notes) {
|
||||||
if (err) return callback(err, null)
|
if (err) return callback(err, null)
|
||||||
if (!notes || notes.length <= 0) {
|
if (!notes || notes.length <= 0) {
|
||||||
return callback(null, notes)
|
return callback(null, notes)
|
||||||
} else {
|
} else {
|
||||||
this.checkAllNotesRevision(callback)
|
Revision.checkAllNotesRevision(callback)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
static saveAllNotesRevision (callback) {
|
|
||||||
|
static saveAllNotesRevision(callback) {
|
||||||
Note.findAll({
|
Note.findAll({
|
||||||
// query all notes that need to save for revision
|
// query all notes that need to save for revision
|
||||||
where: {
|
where: {
|
||||||
|
@ -189,24 +197,24 @@ export class Revision extends Model<Revision> {
|
||||||
}
|
}
|
||||||
}).then(function (notes) {
|
}).then(function (notes) {
|
||||||
if (notes.length <= 0) return callback(null, notes)
|
if (notes.length <= 0) return callback(null, notes)
|
||||||
var savedNotes : any[] = []
|
var savedNotes: any[] = []
|
||||||
async.each(notes, function (note : any, _callback) {
|
async.each(notes, function (note: any, _callback) {
|
||||||
// revision saving policy: note not been modified for 5 mins or not save for 10 mins
|
// revision saving policy: note not been modified for 5 mins or not save for 10 mins
|
||||||
if (note.lastchangeAt && note.savedAt) {
|
if (note.lastchangeAt && note.savedAt) {
|
||||||
var lastchangeAt = moment(note.lastchangeAt)
|
var lastchangeAt = moment(note.lastchangeAt)
|
||||||
var savedAt = moment(note.savedAt)
|
var savedAt = moment(note.savedAt)
|
||||||
if (moment().isAfter(lastchangeAt.add(5, 'minutes'))) {
|
if (moment().isAfter(lastchangeAt.add(5, 'minutes'))) {
|
||||||
savedNotes.push(note)
|
savedNotes.push(note)
|
||||||
this.saveNoteRevision(note, _callback)
|
Revision.saveNoteRevision(note, _callback)
|
||||||
} else if (lastchangeAt.isAfter(savedAt.add(10, 'minutes'))) {
|
} else if (lastchangeAt.isAfter(savedAt.add(10, 'minutes'))) {
|
||||||
savedNotes.push(note)
|
savedNotes.push(note)
|
||||||
this.saveNoteRevision(note, _callback)
|
Revision.saveNoteRevision(note, _callback)
|
||||||
} else {
|
} else {
|
||||||
return _callback(null, null)
|
return _callback(null, null)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
savedNotes.push(note)
|
savedNotes.push(note)
|
||||||
this.saveNoteRevision(note, _callback)
|
Revision.saveNoteRevision(note, _callback)
|
||||||
}
|
}
|
||||||
}, function (err) {
|
}, function (err) {
|
||||||
if (err) {
|
if (err) {
|
||||||
|
@ -220,7 +228,8 @@ export class Revision extends Model<Revision> {
|
||||||
return callback(err, null)
|
return callback(err, null)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
saveNoteRevision (note, callback) {
|
|
||||||
|
static saveNoteRevision(note, callback) {
|
||||||
Revision.findAll({
|
Revision.findAll({
|
||||||
where: {
|
where: {
|
||||||
noteId: note.id
|
noteId: note.id
|
||||||
|
@ -229,13 +238,13 @@ export class Revision extends Model<Revision> {
|
||||||
}).then(function (revisions) {
|
}).then(function (revisions) {
|
||||||
if (revisions.length <= 0) {
|
if (revisions.length <= 0) {
|
||||||
// if no revision available
|
// if no revision available
|
||||||
this.create({
|
Revision.create({
|
||||||
noteId: note.id,
|
noteId: note.id,
|
||||||
lastContent: note.content ? note.content : '',
|
lastContent: note.content ? note.content : '',
|
||||||
length: note.content ? note.content.length : 0,
|
length: note.content ? note.content.length : 0,
|
||||||
authorship: note.authorship
|
authorship: note.authorship
|
||||||
}).then(function (revision) {
|
}).then(function (revision) {
|
||||||
this.finishSaveNoteRevision(note, revision, callback)
|
Revision.finishSaveNoteRevision(note, revision, callback)
|
||||||
}).catch(function (err) {
|
}).catch(function (err) {
|
||||||
return callback(err, null)
|
return callback(err, null)
|
||||||
})
|
})
|
||||||
|
@ -255,12 +264,12 @@ export class Revision extends Model<Revision> {
|
||||||
latestRevision.update({
|
latestRevision.update({
|
||||||
updatedAt: Date.now()
|
updatedAt: Date.now()
|
||||||
}).then(function (revision) {
|
}).then(function (revision) {
|
||||||
this.finishSaveNoteRevision(note, revision, callback)
|
Revision.finishSaveNoteRevision(note, revision, callback)
|
||||||
}).catch(function (err) {
|
}).catch(function (err) {
|
||||||
return callback(err, null)
|
return callback(err, null)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
this.create({
|
Revision.create({
|
||||||
noteId: note.id,
|
noteId: note.id,
|
||||||
patch: patch,
|
patch: patch,
|
||||||
content: note.content,
|
content: note.content,
|
||||||
|
@ -271,7 +280,7 @@ export class Revision extends Model<Revision> {
|
||||||
latestRevision.update({
|
latestRevision.update({
|
||||||
content: null
|
content: null
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
this.finishSaveNoteRevision(note, revision, callback)
|
Revision.finishSaveNoteRevision(note, revision, callback)
|
||||||
}).catch(function (err) {
|
}).catch(function (err) {
|
||||||
return callback(err, null)
|
return callback(err, null)
|
||||||
})
|
})
|
||||||
|
@ -285,7 +294,8 @@ export class Revision extends Model<Revision> {
|
||||||
return callback(err, null)
|
return callback(err, null)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
finishSaveNoteRevision (note, revision, callback) {
|
|
||||||
|
static finishSaveNoteRevision(note, revision, callback) {
|
||||||
note.update({
|
note.update({
|
||||||
savedAt: revision.updatedAt
|
savedAt: revision.updatedAt
|
||||||
}).then(function () {
|
}).then(function () {
|
||||||
|
@ -296,6 +306,5 @@ export class Revision extends Model<Revision> {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue