wiki/server/models/pages.js
/**
   * Update an Existing Page
   *
   * @param {Object} opts Page Properties
   * @returns {Promise} Promise of the Page Model Instance
   */
Update an Existing Page
@param {Object} opts Page Properties
@returns {Promise} Promise of the Page Model Instance
Update an Existing Page @param { Object } opts Page Properties @returns { Promise } Promise of the Page Model Instance
static async updatePage(opts) {
    const ogPage = await WIKI.models.pages.query().findById(opts.id)
    if (!ogPage) {
      throw new Error('Invalid Page Id')
    }
    if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
      locale: opts.locale,
      path: opts.path
    })) {
      throw new WIKI.Error.PageUpdateForbidden()
    }
    if (!opts.content || _.trim(opts.content).length < 1) {
      throw new WIKI.Error.PageEmptyContent()
    }
    await WIKI.models.pageHistory.addVersion({
      ...ogPage,
      isPublished: ogPage.isPublished === true || ogPage.isPublished === 1,
      action: opts.action ? opts.action : 'updated',
      versionDate: ogPage.updatedAt
    })
    if (!_.isPlainObject(ogPage.extra)) {
      ogPage.extra = {}
    }
    let scriptCss = _.get(ogPage, 'extra.css', '')
    if (WIKI.auth.checkAccess(opts.user, ['write:styles'], {
      locale: opts.locale,
      path: opts.path
    })) {
      if (!_.isEmpty(opts.scriptCss)) {
        scriptCss = new CleanCSS({ inline: false }).minify(opts.scriptCss).styles
      } else {
        scriptCss = ''
      }
    }
    let scriptJs = _.get(ogPage, 'extra.js', '')
    if (WIKI.auth.checkAccess(opts.user, ['write:scripts'], {
      locale: opts.locale,
      path: opts.path
    })) {
      scriptJs = opts.scriptJs || ''
    }
    await WIKI.models.pages.query().patch({
      authorId: opts.user.id,
      content: opts.content,
      description: opts.description,
      isPublished: opts.isPublished === true || opts.isPublished === 1,
      publishEndDate: opts.publishEndDate || '',
      publishStartDate: opts.publishStartDate || '',
      title: opts.title,
      extra: JSON.stringify({
        ...ogPage.extra,
        js: scriptJs,
        css: scriptCss
      })
    }).where('id', ogPage.id)
    let page = await WIKI.models.pages.getPageFromDb(ogPage.id)
    await WIKI.models.tags.associateTags({ tags: opts.tags, page })
    await WIKI.models.pages.renderPage(page)
    WIKI.events.outbound.emit('deletePageFromCache', page.hash)
    const pageContents = await WIKI.models.pages.query().findById(page.id).select('render')
    page.safeContent = WIKI.models.pages.cleanHTML(pageContents.render)
    await WIKI.data.searchEngine.updated(page)
    if (!opts.skipStorage) {
      await WIKI.models.storage.pageEvent({
        event: 'updated',
        page
      })
    }
    if ((opts.locale && opts.locale !== page.localeCode) || (opts.path && opts.path !== page.path)) {
      await WIKI.models.pages.movePage({
        id: page.id,
        destinationLocale: opts.locale,
        destinationPath: opts.path,
        user: opts.user
      })
    } else {
      await WIKI.models.knex.table('pageTree').where({
        pageId: page.id
      }).update('title', page.title)
    }
    page.updatedAt = await WIKI.models.pages.query().findById(page.id).select('updatedAt').then(r => r.updatedAt)
    return page
  }
static async updatePage ( opts ) { const ogPage = await WIKI . models . pages . query ( ) . findById ( opts . id ) if ( ! ogPage ) { throw new Error ( 'Invalid Page Id' ) } if ( ! WIKI . auth . checkAccess ( opts . user , [ 'write:pages' ] , { locale : opts . locale , path : opts . path } ) ) { throw new WIKI . Error . PageUpdateForbidden ( ) } if ( ! opts . content || _ . trim ( opts . content ) . length < 1 ) { throw new WIKI . Error . PageEmptyContent ( ) } await WIKI . models . pageHistory . addVersion ( { ... ogPage , isPublished : ogPage . isPublished === true || ogPage . isPublished === 1 , action : opts . action ? opts . action : 'updated' , versionDate : ogPage . updatedAt } ) if ( ! _ . isPlainObject ( ogPage . extra ) ) { ogPage . extra = { } } let scriptCss = _ . get ( ogPage , 'extra.css' , '' ) if ( WIKI . auth . checkAccess ( opts . user , [ 'write:styles' ] , { locale : opts . locale , path : opts . path } ) ) { if ( ! _ . isEmpty ( opts . scriptCss ) ) { scriptCss = new CleanCSS ( { inline : false } ) . minify ( opts . scriptCss ) . styles } else { scriptCss = '' } } let scriptJs = _ . get ( ogPage , 'extra.js' , '' ) if ( WIKI . auth . checkAccess ( opts . user , [ 'write:scripts' ] , { locale : opts . locale , path : opts . path } ) ) { scriptJs = opts . scriptJs || '' } await WIKI . models . pages . query ( ) . patch ( { authorId : opts . user . id , content : opts . content , description : opts . description , isPublished : opts . isPublished === true || opts . isPublished === 1 , publishEndDate : opts . publishEndDate || '' , publishStartDate : opts . publishStartDate || '' , title : opts . title , extra : JSON . stringify ( { ... ogPage . extra , js : scriptJs , css : scriptCss } ) } ) . where ( 'id' , ogPage . id ) let page = await WIKI . models . pages . getPageFromDb ( ogPage . id ) await WIKI . models . tags . associateTags ( { tags : opts . tags , page } ) await WIKI . models . pages . renderPage ( page ) WIKI . events . outbound . emit ( 'deletePageFromCache' , page . hash ) const pageContents = await WIKI . models . pages . query ( ) . findById ( page . id ) . select ( 'render' ) page . safeContent = WIKI . models . pages . cleanHTML ( pageContents . render ) await WIKI . data . searchEngine . updated ( page ) if ( ! opts . skipStorage ) { await WIKI . models . storage . pageEvent ( { event : 'updated' , page } ) } if ( ( opts . locale && opts . locale !== page . localeCode ) || ( opts . path && opts . path !== page . path ) ) { await WIKI . models . pages . movePage ( { id : page . id , destinationLocale : opts . locale , destinationPath : opts . path , user : opts . user } ) } else { await WIKI . models . knex . table ( 'pageTree' ) . where ( { pageId : page . id } ) . update ( 'title' , page . title ) } page . updatedAt = await WIKI . models . pages . query ( ) . findById ( page . id ) . select ( 'updatedAt' ) . then ( r => r . updatedAt ) return page }
Update an Existing Page
 @param {Object} opts Page Properties
 @returns {Promise} Promise of the Page Model Instance
Update an Existing Page @param { Object } opts Page Properties @returns { Promise } Promise of the Page Model Instance
// -> Fetch original page // -> Check for page access // -> Check for empty content // -> Create version snapshot // -> Format Extra Properties // -> Format CSS Scripts // -> Format JS Scripts // -> Update page // -> Save Tags // -> Render page to HTML // -> Update Search Index // -> Update on Storage // -> Perform move? // -> Update title of page tree entry // -> Get latest updatedAt