123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- const Model = require('objection').Model
- const _ = require('lodash')
- /* global WIKI */
- /**
- * 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'},
- title: {type: 'string'},
- description: {type: 'string'},
- isPublished: {type: 'boolean'},
- privateNS: {type: 'string'},
- publishStartDate: {type: 'string'},
- publishEndDate: {type: 'string'},
- content: {type: 'string'},
- contentType: {type: 'string'},
- 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'
- }
- },
- creator: {
- relation: Model.BelongsToOneRelation,
- modelClass: require('./users'),
- join: {
- from: 'pages.creatorId',
- to: 'users.id'
- }
- },
- editor: {
- relation: Model.BelongsToOneRelation,
- modelClass: require('./editors'),
- join: {
- from: 'pages.editorKey',
- to: 'editors.key'
- }
- },
- locale: {
- relation: Model.BelongsToOneRelation,
- modelClass: require('./locales'),
- join: {
- from: 'pages.localeCode',
- to: 'locales.code'
- }
- }
- }
- }
- $beforeUpdate() {
- this.updatedAt = new Date().toISOString()
- }
- $beforeInsert() {
- this.createdAt = new Date().toISOString()
- this.updatedAt = new Date().toISOString()
- }
- static async getPage(opts) {
- const page = await WIKI.models.pages.query().where({
- path: opts.path,
- localeCode: opts.locale
- }).andWhere(builder => {
- builder.where({
- isPublished: true
- }).orWhere({
- isPublished: false,
- authorId: opts.userId
- })
- }).andWhere(builder => {
- if (opts.private) {
- builder.where({ isPrivate: true, privateNS: opts.privateNS })
- } else {
- builder.where({ isPrivate: false })
- }
- }).first()
- return page
- }
- static async createPage(opts) {
- await WIKI.models.pages.renderPage(opts)
- const page = await WIKI.models.pages.query().insertAndFetch({
- authorId: opts.authorId,
- content: opts.content,
- creatorId: opts.authorId,
- contentType: _.get(WIKI.data.editors, `${opts.editor}.contentType`, 'text'),
- description: opts.description,
- editorKey: opts.editor,
- isPrivate: opts.isPrivate,
- isPublished: opts.isPublished,
- localeCode: opts.locale,
- path: opts.path,
- publishEndDate: opts.publishEndDate,
- publishStartDate: opts.publishStartDate,
- title: opts.title
- })
- 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)
- const page = await WIKI.models.pages.query().patchAndFetchById(ogPage.id, {
- authorId: opts.authorId,
- content: opts.content,
- description: opts.description,
- isPublished: opts.isPublished,
- publishEndDate: opts.publishEndDate,
- publishStartDate: opts.publishStartDate,
- title: opts.title
- })
- await WIKI.models.storage.pageEvent({
- event: 'updated',
- page
- })
- return page
- }
- static async renderPage(opts) {
- WIKI.queue.job.renderPage.add(opts, {
- removeOnComplete: true,
- removeOnFail: true
- })
- }
- }
|