pageHistory.js 5.76 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

        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()
  }

87 88 89
  /**
   * Create Page Version
   */
90
  static async addVersion(opts) {
91
    await WIKI.models.pageHistory.query().insert({
92 93 94
      pageId: opts.id,
      authorId: opts.authorId,
      content: opts.content,
95
      contentType: opts.contentType,
96 97
      description: opts.description,
      editorKey: opts.editorKey,
98
      hash: opts.hash,
Nick's avatar
Nick committed
99 100
      isPrivate: (opts.isPrivate === true || opts.isPrivate === 1),
      isPublished: (opts.isPublished === true || opts.isPublished === 1),
101 102
      localeCode: opts.localeCode,
      path: opts.path,
103 104
      publishEndDate: opts.publishEndDate || '',
      publishStartDate: opts.publishStartDate || '',
Nicolas Giard's avatar
Nicolas Giard committed
105
      title: opts.title,
106 107
      action: opts.action || 'updated',
      versionDate: opts.versionDate
108 109
    })
  }
110

111 112 113
  /**
   * Get Page Version
   */
NGPixel's avatar
NGPixel committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
  static async getVersion({ pageId, versionId }) {
    const version = await WIKI.models.pageHistory.query()
      .column([
        'pageHistory.path',
        'pageHistory.title',
        'pageHistory.description',
        'pageHistory.isPrivate',
        'pageHistory.isPublished',
        'pageHistory.publishStartDate',
        'pageHistory.publishEndDate',
        'pageHistory.content',
        'pageHistory.contentType',
        'pageHistory.createdAt',
        'pageHistory.action',
        'pageHistory.authorId',
        'pageHistory.pageId',
130
        'pageHistory.versionDate',
NGPixel's avatar
NGPixel committed
131 132 133 134 135 136 137 138 139 140 141 142
        {
          versionId: 'pageHistory.id',
          editor: 'pageHistory.editorKey',
          locale: 'pageHistory.localeCode',
          authorName: 'author.name'
        }
      ])
      .joinRelated('author')
      .where({
        'pageHistory.id': versionId,
        'pageHistory.pageId': pageId
      }).first()
143 144 145 146 147 148 149 150
    if (version) {
      return {
        ...version,
        updatedAt: version.createdAt || null,
        tags: []
      }
    } else {
      return null
NGPixel's avatar
NGPixel committed
151 152 153
    }
  }

154 155 156
  /**
   * Get History Trail of a Page
   */
157
  static async getHistory({ pageId, offsetPage = 0, offsetSize = 100 }) {
158 159 160 161 162
    const history = await WIKI.models.pageHistory.query()
      .column([
        'pageHistory.id',
        'pageHistory.path',
        'pageHistory.authorId',
Nicolas Giard's avatar
Nicolas Giard committed
163
        'pageHistory.action',
164
        'pageHistory.versionDate',
165 166 167 168
        {
          authorName: 'author.name'
        }
      ])
NGPixel's avatar
NGPixel committed
169
      .joinRelated('author')
170 171 172
      .where({
        'pageHistory.pageId': pageId
      })
173
      .orderBy('pageHistory.versionDate', 'desc')
174
      .page(offsetPage, offsetSize)
175 176

    let prevPh = null
177
    const upperLimit = (offsetPage + 1) * offsetSize
178

179 180 181 182 183 184
    if (history.total >= upperLimit) {
      prevPh = await WIKI.models.pageHistory.query()
        .column([
          'pageHistory.id',
          'pageHistory.path',
          'pageHistory.authorId',
Nicolas Giard's avatar
Nicolas Giard committed
185
          'pageHistory.action',
186
          'pageHistory.versionDate',
187 188 189 190
          {
            authorName: 'author.name'
          }
        ])
NGPixel's avatar
NGPixel committed
191
        .joinRelated('author')
192 193 194
        .where({
          'pageHistory.pageId': pageId
        })
195
        .orderBy('pageHistory.versionDate', 'desc')
196 197 198 199
        .offset((offsetPage + 1) * offsetSize)
        .limit(1)
        .first()
    }
200

201 202 203 204 205
    return {
      trail: _.reduce(_.reverse(history.results), (res, ph) => {
        let actionType = 'edit'
        let valueBefore = null
        let valueAfter = null
206

207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
        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,
222
          versionDate: ph.versionDate
223
        })
224

225 226 227 228 229
        prevPh = ph
        return res
      }, []),
      total: history.total
    }
230
  }
231
}