2018-07-23 01:13:01 +00:00
|
|
|
const Model = require('objection').Model
|
2018-11-25 06:28:20 +00:00
|
|
|
const _ = require('lodash')
|
2018-07-23 01:13:01 +00:00
|
|
|
|
|
|
|
/* 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'},
|
2018-09-16 00:58:23 +00:00
|
|
|
hash: {type: 'string'},
|
2018-07-23 01:13:01 +00:00
|
|
|
title: {type: 'string'},
|
|
|
|
description: {type: 'string'},
|
|
|
|
isPublished: {type: 'boolean'},
|
|
|
|
publishStartDate: {type: 'string'},
|
|
|
|
publishEndDate: {type: 'string'},
|
|
|
|
content: {type: 'string'},
|
2018-11-11 04:40:55 +00:00
|
|
|
contentType: {type: 'string'},
|
2018-07-23 01:13:01 +00:00
|
|
|
|
|
|
|
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) {
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.pageHistory.query().insert({
|
2018-07-23 01:13:01 +00:00
|
|
|
pageId: opts.id,
|
|
|
|
authorId: opts.authorId,
|
|
|
|
content: opts.content,
|
2018-11-11 04:40:55 +00:00
|
|
|
contentType: opts.contentType,
|
2018-07-23 01:13:01 +00:00
|
|
|
description: opts.description,
|
|
|
|
editorKey: opts.editorKey,
|
2018-09-16 00:58:23 +00:00
|
|
|
hash: opts.hash,
|
2019-02-25 04:48:28 +00:00
|
|
|
isPrivate: (opts.isPrivate === true || opts.isPrivate === 1),
|
|
|
|
isPublished: (opts.isPublished === true || opts.isPublished === 1),
|
2018-07-23 01:13:01 +00:00
|
|
|
localeCode: opts.localeCode,
|
|
|
|
path: opts.path,
|
2018-11-18 04:03:58 +00:00
|
|
|
publishEndDate: opts.publishEndDate || '',
|
|
|
|
publishStartDate: opts.publishStartDate || '',
|
2019-01-26 23:35:56 +00:00
|
|
|
title: opts.title,
|
|
|
|
action: opts.action || 'updated'
|
2018-07-23 01:13:01 +00:00
|
|
|
})
|
|
|
|
}
|
2018-11-25 06:28:20 +00:00
|
|
|
|
2018-11-25 23:25:52 +00:00
|
|
|
static async getHistory({ pageId, offsetPage = 0, offsetSize = 100 }) {
|
2018-11-25 06:28:20 +00:00
|
|
|
const history = await WIKI.models.pageHistory.query()
|
|
|
|
.column([
|
|
|
|
'pageHistory.id',
|
|
|
|
'pageHistory.path',
|
|
|
|
'pageHistory.authorId',
|
2019-01-26 23:35:56 +00:00
|
|
|
'pageHistory.action',
|
2018-11-25 06:28:20 +00:00
|
|
|
'pageHistory.createdAt',
|
|
|
|
{
|
|
|
|
authorName: 'author.name'
|
|
|
|
}
|
|
|
|
])
|
2020-01-25 00:20:53 +00:00
|
|
|
.joinRelated('author')
|
2018-11-25 06:28:20 +00:00
|
|
|
.where({
|
|
|
|
'pageHistory.pageId': pageId
|
|
|
|
})
|
2018-11-25 23:25:52 +00:00
|
|
|
.orderBy('pageHistory.createdAt', 'desc')
|
|
|
|
.page(offsetPage, offsetSize)
|
2018-11-25 06:28:20 +00:00
|
|
|
|
|
|
|
let prevPh = null
|
2018-11-25 23:25:52 +00:00
|
|
|
const upperLimit = (offsetPage + 1) * offsetSize
|
2018-11-25 06:28:20 +00:00
|
|
|
|
2018-11-25 23:25:52 +00:00
|
|
|
if (history.total >= upperLimit) {
|
|
|
|
prevPh = await WIKI.models.pageHistory.query()
|
|
|
|
.column([
|
|
|
|
'pageHistory.id',
|
|
|
|
'pageHistory.path',
|
|
|
|
'pageHistory.authorId',
|
2019-01-26 23:35:56 +00:00
|
|
|
'pageHistory.action',
|
2018-11-25 23:25:52 +00:00
|
|
|
'pageHistory.createdAt',
|
|
|
|
{
|
|
|
|
authorName: 'author.name'
|
|
|
|
}
|
|
|
|
])
|
2020-01-25 00:20:53 +00:00
|
|
|
.joinRelated('author')
|
2018-11-25 23:25:52 +00:00
|
|
|
.where({
|
|
|
|
'pageHistory.pageId': pageId
|
|
|
|
})
|
|
|
|
.orderBy('pageHistory.createdAt', 'desc')
|
|
|
|
.offset((offsetPage + 1) * offsetSize)
|
|
|
|
.limit(1)
|
|
|
|
.first()
|
|
|
|
}
|
2018-11-25 06:28:20 +00:00
|
|
|
|
2018-11-25 23:25:52 +00:00
|
|
|
return {
|
|
|
|
trail: _.reduce(_.reverse(history.results), (res, ph) => {
|
|
|
|
let actionType = 'edit'
|
|
|
|
let valueBefore = null
|
|
|
|
let valueAfter = null
|
2018-11-25 06:28:20 +00:00
|
|
|
|
2018-11-25 23:25:52 +00:00
|
|
|
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
|
|
|
|
})
|
2018-11-25 06:28:20 +00:00
|
|
|
|
2018-11-25 23:25:52 +00:00
|
|
|
prevPh = ph
|
|
|
|
return res
|
|
|
|
}, []),
|
|
|
|
total: history.total
|
|
|
|
}
|
2018-11-25 06:28:20 +00:00
|
|
|
}
|
2018-07-23 01:13:01 +00:00
|
|
|
}
|