mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-19 01:35:18 -04: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,17 +1,18 @@
|
|||
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 {Utils} from "../utils";
|
||||
|
||||
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 = {}
|
||||
|
@ -60,26 +61,29 @@ export class Revision extends Model<Revision> {
|
|||
|
||||
@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'}))
|
||||
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))
|
||||
this.setDataValue('lastContent', Utils.stripNullByte(value))
|
||||
}
|
||||
|
||||
@Column(DataType.TEXT({length: 'long'}))
|
||||
get content(): string {
|
||||
return Sequelize.processData(this.getDataValue('content'), '')
|
||||
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)
|
||||
|
@ -87,8 +91,9 @@ export class Revision extends Model<Revision> {
|
|||
|
||||
@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)
|
||||
}
|
||||
|
@ -120,6 +125,7 @@ export class Revision extends Model<Revision> {
|
|||
callback(err, null)
|
||||
})
|
||||
}
|
||||
|
||||
getPatchedNoteRevisionByTime(note, time, callback) {
|
||||
// find all revisions to prepare for all possible calculation
|
||||
Revision.findAll({
|
||||
|
@ -151,16 +157,18 @@ export class Revision extends Model<Revision> {
|
|||
return callback(err, null)
|
||||
})
|
||||
}
|
||||
|
||||
static checkAllNotesRevision(callback) {
|
||||
this.saveAllNotesRevision(function (err, notes) {
|
||||
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) {
|
||||
Note.findAll({
|
||||
// query all notes that need to save for revision
|
||||
|
@ -197,16 +205,16 @@ export class Revision extends Model<Revision> {
|
|||
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…
Add table
Add a link
Reference in a new issue