mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-03-15 03:53:03 +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'
|
||||
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 {BelongsTo, Column, DataType, ForeignKey, IsUUID, Model, PrimaryKey, Table} from 'sequelize-typescript'
|
||||
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 = {}
|
||||
|
||||
function createDmpWorker () {
|
||||
function createDmpWorker() {
|
||||
var worker = childProcess.fork(path.resolve(__dirname, '../workers/dmpWorker.js'), ['ignore'])
|
||||
logger.debug('dmp worker process started')
|
||||
worker.on('message', function (data: any) {
|
||||
|
@ -41,7 +42,7 @@ function createDmpWorker () {
|
|||
return worker
|
||||
}
|
||||
|
||||
function sendDmpWorker (data, callback) {
|
||||
function sendDmpWorker(data, callback) {
|
||||
if (!dmpWorker) dmpWorker = createDmpWorker()
|
||||
var cacheKey = Date.now() + '_' + shortId.generate()
|
||||
dmpCallbackCache[cacheKey] = callback
|
||||
|
@ -56,39 +57,43 @@ export class Revision extends Model<Revision> {
|
|||
@IsUUID(4)
|
||||
@PrimaryKey
|
||||
@Column
|
||||
id: string;
|
||||
id: string;
|
||||
|
||||
@Column(DataType.TEXT({ length: 'long' }))
|
||||
@Column(DataType.TEXT({length: 'long'}))
|
||||
get patch(): string {
|
||||
return Sequelize.processData(this.getDataValue('patch'), '')
|
||||
return Utils.processData(this.getDataValue('patch'), '')
|
||||
}
|
||||
|
||||
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 {
|
||||
return Sequelize.processData(this.getDataValue('lastContent'), '')
|
||||
}
|
||||
set lastContent(value: string) {
|
||||
this.setDataValue('lastContent', Sequelize.stripNullByte(value))
|
||||
return Utils.processData(this.getDataValue('lastContent'), '')
|
||||
}
|
||||
|
||||
@Column(DataType.TEXT({ length: 'long' }))
|
||||
get content(): string {
|
||||
return Sequelize.processData(this.getDataValue('content'), '')
|
||||
set lastContent(value: string) {
|
||||
this.setDataValue('lastContent', Utils.stripNullByte(value))
|
||||
}
|
||||
|
||||
@Column(DataType.TEXT({length: 'long'}))
|
||||
get content(): string {
|
||||
return Utils.processData(this.getDataValue('content'), '')
|
||||
}
|
||||
|
||||
set content(value: string) {
|
||||
this.setDataValue('content', Sequelize.stripNullByte(value))
|
||||
this.setDataValue('content', Utils.stripNullByte(value))
|
||||
}
|
||||
|
||||
@Column(DataType.INTEGER)
|
||||
length : number
|
||||
length: number
|
||||
|
||||
@Column(DataType.TEXT({ length: 'long' }))
|
||||
@Column(DataType.TEXT({length: 'long'}))
|
||||
get authorship(): string {
|
||||
return Sequelize.processData(this.getDataValue('authorship'), [], JSON.parse)
|
||||
return Utils.processData(this.getDataValue('authorship'), [], JSON.parse)
|
||||
}
|
||||
|
||||
set authorship(value: string) {
|
||||
this.setDataValue('authorship', value ? JSON.stringify(value) : value)
|
||||
}
|
||||
|
@ -97,21 +102,21 @@ export class Revision extends Model<Revision> {
|
|||
@Column(DataType.UUID)
|
||||
noteId: string
|
||||
|
||||
@BelongsTo(() => Note, { foreignKey: 'noteId', constraints: false, onDelete: 'CASCADE', hooks: true })
|
||||
@BelongsTo(() => Note, {foreignKey: 'noteId', constraints: false, onDelete: 'CASCADE', hooks: true})
|
||||
note: Note;
|
||||
|
||||
getNoteRevisions (note, callback) {
|
||||
getNoteRevisions(note, callback) {
|
||||
Revision.findAll({
|
||||
where: {
|
||||
noteId: note.id
|
||||
},
|
||||
order: [['createdAt', 'DESC']]
|
||||
}).then(function (revisions) {
|
||||
var data : any[] = []
|
||||
var data: any[] = []
|
||||
for (var i = 0; i < revisions.length; i++) {
|
||||
var revision = revisions[i]
|
||||
data.push({
|
||||
time : moment(revision.createdAt).valueOf(),
|
||||
time: moment(revision.createdAt).valueOf(),
|
||||
length: revision.length
|
||||
})
|
||||
}
|
||||
|
@ -120,7 +125,8 @@ export class Revision extends Model<Revision> {
|
|||
callback(err, null)
|
||||
})
|
||||
}
|
||||
getPatchedNoteRevisionByTime (note, time, callback) {
|
||||
|
||||
getPatchedNoteRevisionByTime(note, time, callback) {
|
||||
// find all revisions to prepare for all possible calculation
|
||||
Revision.findAll({
|
||||
where: {
|
||||
|
@ -151,17 +157,19 @@ export class Revision extends Model<Revision> {
|
|||
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 (!notes || notes.length <= 0) {
|
||||
return callback(null, notes)
|
||||
} else {
|
||||
this.checkAllNotesRevision(callback)
|
||||
Revision.checkAllNotesRevision(callback)
|
||||
}
|
||||
})
|
||||
}
|
||||
static saveAllNotesRevision (callback) {
|
||||
|
||||
static saveAllNotesRevision(callback) {
|
||||
Note.findAll({
|
||||
// query all notes that need to save for revision
|
||||
where: {
|
||||
|
@ -189,24 +197,24 @@ export class Revision extends Model<Revision> {
|
|||
}
|
||||
}).then(function (notes) {
|
||||
if (notes.length <= 0) return callback(null, notes)
|
||||
var savedNotes : any[] = []
|
||||
async.each(notes, function (note : any, _callback) {
|
||||
var savedNotes: any[] = []
|
||||
async.each(notes, function (note: any, _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)
|
||||
this.saveNoteRevision(note, _callback)
|
||||
Revision.saveNoteRevision(note, _callback)
|
||||
} else if (lastchangeAt.isAfter(savedAt.add(10, 'minutes'))) {
|
||||
savedNotes.push(note)
|
||||
this.saveNoteRevision(note, _callback)
|
||||
Revision.saveNoteRevision(note, _callback)
|
||||
} else {
|
||||
return _callback(null, null)
|
||||
}
|
||||
} else {
|
||||
savedNotes.push(note)
|
||||
this.saveNoteRevision(note, _callback)
|
||||
Revision.saveNoteRevision(note, _callback)
|
||||
}
|
||||
}, function (err) {
|
||||
if (err) {
|
||||
|
@ -220,7 +228,8 @@ export class Revision extends Model<Revision> {
|
|||
return callback(err, null)
|
||||
})
|
||||
}
|
||||
saveNoteRevision (note, callback) {
|
||||
|
||||
static saveNoteRevision(note, callback) {
|
||||
Revision.findAll({
|
||||
where: {
|
||||
noteId: note.id
|
||||
|
@ -229,13 +238,13 @@ export class Revision extends Model<Revision> {
|
|||
}).then(function (revisions) {
|
||||
if (revisions.length <= 0) {
|
||||
// if no revision available
|
||||
this.create({
|
||||
Revision.create({
|
||||
noteId: note.id,
|
||||
lastContent: note.content ? note.content : '',
|
||||
length: note.content ? note.content.length : 0,
|
||||
authorship: note.authorship
|
||||
}).then(function (revision) {
|
||||
this.finishSaveNoteRevision(note, revision, callback)
|
||||
Revision.finishSaveNoteRevision(note, revision, callback)
|
||||
}).catch(function (err) {
|
||||
return callback(err, null)
|
||||
})
|
||||
|
@ -255,12 +264,12 @@ export class Revision extends Model<Revision> {
|
|||
latestRevision.update({
|
||||
updatedAt: Date.now()
|
||||
}).then(function (revision) {
|
||||
this.finishSaveNoteRevision(note, revision, callback)
|
||||
Revision.finishSaveNoteRevision(note, revision, callback)
|
||||
}).catch(function (err) {
|
||||
return callback(err, null)
|
||||
})
|
||||
} else {
|
||||
this.create({
|
||||
Revision.create({
|
||||
noteId: note.id,
|
||||
patch: patch,
|
||||
content: note.content,
|
||||
|
@ -271,7 +280,7 @@ export class Revision extends Model<Revision> {
|
|||
latestRevision.update({
|
||||
content: null
|
||||
}).then(function () {
|
||||
this.finishSaveNoteRevision(note, revision, callback)
|
||||
Revision.finishSaveNoteRevision(note, revision, callback)
|
||||
}).catch(function (err) {
|
||||
return callback(err, null)
|
||||
})
|
||||
|
@ -285,7 +294,8 @@ export class Revision extends Model<Revision> {
|
|||
return callback(err, null)
|
||||
})
|
||||
}
|
||||
finishSaveNoteRevision (note, revision, callback) {
|
||||
|
||||
static finishSaveNoteRevision(note, revision, callback) {
|
||||
note.update({
|
||||
savedAt: revision.updatedAt
|
||||
}).then(function () {
|
||||
|
@ -296,6 +306,5 @@ export class Revision extends Model<Revision> {
|
|||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue