pageHistory.js 6.13 KB
Newer Older
1
const Model = require('objection').Model
2
const _ = require('lodash')
NGPixel's avatar
NGPixel committed
3
const { DateTime, Duration } = require('luxon')
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

/* 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'},
21
        hash: {type: 'string'},
22 23 24 25 26 27
        title: {type: 'string'},
        description: {type: 'string'},
        isPublished: {type: 'boolean'},
        publishStartDate: {type: 'string'},
        publishEndDate: {type: 'string'},
        content: {type: 'string'},
28
        contentType: {type: 'string'},
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()
  }

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

112 113 114
  /**
   * Get Page Version
   */
NGPixel's avatar
NGPixel committed
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  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',
131
        'pageHistory.versionDate',
NGPixel's avatar
NGPixel committed
132 133 134 135 136 137 138 139 140 141 142 143
        {
          versionId: 'pageHistory.id',
          editor: 'pageHistory.editorKey',
          locale: 'pageHistory.localeCode',
          authorName: 'author.name'
        }
      ])
      .joinRelated('author')
      .where({
        'pageHistory.id': versionId,
        'pageHistory.pageId': pageId
      }).first()
144 145 146 147 148 149 150 151
    if (version) {
      return {
        ...version,
        updatedAt: version.createdAt || null,
        tags: []
      }
    } else {
      return null
NGPixel's avatar
NGPixel committed
152 153 154
    }
  }

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

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

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

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

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

226 227 228 229 230
        prevPh = ph
        return res
      }, []),
      total: history.total
    }
231
  }
NGPixel's avatar
NGPixel committed
232 233 234 235 236 237 238 239 240 241 242

  /**
   * Purge history older than X
   *
   * @param {String} olderThan ISO 8601 Duration
   */
  static async purge (olderThan) {
    const dur = Duration.fromISO(olderThan)
    const olderThanISO = DateTime.utc().minus(dur)
    await WIKI.models.pageHistory.query().where('versionDate', '<', olderThanISO.toISO()).del()
  }
243
}