const Model = require('objection').Model const _ = require('lodash') const JSBinType = require('js-binary').Type const pageHelper = require('../helpers/page') const path = require('path') const fs = require('fs-extra') const yaml = require('js-yaml') /* global WIKI */ const frontmatterRegex = { html: /^()?(?:\n|\r)*([\w\W]*)*/, legacy: /^(\n\n' + this.content default: return this.content } } /** * Parse injected page metadata from raw content * * @param {String} raw Raw file contents * @param {String} contentType Content Type */ static parseMetadata (raw, contentType) { let result switch (contentType) { case 'markdown': result = frontmatterRegex.markdown.exec(raw) if (result[2]) { return { ...yaml.safeLoad(result[2]), content: result[3] } } else { // Attempt legacy v1 format result = frontmatterRegex.legacy.exec(raw) if (result[2]) { return { title: result[2], description: result[4], content: result[5] } } } break case 'html': result = frontmatterRegex.html.exec(raw) if (result[2]) { return { ...yaml.safeLoad(result[2]), content: result[3] } } break } return { content: raw } } static async createPage(opts) { await WIKI.models.pages.query().insert({ authorId: opts.authorId, content: opts.content, creatorId: opts.authorId, contentType: _.get(_.find(WIKI.data.editors, ['key', opts.editor]), `contentType`, 'text'), description: opts.description, editorKey: opts.editor, hash: pageHelper.generateHash({ path: opts.path, locale: opts.locale, privateNS: opts.isPrivate ? 'TODO' : '' }), isPrivate: opts.isPrivate, isPublished: opts.isPublished, localeCode: opts.locale, path: opts.path, publishEndDate: opts.publishEndDate || '', publishStartDate: opts.publishStartDate || '', title: opts.title, toc: '[]' }) const page = await WIKI.models.pages.getPageFromDb({ path: opts.path, locale: opts.locale, userId: opts.authorId, isPrivate: opts.isPrivate }) await WIKI.models.pages.renderPage(page) if (!opts.skipStorage) { await WIKI.models.storage.pageEvent({ event: 'created', page }) } return page } static async updatePage(opts) { const ogPage = await WIKI.models.pages.query().findById(opts.id) if (!ogPage) { throw new Error('Invalid Page Id') } await WIKI.models.pageHistory.addVersion({ ...ogPage, isPublished: ogPage.isPublished === true || ogPage.isPublished === 1, action: 'updated' }) await WIKI.models.pages.query().patch({ authorId: opts.authorId, content: opts.content, description: opts.description, isPublished: opts.isPublished === true || opts.isPublished === 1, publishEndDate: opts.publishEndDate || '', publishStartDate: opts.publishStartDate || '', title: opts.title }).where('id', ogPage.id) const page = await WIKI.models.pages.getPageFromDb({ path: ogPage.path, locale: ogPage.localeCode, userId: ogPage.authorId, isPrivate: ogPage.isPrivate }) await WIKI.models.pages.renderPage(page) if (!opts.skipStorage) { await WIKI.models.storage.pageEvent({ event: 'updated', page }) } return page } static async deletePage(opts) { let page if (_.has(opts, 'id')) { page = await WIKI.models.pages.query().findById(opts.id) } else { page = await await WIKI.models.pages.query().findOne({ path: opts.path, localeCode: opts.locale }) } 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) if (!opts.skipStorage) { await WIKI.models.storage.pageEvent({ event: 'deleted', page }) } } static async renderPage(page) { const renderJob = await WIKI.scheduler.registerJob({ name: 'render-page', immediate: true, worker: true }, page.id) return renderJob.finished } static async getPage(opts) { let page = await WIKI.models.pages.getPageFromCache(opts) if (!page) { page = await WIKI.models.pages.getPageFromDb(opts) if (page) { await WIKI.models.pages.savePageToCache(page) } } return page } static async getPageFromDb(opts) { const queryModeID = _.isNumber(opts) return WIKI.models.pages.query() .column([ 'pages.*', { authorName: 'author.name', authorEmail: 'author.email', creatorName: 'creator.name', creatorEmail: 'creator.email' } ]) .joinRelation('author') .joinRelation('creator') .where(queryModeID ? { 'pages.id': opts } : { 'pages.path': opts.path, 'pages.localeCode': opts.locale }) .andWhere(builder => { if (queryModeID) return builder.where({ 'pages.isPublished': true }).orWhere({ 'pages.isPublished': false, 'pages.authorId': opts.userId }) }) .andWhere(builder => { if (queryModeID) return 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({ id: page.id, authorId: page.authorId, authorName: page.authorName, createdAt: page.createdAt, creatorId: page.creatorId, creatorName: page.creatorName, description: page.description, isPrivate: page.isPrivate === 1 || page.isPrivate === true, isPublished: page.isPublished === 1 || page.isPublished === true, publishEndDate: page.publishEndDate, publishStartDate: page.publishStartDate, render: page.render, title: page.title, toc: _.isString(page.toc) ? page.toc : JSON.stringify(page.toc), 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 } } static async deletePageFromCache(page) { return fs.remove(path.join(process.cwd(), `data/cache/${page.hash}.bin`)) } }