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

@@ -46,11 +46,11 @@ router.get(['/p', '/p/*'], (req, res, next) => {
})
/**
* View document
* History
*/
router.get(['/h', '/h/*'], async (req, res, next) => {
const pageArgs = pageHelper.parsePath(req.path)
const page = await WIKI.models.pages.getPage({
const page = await WIKI.models.pages.getPageFromDb({
path: pageArgs.path,
locale: pageArgs.locale,
userId: req.user.id,
@@ -63,6 +63,24 @@ router.get(['/h', '/h/*'], async (req, res, next) => {
}
})
/**
* Source
*/
router.get(['/s', '/s/*'], async (req, res, next) => {
const pageArgs = pageHelper.parsePath(req.path)
const page = await WIKI.models.pages.getPageFromDb({
path: pageArgs.path,
locale: pageArgs.locale,
userId: req.user.id,
isPrivate: false
})
if (page) {
res.render('source', { page })
} else {
res.redirect(`/${pageArgs.path}`)
}
})
/**
* View document
*/

View File

@@ -10,6 +10,12 @@ module.exports = {
async pages() { return {} }
},
PageQuery: {
async history(obj, args, context, info) {
return WIKI.models.pageHistory.getHistory({
pageId: args.id,
offset: args.offset || 0
})
},
async list(obj, args, context, info) {
return WIKI.models.pages.query().select(
'pages.*',

View File

@@ -15,6 +15,11 @@ extend type Mutation {
# -----------------------------------------------
type PageQuery {
history(
id: Int!
offset: Int
): [PageHistory]
list(
filter: String
orderBy: String
@@ -92,3 +97,13 @@ type Page {
createdAt: Date!
updatedAt: Date!
}
type PageHistory {
versionId: Int!
authorId: Int!
authorName: String!
actionType: String!
valueBefore: String
valueAfter: String
createdAt: Date!
}

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
}, [])
}
}

View File

@@ -197,7 +197,7 @@ module.exports = class Page extends Model {
}
static async getPageFromDb(opts) {
const page = await WIKI.models.pages.query()
return WIKI.models.pages.query()
.column([
'pages.*',
{
@@ -227,7 +227,6 @@ module.exports = class Page extends Model {
}
})
.first()
return page
}
static async savePageToCache(page) {

View File

@@ -5,7 +5,8 @@ block head
block body
#root
history(
id=page.id
:page-id=page.id
locale=page.localeCode
path=page.path
live-content=page.content
)

11
server/views/source.pug Normal file
View File

@@ -0,0 +1,11 @@
extends master.pug
block head
block body
#root
page-source(
page-id=page.id
locale=page.localeCode
path=page.path
)= page.content