pageHistory.js 4.54 KB
Newer Older
1
const Model = require('objection').Model
2
const _ = require('lodash')
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

/* 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'},
20
        hash: {type: 'string'},
21 22 23 24 25 26
        title: {type: 'string'},
        description: {type: 'string'},
        isPublished: {type: 'boolean'},
        publishStartDate: {type: 'string'},
        publishEndDate: {type: 'string'},
        content: {type: 'string'},
27
        contentType: {type: 'string'},
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

        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) {
88
    await WIKI.models.pageHistory.query().insert({
89 90 91
      pageId: opts.id,
      authorId: opts.authorId,
      content: opts.content,
92
      contentType: opts.contentType,
93 94
      description: opts.description,
      editorKey: opts.editorKey,
95
      hash: opts.hash,
Nick's avatar
Nick committed
96 97
      isPrivate: (opts.isPrivate === true || opts.isPrivate === 1),
      isPublished: (opts.isPublished === true || opts.isPublished === 1),
98 99
      localeCode: opts.localeCode,
      path: opts.path,
100 101
      publishEndDate: opts.publishEndDate || '',
      publishStartDate: opts.publishStartDate || '',
Nicolas Giard's avatar
Nicolas Giard committed
102 103
      title: opts.title,
      action: opts.action || 'updated'
104 105
    })
  }
106

107
  static async getHistory({ pageId, offsetPage = 0, offsetSize = 100 }) {
108 109 110 111 112
    const history = await WIKI.models.pageHistory.query()
      .column([
        'pageHistory.id',
        'pageHistory.path',
        'pageHistory.authorId',
Nicolas Giard's avatar
Nicolas Giard committed
113
        'pageHistory.action',
114 115 116 117 118 119 120 121 122
        'pageHistory.createdAt',
        {
          authorName: 'author.name'
        }
      ])
      .joinRelation('author')
      .where({
        'pageHistory.pageId': pageId
      })
123 124
      .orderBy('pageHistory.createdAt', 'desc')
      .page(offsetPage, offsetSize)
125 126

    let prevPh = null
127
    const upperLimit = (offsetPage + 1) * offsetSize
128

129 130 131 132 133 134
    if (history.total >= upperLimit) {
      prevPh = await WIKI.models.pageHistory.query()
        .column([
          'pageHistory.id',
          'pageHistory.path',
          'pageHistory.authorId',
Nicolas Giard's avatar
Nicolas Giard committed
135
          'pageHistory.action',
136 137 138 139 140 141 142 143 144 145 146 147 148 149
          'pageHistory.createdAt',
          {
            authorName: 'author.name'
          }
        ])
        .joinRelation('author')
        .where({
          'pageHistory.pageId': pageId
        })
        .orderBy('pageHistory.createdAt', 'desc')
        .offset((offsetPage + 1) * offsetSize)
        .limit(1)
        .first()
    }
150

151 152 153 154 155
    return {
      trail: _.reduce(_.reverse(history.results), (res, ph) => {
        let actionType = 'edit'
        let valueBefore = null
        let valueAfter = null
156

157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        if (!prevPh && history.total < upperLimit) {
          actionType = 'initial'
        } else if (_.get(prevPh, 'path', '') !== ph.path) {
          actionType = 'move'
          valueBefore = _.get(prevPh, 'path', '')
          valueAfter = ph.path
        }

        res.unshift({
          versionId: ph.id,
          authorId: ph.authorId,
          authorName: ph.authorName,
          actionType,
          valueBefore,
          valueAfter,
          createdAt: ph.createdAt
        })
174

175 176 177 178 179
        prevPh = ph
        return res
      }, []),
      total: history.total
    }
180
  }
181
}