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'
|
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";
|
||||||
|
|
||||||
|
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 dmpWorker: ChildProcess | null = createDmpWorker()
|
||||||
var dmpCallbackCache = {}
|
var dmpCallbackCache = {}
|
||||||
|
@ -60,26 +61,29 @@ export class Revision extends Model<Revision> {
|
||||||
|
|
||||||
@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) {
|
set lastContent(value: string) {
|
||||||
this.setDataValue('lastContent', Sequelize.stripNullByte(value))
|
this.setDataValue('lastContent', Utils.stripNullByte(value))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Column(DataType.TEXT({length: 'long'}))
|
@Column(DataType.TEXT({length: 'long'}))
|
||||||
get content(): string {
|
get content(): string {
|
||||||
return Sequelize.processData(this.getDataValue('content'), '')
|
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)
|
||||||
|
@ -87,8 +91,9 @@ export class Revision extends Model<Revision> {
|
||||||
|
|
||||||
@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)
|
||||||
}
|
}
|
||||||
|
@ -120,6 +125,7 @@ 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({
|
||||||
|
@ -151,16 +157,18 @@ export class Revision extends Model<Revision> {
|
||||||
return callback(err, null)
|
return callback(err, null)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
static checkAllNotesRevision(callback) {
|
static checkAllNotesRevision(callback) {
|
||||||
this.saveAllNotesRevision(function (err, notes) {
|
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
|
||||||
|
@ -197,16 +205,16 @@ export class Revision extends Model<Revision> {
|
||||||
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…
Add table
Add a link
Reference in a new issue