mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-19 01:35:18 -04:00
note.ts: Formatting, use Utils instead of global Sequelize object
Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
parent
522d92a9a1
commit
477a37b48c
1 changed files with 109 additions and 42 deletions
|
@ -1,11 +1,25 @@
|
||||||
import { AllowNull, AfterCreate, BeforeCreate, Default, Unique, IsUUID, Model, PrimaryKey, Column, DataType, Table, BelongsTo, HasMany, ForeignKey } from "sequelize-typescript";
|
import {
|
||||||
import { generate as shortIdGenerate, isValid as shortIdIsValid } from "shortid";
|
AfterCreate,
|
||||||
import { Author } from "./author";
|
AllowNull,
|
||||||
import { User } from './user';
|
BeforeCreate,
|
||||||
import { Revision } from './revision';
|
BelongsTo,
|
||||||
|
Column,
|
||||||
|
DataType,
|
||||||
|
Default,
|
||||||
|
ForeignKey,
|
||||||
|
HasMany,
|
||||||
|
Model,
|
||||||
|
PrimaryKey,
|
||||||
|
Table,
|
||||||
|
Unique
|
||||||
|
} from "sequelize-typescript";
|
||||||
|
import {generate as shortIdGenerate, isValid as shortIdIsValid} from "shortid";
|
||||||
|
import {Author} from "./author";
|
||||||
|
import {User} from './user';
|
||||||
|
import {Revision} from './revision';
|
||||||
|
import {Utils} from "../utils";
|
||||||
|
import Sequelize from "sequelize";
|
||||||
|
|
||||||
// external modules
|
|
||||||
// external modules
|
|
||||||
var fs = require('fs')
|
var fs = require('fs')
|
||||||
var path = require('path')
|
var path = require('path')
|
||||||
var LZString = require('lz-string')
|
var LZString = require('lz-string')
|
||||||
|
@ -18,7 +32,6 @@ var base64url = require('base64url')
|
||||||
var md = require('markdown-it')()
|
var md = require('markdown-it')()
|
||||||
var metaMarked = require('meta-marked')
|
var metaMarked = require('meta-marked')
|
||||||
var cheerio = require('cheerio')
|
var cheerio = require('cheerio')
|
||||||
var Sequelize = require('sequelize')
|
|
||||||
var async = require('async')
|
var async = require('async')
|
||||||
var moment = require('moment')
|
var moment = require('moment')
|
||||||
var DiffMatchPatch = require('diff-match-patch')
|
var DiffMatchPatch = require('diff-match-patch')
|
||||||
|
@ -42,7 +55,7 @@ enum PermissionEnum {
|
||||||
private = "private"
|
private = "private"
|
||||||
};
|
};
|
||||||
|
|
||||||
@Table({ paranoid: false })
|
@Table({paranoid: false})
|
||||||
export class Note extends Model<Note> {
|
export class Note extends Model<Note> {
|
||||||
@PrimaryKey
|
@PrimaryKey
|
||||||
@Default(Sequelize.UUIDV4)
|
@Default(Sequelize.UUIDV4)
|
||||||
|
@ -69,24 +82,27 @@ export class Note extends Model<Note> {
|
||||||
|
|
||||||
@Column(DataType.TEXT)
|
@Column(DataType.TEXT)
|
||||||
get title(): string {
|
get title(): string {
|
||||||
return Sequelize.processData(this.getDataValue('title'), '')
|
return Utils.processData(this.getDataValue('title'), '')
|
||||||
}
|
}
|
||||||
|
|
||||||
set title(value: string) {
|
set title(value: string) {
|
||||||
this.setDataValue('title', Sequelize.stripNullByte(value))
|
this.setDataValue('title', 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) {
|
|
||||||
this.setDataValue('content', Sequelize.stripNullByte(value))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Column(DataType.TEXT({ length: 'long' }))
|
set content(value: string) {
|
||||||
get authorship(): string {
|
this.setDataValue('content', Utils.stripNullByte(value))
|
||||||
return Sequelize.processData(this.getDataValue('authorship'), [], JSON.parse)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Column(DataType.TEXT({length: 'long'}))
|
||||||
|
get authorship(): string {
|
||||||
|
return Utils.processData(this.getDataValue('authorship'), [], JSON.parse)
|
||||||
|
}
|
||||||
|
|
||||||
set authorship(value: string) {
|
set authorship(value: string) {
|
||||||
this.setDataValue('authorship', JSON.stringify(value))
|
this.setDataValue('authorship', JSON.stringify(value))
|
||||||
}
|
}
|
||||||
|
@ -103,20 +119,20 @@ export class Note extends Model<Note> {
|
||||||
@Column
|
@Column
|
||||||
ownerId: string;
|
ownerId: string;
|
||||||
|
|
||||||
@BelongsTo(() => User, { foreignKey: 'ownerId', constraints: false, onDelete: 'CASCADE', hooks: true })
|
@BelongsTo(() => User, {foreignKey: 'ownerId', constraints: false, onDelete: 'CASCADE', hooks: true})
|
||||||
owner: User;
|
owner: User;
|
||||||
|
|
||||||
@ForeignKey(() => User)
|
@ForeignKey(() => User)
|
||||||
@Column
|
@Column
|
||||||
lastchangeuserId: string;
|
lastchangeuserId: string;
|
||||||
|
|
||||||
@BelongsTo(() => User, { foreignKey: 'lastchangeuserId', constraints: false })
|
@BelongsTo(() => User, {foreignKey: 'lastchangeuserId', constraints: false})
|
||||||
lastchangeuser: User;
|
lastchangeuser: User;
|
||||||
|
|
||||||
@HasMany(() => Revision, { foreignKey: 'noteId', constraints: false })
|
@HasMany(() => Revision, {foreignKey: 'noteId', constraints: false})
|
||||||
revisions: Revision[];
|
revisions: Revision[];
|
||||||
|
|
||||||
@HasMany(() => Author, { foreignKey: 'noteId', constraints: false })
|
@HasMany(() => Author, {foreignKey: 'noteId', constraints: false})
|
||||||
authors: Author[];
|
authors: Author[];
|
||||||
|
|
||||||
@BeforeCreate
|
@BeforeCreate
|
||||||
|
@ -155,7 +171,7 @@ export class Note extends Model<Note> {
|
||||||
@AfterCreate
|
@AfterCreate
|
||||||
static saveRevision(note) {
|
static saveRevision(note) {
|
||||||
return new Promise(function (resolve, reject) {
|
return new Promise(function (resolve, reject) {
|
||||||
Sequelize.models.Revision.saveNoteRevision(note, function (err, revision) {
|
Revision.saveNoteRevision(note, function (err, revision) {
|
||||||
if (err) {
|
if (err) {
|
||||||
return reject(err)
|
return reject(err)
|
||||||
}
|
}
|
||||||
|
@ -191,11 +207,17 @@ export class Note extends Model<Note> {
|
||||||
idParts.push(id.substr(20, 12))
|
idParts.push(id.substr(20, 12))
|
||||||
return idParts.join('-')
|
return idParts.join('-')
|
||||||
}
|
}
|
||||||
|
|
||||||
static checkNoteIdValid(id) {
|
static checkNoteIdValid(id) {
|
||||||
var uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
var uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
|
||||||
var result = id.match(uuidRegex)
|
var result = id.match(uuidRegex)
|
||||||
if (result && result.length === 1) { return true } else { return false }
|
if (result && result.length === 1) {
|
||||||
|
return true
|
||||||
|
} else {
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static parseNoteId(noteId, callback) {
|
static parseNoteId(noteId, callback) {
|
||||||
async.series({
|
async.series({
|
||||||
parseNoteIdByAlias: function (_callback) {
|
parseNoteIdByAlias: function (_callback) {
|
||||||
|
@ -221,7 +243,7 @@ export class Note extends Model<Note> {
|
||||||
content: body,
|
content: body,
|
||||||
lastchangeAt: fsModifiedTime
|
lastchangeAt: fsModifiedTime
|
||||||
}).then(function (note) {
|
}).then(function (note) {
|
||||||
Sequelize.models.Revision.saveNoteRevision(note, function (err, revision) {
|
Revision.saveNoteRevision(note, function (err, revision) {
|
||||||
if (err) return _callback(err, null)
|
if (err) return _callback(err, null)
|
||||||
// update authorship on after making revision of docs
|
// update authorship on after making revision of docs
|
||||||
var patch = dmp.patch_fromText(revision.patch)
|
var patch = dmp.patch_fromText(revision.patch)
|
||||||
|
@ -281,7 +303,11 @@ export class Note extends Model<Note> {
|
||||||
// try to parse note id by LZString Base64
|
// try to parse note id by LZString Base64
|
||||||
try {
|
try {
|
||||||
var id = LZString.decompressFromBase64(noteId)
|
var id = LZString.decompressFromBase64(noteId)
|
||||||
if (id && Note.checkNoteIdValid(id)) { return callback(null, id) } else { return _callback(null, null) }
|
if (id && Note.checkNoteIdValid(id)) {
|
||||||
|
return callback(null, id)
|
||||||
|
} else {
|
||||||
|
return _callback(null, null)
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
if (err.message === 'Cannot read property \'charAt\' of undefined') {
|
if (err.message === 'Cannot read property \'charAt\' of undefined') {
|
||||||
logger.warning('Looks like we can not decode "' + noteId + '" with LZString. Can be ignored.')
|
logger.warning('Looks like we can not decode "' + noteId + '" with LZString. Can be ignored.')
|
||||||
|
@ -295,7 +321,11 @@ export class Note extends Model<Note> {
|
||||||
// try to parse note id by base64url
|
// try to parse note id by base64url
|
||||||
try {
|
try {
|
||||||
var id = Note.decodeNoteId(noteId)
|
var id = Note.decodeNoteId(noteId)
|
||||||
if (id && Note.checkNoteIdValid(id)) { return callback(null, id) } else { return _callback(null, null) }
|
if (id && Note.checkNoteIdValid(id)) {
|
||||||
|
return callback(null, id)
|
||||||
|
} else {
|
||||||
|
return _callback(null, null)
|
||||||
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
logger.error(err)
|
logger.error(err)
|
||||||
return _callback(null, null)
|
return _callback(null, null)
|
||||||
|
@ -330,6 +360,7 @@ export class Note extends Model<Note> {
|
||||||
return callback(null, null)
|
return callback(null, null)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
parseNoteInfo(body) {
|
parseNoteInfo(body) {
|
||||||
var parsed = Note.extractMeta(body)
|
var parsed = Note.extractMeta(body)
|
||||||
var $ = cheerio.load(md.render(parsed.markdown))
|
var $ = cheerio.load(md.render(parsed.markdown))
|
||||||
|
@ -338,32 +369,40 @@ export class Note extends Model<Note> {
|
||||||
tags: Note.extractNoteTags(parsed.meta, $)
|
tags: Note.extractNoteTags(parsed.meta, $)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static parseNoteTitle(body) {
|
static parseNoteTitle(body) {
|
||||||
const parsed = Note.extractMeta(body)
|
const parsed = Note.extractMeta(body)
|
||||||
var $ = cheerio.load(md.render(parsed.markdown))
|
var $ = cheerio.load(md.render(parsed.markdown))
|
||||||
return Note.extractNoteTitle(parsed.meta, $)
|
return Note.extractNoteTitle(parsed.meta, $)
|
||||||
}
|
}
|
||||||
|
|
||||||
static extractNoteTitle(meta, $) {
|
static extractNoteTitle(meta, $) {
|
||||||
var title = ''
|
var title = ''
|
||||||
if (meta.title && (typeof meta.title === 'string' || typeof meta.title === 'number')) {
|
if (meta.title && (typeof meta.title === 'string' || typeof meta.title === 'number')) {
|
||||||
title = meta.title
|
title = meta.title
|
||||||
} else {
|
} else {
|
||||||
var h1s = $('h1')
|
var h1s = $('h1')
|
||||||
if (h1s.length > 0 && h1s.first().text().split('\n').length === 1) { title = S(h1s.first().text()).stripTags().s }
|
if (h1s.length > 0 && h1s.first().text().split('\n').length === 1) {
|
||||||
|
title = S(h1s.first().text()).stripTags().s
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (!title) title = 'Untitled'
|
if (!title) title = 'Untitled'
|
||||||
return title
|
return title
|
||||||
}
|
}
|
||||||
|
|
||||||
static generateDescription(markdown) {
|
static generateDescription(markdown) {
|
||||||
return markdown.substr(0, 100).replace(/(?:\r\n|\r|\n)/g, ' ')
|
return markdown.substr(0, 100).replace(/(?:\r\n|\r|\n)/g, ' ')
|
||||||
}
|
}
|
||||||
|
|
||||||
static decodeTitle(title) {
|
static decodeTitle(title) {
|
||||||
return title || 'Untitled'
|
return title || 'Untitled'
|
||||||
}
|
}
|
||||||
|
|
||||||
static generateWebTitle(title) {
|
static generateWebTitle(title) {
|
||||||
title = !title || title === 'Untitled' ? 'CodiMD - Collaborative markdown notes' : title + ' - CodiMD'
|
title = !title || title === 'Untitled' ? 'CodiMD - Collaborative markdown notes' : title + ' - CodiMD'
|
||||||
return title
|
return title
|
||||||
}
|
}
|
||||||
|
|
||||||
static extractNoteTags(meta, $) {
|
static extractNoteTags(meta, $) {
|
||||||
var tags: string[] = []
|
var tags: string[] = []
|
||||||
var rawtags: string[] = []
|
var rawtags: string[] = []
|
||||||
|
@ -393,7 +432,9 @@ export class Note extends Model<Note> {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!found) { tags.push(rawtags[i]) }
|
if (!found) {
|
||||||
|
tags.push(rawtags[i])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return tags
|
return tags
|
||||||
}
|
}
|
||||||
|
@ -411,27 +452,52 @@ export class Note extends Model<Note> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static parseMeta(meta): NoteMetadata {
|
static parseMeta(meta): NoteMetadata {
|
||||||
var _meta = new NoteMetadata();
|
var _meta = new NoteMetadata();
|
||||||
if (meta) {
|
if (meta) {
|
||||||
if (meta.title && (typeof meta.title === 'string' || typeof meta.title === 'number')) { _meta.title = meta.title }
|
if (meta.title && (typeof meta.title === 'string' || typeof meta.title === 'number')) {
|
||||||
if (meta.description && (typeof meta.description === 'string' || typeof meta.description === 'number')) { _meta.description = meta.description }
|
_meta.title = meta.title
|
||||||
if (meta.robots && (typeof meta.robots === 'string' || typeof meta.robots === 'number')) { _meta.robots = meta.robots }
|
}
|
||||||
if (meta.GA && (typeof meta.GA === 'string' || typeof meta.GA === 'number')) { _meta.GA = meta.GA }
|
if (meta.description && (typeof meta.description === 'string' || typeof meta.description === 'number')) {
|
||||||
if (meta.disqus && (typeof meta.disqus === 'string' || typeof meta.disqus === 'number')) { _meta.disqus = meta.disqus }
|
_meta.description = meta.description
|
||||||
if (meta.slideOptions && (typeof meta.slideOptions === 'object')) { _meta.slideOptions = meta.slideOptions }
|
}
|
||||||
if (meta.opengraph && (typeof meta.opengraph === 'object')) { _meta.opengraph = meta.opengraph }
|
if (meta.robots && (typeof meta.robots === 'string' || typeof meta.robots === 'number')) {
|
||||||
|
_meta.robots = meta.robots
|
||||||
|
}
|
||||||
|
if (meta.GA && (typeof meta.GA === 'string' || typeof meta.GA === 'number')) {
|
||||||
|
_meta.GA = meta.GA
|
||||||
|
}
|
||||||
|
if (meta.disqus && (typeof meta.disqus === 'string' || typeof meta.disqus === 'number')) {
|
||||||
|
_meta.disqus = meta.disqus
|
||||||
|
}
|
||||||
|
if (meta.slideOptions && (typeof meta.slideOptions === 'object')) {
|
||||||
|
_meta.slideOptions = meta.slideOptions
|
||||||
|
}
|
||||||
|
if (meta.opengraph && (typeof meta.opengraph === 'object')) {
|
||||||
|
_meta.opengraph = meta.opengraph
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return _meta
|
return _meta
|
||||||
}
|
}
|
||||||
|
|
||||||
static parseOpengraph(meta, title) {
|
static parseOpengraph(meta, title) {
|
||||||
var _ogdata: any = {}
|
var _ogdata: any = {}
|
||||||
if (meta.opengraph) { _ogdata = meta.opengraph }
|
if (meta.opengraph) {
|
||||||
if (!(_ogdata.title && (typeof _ogdata.title === 'string' || typeof _ogdata.title === 'number'))) { _ogdata.title = title }
|
_ogdata = meta.opengraph
|
||||||
if (!(_ogdata.description && (typeof _ogdata.description === 'string' || typeof _ogdata.description === 'number'))) { _ogdata.description = meta.description || '' }
|
}
|
||||||
if (!(_ogdata.type && (typeof _ogdata.type === 'string'))) { _ogdata.type = 'website' }
|
if (!(_ogdata.title && (typeof _ogdata.title === 'string' || typeof _ogdata.title === 'number'))) {
|
||||||
|
_ogdata.title = title
|
||||||
|
}
|
||||||
|
if (!(_ogdata.description && (typeof _ogdata.description === 'string' || typeof _ogdata.description === 'number'))) {
|
||||||
|
_ogdata.description = meta.description || ''
|
||||||
|
}
|
||||||
|
if (!(_ogdata.type && (typeof _ogdata.type === 'string'))) {
|
||||||
|
_ogdata.type = 'website'
|
||||||
|
}
|
||||||
return _ogdata
|
return _ogdata
|
||||||
}
|
}
|
||||||
|
|
||||||
static updateAuthorshipByOperation(operation, userId, authorships) {
|
static updateAuthorshipByOperation(operation, userId, authorships) {
|
||||||
var index = 0
|
var index = 0
|
||||||
var timestamp = Date.now()
|
var timestamp = Date.now()
|
||||||
|
@ -533,6 +599,7 @@ export class Note extends Model<Note> {
|
||||||
}
|
}
|
||||||
return authorships
|
return authorships
|
||||||
}
|
}
|
||||||
|
|
||||||
static transformPatchToOperations(patch, contentLength) {
|
static transformPatchToOperations(patch, contentLength) {
|
||||||
var operations: any = []
|
var operations: any = []
|
||||||
if (patch.length > 0) {
|
if (patch.length > 0) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue