feat: source + history (wip)

This commit is contained in:
Nicolas Giard
2018-11-25 01:28:20 -05:00
parent 076aeaf749
commit 78ba895eee
17 changed files with 374 additions and 81 deletions

View File

@@ -1,4 +1,5 @@
const Model = require('objection').Model
const _ = require('lodash')
/* global WIKI */
@@ -101,4 +102,53 @@ module.exports = class PageHistory extends Model {
title: opts.title
})
}
static async getHistory({ pageId, offset = 0 }) {
const history = await WIKI.models.pageHistory.query()
.column([
'pageHistory.id',
'pageHistory.path',
'pageHistory.authorId',
'pageHistory.createdAt',
{
authorName: 'author.name'
}
])
.joinRelation('author')
.where({
'pageHistory.pageId': pageId
})
.orderBy('pageHistory.createdAt', 'asc')
.offset(offset)
.limit(20)
let prevPh = null
return _.reduce(history, (res, ph) => {
let actionType = 'edit'
let valueBefore = null
let valueAfter = null
if (!prevPh && offset === 0) {
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
})
prevPh = ph
return res
}, [])
}
}