feat: save page - updated + page history
This commit is contained in:
		
							
								
								
									
										100
									
								
								server/db/models/pageHistory.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										100
									
								
								server/db/models/pageHistory.js
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,100 @@
 | 
			
		||||
const Model = require('objection').Model
 | 
			
		||||
 | 
			
		||||
/* global WIKI */
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Page History model
 | 
			
		||||
 */
 | 
			
		||||
module.exports = class PageHistory extends Model {
 | 
			
		||||
  static get tableName() { return 'pageHistory' }
 | 
			
		||||
 | 
			
		||||
  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'},
 | 
			
		||||
        publishStartDate: {type: 'string'},
 | 
			
		||||
        publishEndDate: {type: 'string'},
 | 
			
		||||
        content: {type: 'string'},
 | 
			
		||||
 | 
			
		||||
        createdAt: {type: 'string'}
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static get relationMappings() {
 | 
			
		||||
    return {
 | 
			
		||||
      tags: {
 | 
			
		||||
        relation: Model.ManyToManyRelation,
 | 
			
		||||
        modelClass: require('./tags'),
 | 
			
		||||
        join: {
 | 
			
		||||
          from: 'pageHistory.id',
 | 
			
		||||
          through: {
 | 
			
		||||
            from: 'pageHistoryTags.pageId',
 | 
			
		||||
            to: 'pageHistoryTags.tagId'
 | 
			
		||||
          },
 | 
			
		||||
          to: 'tags.id'
 | 
			
		||||
        }
 | 
			
		||||
      },
 | 
			
		||||
      page: {
 | 
			
		||||
        relation: Model.BelongsToOneRelation,
 | 
			
		||||
        modelClass: require('./pages'),
 | 
			
		||||
        join: {
 | 
			
		||||
          from: 'pageHistory.pageId',
 | 
			
		||||
          to: 'pages.id'
 | 
			
		||||
        }
 | 
			
		||||
      },
 | 
			
		||||
      author: {
 | 
			
		||||
        relation: Model.BelongsToOneRelation,
 | 
			
		||||
        modelClass: require('./users'),
 | 
			
		||||
        join: {
 | 
			
		||||
          from: 'pageHistory.authorId',
 | 
			
		||||
          to: 'users.id'
 | 
			
		||||
        }
 | 
			
		||||
      },
 | 
			
		||||
      editor: {
 | 
			
		||||
        relation: Model.BelongsToOneRelation,
 | 
			
		||||
        modelClass: require('./editors'),
 | 
			
		||||
        join: {
 | 
			
		||||
          from: 'pageHistory.editorKey',
 | 
			
		||||
          to: 'editors.key'
 | 
			
		||||
        }
 | 
			
		||||
      },
 | 
			
		||||
      locale: {
 | 
			
		||||
        relation: Model.BelongsToOneRelation,
 | 
			
		||||
        modelClass: require('./locales'),
 | 
			
		||||
        join: {
 | 
			
		||||
          from: 'pageHistory.localeCode',
 | 
			
		||||
          to: 'locales.code'
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  $beforeInsert() {
 | 
			
		||||
    this.createdAt = new Date().toISOString()
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static async addVersion(opts) {
 | 
			
		||||
    await WIKI.db.pageHistory.query().insert({
 | 
			
		||||
      pageId: opts.id,
 | 
			
		||||
      authorId: opts.authorId,
 | 
			
		||||
      content: opts.content,
 | 
			
		||||
      description: opts.description,
 | 
			
		||||
      editorKey: opts.editorKey,
 | 
			
		||||
      isPrivate: opts.isPrivate,
 | 
			
		||||
      isPublished: opts.isPublished,
 | 
			
		||||
      localeCode: opts.localeCode,
 | 
			
		||||
      path: opts.path,
 | 
			
		||||
      publishEndDate: opts.publishEndDate,
 | 
			
		||||
      publishStartDate: opts.publishStartDate,
 | 
			
		||||
      title: opts.title
 | 
			
		||||
    })
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
@@ -51,6 +51,14 @@ module.exports = class Page extends Model {
 | 
			
		||||
          to: 'users.id'
 | 
			
		||||
        }
 | 
			
		||||
      },
 | 
			
		||||
      creator: {
 | 
			
		||||
        relation: Model.BelongsToOneRelation,
 | 
			
		||||
        modelClass: require('./users'),
 | 
			
		||||
        join: {
 | 
			
		||||
          from: 'pages.creatorId',
 | 
			
		||||
          to: 'users.id'
 | 
			
		||||
        }
 | 
			
		||||
      },
 | 
			
		||||
      editor: {
 | 
			
		||||
        relation: Model.BelongsToOneRelation,
 | 
			
		||||
        modelClass: require('./editors'),
 | 
			
		||||
@@ -82,6 +90,7 @@ module.exports = class Page extends Model {
 | 
			
		||||
    const page = await WIKI.db.pages.query().insertAndFetch({
 | 
			
		||||
      authorId: opts.authorId,
 | 
			
		||||
      content: opts.content,
 | 
			
		||||
      creatorId: opts.authorId,
 | 
			
		||||
      description: opts.description,
 | 
			
		||||
      editorKey: opts.editor,
 | 
			
		||||
      isPrivate: opts.isPrivate,
 | 
			
		||||
@@ -92,7 +101,26 @@ module.exports = class Page extends Model {
 | 
			
		||||
      publishStartDate: opts.publishStartDate,
 | 
			
		||||
      title: opts.title
 | 
			
		||||
    })
 | 
			
		||||
    await WIKI.db.storage.createPage(page)
 | 
			
		||||
    await WIKI.db.storage.pageEvent({
 | 
			
		||||
      event: 'created',
 | 
			
		||||
      page
 | 
			
		||||
    })
 | 
			
		||||
    return page
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static async updatePage(opts) {
 | 
			
		||||
    const ogPage = await WIKI.db.pages.query().findById(opts.id)
 | 
			
		||||
    if (!ogPage) {
 | 
			
		||||
      throw new Error('Invalid Page Id')
 | 
			
		||||
    }
 | 
			
		||||
    await WIKI.db.pageHistory.addVersion(ogPage)
 | 
			
		||||
    const page = await WIKI.db.pages.query().patchAndFetch({
 | 
			
		||||
      title: opts.title
 | 
			
		||||
    }).where('id', opts.id)
 | 
			
		||||
    await WIKI.db.storage.pageEvent({
 | 
			
		||||
      event: 'updated',
 | 
			
		||||
      page
 | 
			
		||||
    })
 | 
			
		||||
    return page
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -87,12 +87,12 @@ module.exports = class Storage extends Model {
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  static async createPage(page) {
 | 
			
		||||
  static async pageEvent(event, page) {
 | 
			
		||||
    const targets = await WIKI.db.storage.query().where('isEnabled', true)
 | 
			
		||||
    if (targets && targets.length > 0) {
 | 
			
		||||
      _.forEach(targets, target => {
 | 
			
		||||
        WIKI.queue.job.syncStorage.add({
 | 
			
		||||
          event: 'created',
 | 
			
		||||
          event,
 | 
			
		||||
          target,
 | 
			
		||||
          page
 | 
			
		||||
        }, {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user