2018-05-20 22:50:51 +00:00
|
|
|
const Model = require('objection').Model
|
2018-08-19 05:22:59 +00:00
|
|
|
const _ = require('lodash')
|
2018-09-16 00:58:23 +00:00
|
|
|
const JSBinType = require('js-binary').Type
|
|
|
|
const pageHelper = require('../helpers/page')
|
|
|
|
const path = require('path')
|
|
|
|
const fs = require('fs-extra')
|
2018-05-20 22:50:51 +00:00
|
|
|
|
2018-07-22 20:25:39 +00:00
|
|
|
/* global WIKI */
|
|
|
|
|
2018-05-20 22:50:51 +00:00
|
|
|
/**
|
|
|
|
* Pages model
|
|
|
|
*/
|
|
|
|
module.exports = class Page extends Model {
|
|
|
|
static get tableName() { return 'pages' }
|
|
|
|
|
|
|
|
static get jsonSchema () {
|
|
|
|
return {
|
|
|
|
type: 'object',
|
|
|
|
required: ['path', 'title'],
|
|
|
|
|
|
|
|
properties: {
|
|
|
|
id: {type: 'integer'},
|
|
|
|
path: {type: 'string'},
|
2018-09-16 00:58:23 +00:00
|
|
|
hash: {type: 'string'},
|
2018-05-20 22:50:51 +00:00
|
|
|
title: {type: 'string'},
|
|
|
|
description: {type: 'string'},
|
|
|
|
isPublished: {type: 'boolean'},
|
2018-08-19 05:22:59 +00:00
|
|
|
privateNS: {type: 'string'},
|
2018-05-20 22:50:51 +00:00
|
|
|
publishStartDate: {type: 'string'},
|
|
|
|
publishEndDate: {type: 'string'},
|
|
|
|
content: {type: 'string'},
|
2018-07-30 02:23:33 +00:00
|
|
|
contentType: {type: 'string'},
|
2018-05-20 22:50:51 +00:00
|
|
|
|
|
|
|
createdAt: {type: 'string'},
|
|
|
|
updatedAt: {type: 'string'}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static get relationMappings() {
|
|
|
|
return {
|
|
|
|
tags: {
|
|
|
|
relation: Model.ManyToManyRelation,
|
|
|
|
modelClass: require('./tags'),
|
|
|
|
join: {
|
|
|
|
from: 'pages.id',
|
|
|
|
through: {
|
|
|
|
from: 'pageTags.pageId',
|
|
|
|
to: 'pageTags.tagId'
|
|
|
|
},
|
|
|
|
to: 'tags.id'
|
|
|
|
}
|
|
|
|
},
|
|
|
|
author: {
|
|
|
|
relation: Model.BelongsToOneRelation,
|
|
|
|
modelClass: require('./users'),
|
|
|
|
join: {
|
|
|
|
from: 'pages.authorId',
|
|
|
|
to: 'users.id'
|
|
|
|
}
|
|
|
|
},
|
2018-07-23 01:13:01 +00:00
|
|
|
creator: {
|
|
|
|
relation: Model.BelongsToOneRelation,
|
|
|
|
modelClass: require('./users'),
|
|
|
|
join: {
|
|
|
|
from: 'pages.creatorId',
|
|
|
|
to: 'users.id'
|
|
|
|
}
|
|
|
|
},
|
2018-07-22 04:29:39 +00:00
|
|
|
editor: {
|
|
|
|
relation: Model.BelongsToOneRelation,
|
|
|
|
modelClass: require('./editors'),
|
|
|
|
join: {
|
|
|
|
from: 'pages.editorKey',
|
|
|
|
to: 'editors.key'
|
|
|
|
}
|
|
|
|
},
|
2018-05-20 22:50:51 +00:00
|
|
|
locale: {
|
|
|
|
relation: Model.BelongsToOneRelation,
|
|
|
|
modelClass: require('./locales'),
|
|
|
|
join: {
|
2018-07-22 04:29:39 +00:00
|
|
|
from: 'pages.localeCode',
|
2018-05-20 22:50:51 +00:00
|
|
|
to: 'locales.code'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$beforeUpdate() {
|
|
|
|
this.updatedAt = new Date().toISOString()
|
|
|
|
}
|
|
|
|
$beforeInsert() {
|
|
|
|
this.createdAt = new Date().toISOString()
|
|
|
|
this.updatedAt = new Date().toISOString()
|
|
|
|
}
|
2018-07-22 20:25:39 +00:00
|
|
|
|
2018-09-16 00:58:23 +00:00
|
|
|
static get cacheSchema() {
|
|
|
|
return new JSBinType({
|
2019-01-26 23:35:56 +00:00
|
|
|
id: 'uint',
|
2018-09-16 00:58:23 +00:00
|
|
|
authorId: 'uint',
|
|
|
|
authorName: 'string',
|
|
|
|
createdAt: 'string',
|
|
|
|
creatorId: 'uint',
|
|
|
|
creatorName: 'string',
|
|
|
|
description: 'string',
|
|
|
|
isPrivate: 'boolean',
|
|
|
|
isPublished: 'boolean',
|
|
|
|
publishEndDate: 'string',
|
|
|
|
publishStartDate: 'string',
|
|
|
|
render: 'string',
|
|
|
|
title: 'string',
|
2018-11-18 04:03:58 +00:00
|
|
|
toc: 'string',
|
2018-09-16 00:58:23 +00:00
|
|
|
updatedAt: 'string'
|
|
|
|
})
|
2018-08-19 05:22:59 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 23:06:03 +00:00
|
|
|
/**
|
|
|
|
* Inject page metadata into contents
|
|
|
|
*/
|
|
|
|
injectMetadata () {
|
|
|
|
let meta = [
|
|
|
|
['title', this.title],
|
|
|
|
['description', this.description],
|
|
|
|
['published', this.isPublished.toString()],
|
|
|
|
['date', this.updatedAt],
|
|
|
|
['tags', '']
|
|
|
|
]
|
|
|
|
switch (this.contentType) {
|
|
|
|
case 'markdown':
|
|
|
|
return '---\n' + meta.map(mt => `${mt[0]}: ${mt[1]}`).join('\n') + '\n---\n\n' + this.content
|
|
|
|
case 'html':
|
|
|
|
return '<!--\n' + meta.map(mt => `${mt[0]}: ${mt[1]}`).join('\n') + '\n-->\n\n' + this.content
|
|
|
|
default:
|
|
|
|
return this.content
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 20:25:39 +00:00
|
|
|
static async createPage(opts) {
|
2018-09-16 00:58:23 +00:00
|
|
|
await WIKI.models.pages.query().insert({
|
2018-07-22 20:25:39 +00:00
|
|
|
authorId: opts.authorId,
|
|
|
|
content: opts.content,
|
2018-07-23 01:13:01 +00:00
|
|
|
creatorId: opts.authorId,
|
2018-08-20 05:02:57 +00:00
|
|
|
contentType: _.get(_.find(WIKI.data.editors, ['key', opts.editor]), `contentType`, 'text'),
|
2018-07-22 20:25:39 +00:00
|
|
|
description: opts.description,
|
|
|
|
editorKey: opts.editor,
|
2018-09-16 00:58:23 +00:00
|
|
|
hash: pageHelper.generateHash({ path: opts.path, locale: opts.locale, privateNS: opts.isPrivate ? 'TODO' : '' }),
|
2018-07-22 20:25:39 +00:00
|
|
|
isPrivate: opts.isPrivate,
|
|
|
|
isPublished: opts.isPublished,
|
|
|
|
localeCode: opts.locale,
|
|
|
|
path: opts.path,
|
2018-11-18 04:03:58 +00:00
|
|
|
publishEndDate: opts.publishEndDate || '',
|
|
|
|
publishStartDate: opts.publishStartDate || '',
|
|
|
|
title: opts.title,
|
|
|
|
toc: '[]'
|
2018-07-22 20:25:39 +00:00
|
|
|
})
|
2018-09-16 04:35:03 +00:00
|
|
|
const page = await WIKI.models.pages.getPageFromDb({
|
|
|
|
path: opts.path,
|
|
|
|
locale: opts.locale,
|
|
|
|
userId: opts.authorId,
|
|
|
|
isPrivate: opts.isPrivate
|
|
|
|
})
|
2018-09-10 00:33:10 +00:00
|
|
|
await WIKI.models.pages.renderPage(page)
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.storage.pageEvent({
|
2018-07-23 01:13:01 +00:00
|
|
|
event: 'created',
|
|
|
|
page
|
|
|
|
})
|
|
|
|
return page
|
|
|
|
}
|
|
|
|
|
|
|
|
static async updatePage(opts) {
|
2018-07-30 02:23:33 +00:00
|
|
|
const ogPage = await WIKI.models.pages.query().findById(opts.id)
|
2018-07-23 01:13:01 +00:00
|
|
|
if (!ogPage) {
|
|
|
|
throw new Error('Invalid Page Id')
|
|
|
|
}
|
2019-01-26 23:35:56 +00:00
|
|
|
await WIKI.models.pageHistory.addVersion({
|
|
|
|
...ogPage,
|
2019-02-22 22:05:18 +00:00
|
|
|
isPublished: ogPage.isPublished === true || ogPage.isPublished === 1,
|
2019-01-26 23:35:56 +00:00
|
|
|
action: 'updated'
|
|
|
|
})
|
2018-09-16 00:58:23 +00:00
|
|
|
await WIKI.models.pages.query().patch({
|
2018-07-30 02:23:33 +00:00
|
|
|
authorId: opts.authorId,
|
|
|
|
content: opts.content,
|
|
|
|
description: opts.description,
|
|
|
|
isPublished: opts.isPublished,
|
2018-11-18 04:03:58 +00:00
|
|
|
publishEndDate: opts.publishEndDate || '',
|
|
|
|
publishStartDate: opts.publishStartDate || '',
|
2018-07-23 01:13:01 +00:00
|
|
|
title: opts.title
|
2018-09-16 00:58:23 +00:00
|
|
|
}).where('id', ogPage.id)
|
2018-09-16 04:35:03 +00:00
|
|
|
const page = await WIKI.models.pages.getPageFromDb({
|
|
|
|
path: ogPage.path,
|
|
|
|
locale: ogPage.localeCode,
|
|
|
|
userId: ogPage.authorId,
|
|
|
|
isPrivate: ogPage.isPrivate
|
|
|
|
})
|
2018-09-10 00:33:10 +00:00
|
|
|
await WIKI.models.pages.renderPage(page)
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.storage.pageEvent({
|
2018-07-23 01:13:01 +00:00
|
|
|
event: 'updated',
|
|
|
|
page
|
|
|
|
})
|
2018-07-22 20:25:39 +00:00
|
|
|
return page
|
|
|
|
}
|
2018-08-09 04:07:15 +00:00
|
|
|
|
2019-01-26 23:35:56 +00:00
|
|
|
static async deletePage(opts) {
|
|
|
|
const page = await WIKI.models.pages.query().findById(opts.id)
|
|
|
|
if (!page) {
|
|
|
|
throw new Error('Invalid Page Id')
|
|
|
|
}
|
|
|
|
await WIKI.models.pageHistory.addVersion({
|
|
|
|
...page,
|
|
|
|
action: 'deleted'
|
|
|
|
})
|
|
|
|
await WIKI.models.pages.query().delete().where('id', page.id)
|
|
|
|
await WIKI.models.pages.deletePageFromCache(page)
|
|
|
|
await WIKI.models.storage.pageEvent({
|
|
|
|
event: 'deleted',
|
|
|
|
page
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2018-09-10 00:33:10 +00:00
|
|
|
static async renderPage(page) {
|
2019-02-13 22:20:46 +00:00
|
|
|
const renderJob = await WIKI.scheduler.registerJob({
|
|
|
|
name: 'render-page',
|
|
|
|
immediate: true,
|
|
|
|
worker: true
|
|
|
|
}, page.id)
|
|
|
|
return renderJob.finished
|
2018-08-09 04:07:15 +00:00
|
|
|
}
|
2018-09-16 00:58:23 +00:00
|
|
|
|
|
|
|
static async getPage(opts) {
|
|
|
|
let page = await WIKI.models.pages.getPageFromCache(opts)
|
|
|
|
if (!page) {
|
|
|
|
page = await WIKI.models.pages.getPageFromDb(opts)
|
2018-09-16 22:36:15 +00:00
|
|
|
if (page) {
|
|
|
|
await WIKI.models.pages.savePageToCache(page)
|
|
|
|
}
|
2018-09-16 00:58:23 +00:00
|
|
|
}
|
|
|
|
return page
|
|
|
|
}
|
|
|
|
|
|
|
|
static async getPageFromDb(opts) {
|
2019-02-13 22:20:46 +00:00
|
|
|
const queryModeID = _.isNumber(opts)
|
2018-11-25 06:28:20 +00:00
|
|
|
return WIKI.models.pages.query()
|
2018-09-16 00:58:23 +00:00
|
|
|
.column([
|
|
|
|
'pages.*',
|
|
|
|
{
|
|
|
|
authorName: 'author.name',
|
2019-02-03 22:08:06 +00:00
|
|
|
authorEmail: 'author.email',
|
|
|
|
creatorName: 'creator.name',
|
|
|
|
creatorEmail: 'creator.email'
|
2018-09-16 00:58:23 +00:00
|
|
|
}
|
|
|
|
])
|
|
|
|
.joinRelation('author')
|
|
|
|
.joinRelation('creator')
|
2019-02-13 22:20:46 +00:00
|
|
|
.where(queryModeID ? {
|
|
|
|
'pages.id': opts
|
|
|
|
} : {
|
2018-09-16 00:58:23 +00:00
|
|
|
'pages.path': opts.path,
|
|
|
|
'pages.localeCode': opts.locale
|
|
|
|
})
|
|
|
|
.andWhere(builder => {
|
2019-02-13 22:20:46 +00:00
|
|
|
if (queryModeID) return
|
2018-09-16 00:58:23 +00:00
|
|
|
builder.where({
|
|
|
|
'pages.isPublished': true
|
|
|
|
}).orWhere({
|
|
|
|
'pages.isPublished': false,
|
|
|
|
'pages.authorId': opts.userId
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.andWhere(builder => {
|
2019-02-13 22:20:46 +00:00
|
|
|
if (queryModeID) return
|
2018-09-16 00:58:23 +00:00
|
|
|
if (opts.isPrivate) {
|
|
|
|
builder.where({ 'pages.isPrivate': true, 'pages.privateNS': opts.privateNS })
|
|
|
|
} else {
|
|
|
|
builder.where({ 'pages.isPrivate': false })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.first()
|
|
|
|
}
|
|
|
|
|
|
|
|
static async savePageToCache(page) {
|
|
|
|
const cachePath = path.join(process.cwd(), `data/cache/${page.hash}.bin`)
|
|
|
|
await fs.outputFile(cachePath, WIKI.models.pages.cacheSchema.encode({
|
2019-01-26 23:35:56 +00:00
|
|
|
id: page.id,
|
2018-09-16 00:58:23 +00:00
|
|
|
authorId: page.authorId,
|
|
|
|
authorName: page.authorName,
|
|
|
|
createdAt: page.createdAt,
|
|
|
|
creatorId: page.creatorId,
|
|
|
|
creatorName: page.creatorName,
|
|
|
|
description: page.description,
|
2018-12-02 04:03:14 +00:00
|
|
|
isPrivate: page.isPrivate === 1 || page.isPrivate === true,
|
|
|
|
isPublished: page.isPublished === 1 || page.isPublished === true,
|
2018-09-16 00:58:23 +00:00
|
|
|
publishEndDate: page.publishEndDate,
|
|
|
|
publishStartDate: page.publishStartDate,
|
|
|
|
render: page.render,
|
|
|
|
title: page.title,
|
2018-12-02 04:03:14 +00:00
|
|
|
toc: _.isString(page.toc) ? page.toc : JSON.stringify(page.toc),
|
2018-09-16 00:58:23 +00:00
|
|
|
updatedAt: page.updatedAt
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
static async getPageFromCache(opts) {
|
|
|
|
const pageHash = pageHelper.generateHash({ path: opts.path, locale: opts.locale, privateNS: opts.isPrivate ? 'TODO' : '' })
|
|
|
|
const cachePath = path.join(process.cwd(), `data/cache/${pageHash}.bin`)
|
|
|
|
|
|
|
|
try {
|
|
|
|
const pageBuffer = await fs.readFile(cachePath)
|
|
|
|
let page = WIKI.models.pages.cacheSchema.decode(pageBuffer)
|
|
|
|
return {
|
|
|
|
...page,
|
|
|
|
path: opts.path,
|
|
|
|
localeCode: opts.locale,
|
|
|
|
isPrivate: opts.isPrivate
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
WIKI.logger.error(err)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2019-01-26 23:35:56 +00:00
|
|
|
|
|
|
|
static async deletePageFromCache(page) {
|
|
|
|
return fs.remove(path.join(process.cwd(), `data/cache/${page.hash}.bin`))
|
|
|
|
}
|
2018-05-20 22:50:51 +00:00
|
|
|
}
|