2019-09-09 01:31:22 +00:00
|
|
|
const _ = require('lodash')
|
2018-05-20 22:50:51 +00:00
|
|
|
const graphHelper = require('../../helpers/graph')
|
|
|
|
|
|
|
|
/* global WIKI */
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
Query: {
|
|
|
|
async pages() { return {} }
|
|
|
|
},
|
|
|
|
Mutation: {
|
|
|
|
async pages() { return {} }
|
|
|
|
},
|
|
|
|
PageQuery: {
|
2019-10-13 23:59:50 +00:00
|
|
|
/**
|
|
|
|
* PAGE HISTORY
|
|
|
|
*/
|
2018-11-25 06:28:20 +00:00
|
|
|
async history(obj, args, context, info) {
|
|
|
|
return WIKI.models.pageHistory.getHistory({
|
|
|
|
pageId: args.id,
|
2018-11-25 23:25:52 +00:00
|
|
|
offsetPage: args.offsetPage || 0,
|
|
|
|
offsetSize: args.offsetSize || 100
|
2018-11-25 06:28:20 +00:00
|
|
|
})
|
2019-03-09 05:51:02 +00:00
|
|
|
},
|
2019-10-13 23:59:50 +00:00
|
|
|
/**
|
|
|
|
* SEARCH PAGES
|
|
|
|
*/
|
2019-03-09 05:51:02 +00:00
|
|
|
async search (obj, args, context) {
|
|
|
|
if (WIKI.data.searchEngine) {
|
2019-09-09 01:31:22 +00:00
|
|
|
const resp = await WIKI.data.searchEngine.query(args.query, args)
|
|
|
|
return {
|
|
|
|
...resp,
|
|
|
|
results: _.filter(resp.results, r => {
|
|
|
|
return WIKI.auth.checkAccess(context.req.user, ['read:pages'], {
|
|
|
|
path: r.path,
|
|
|
|
locale: r.locale
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
2019-03-09 05:51:02 +00:00
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
results: [],
|
|
|
|
suggestions: [],
|
|
|
|
totalHits: 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2019-10-13 23:59:50 +00:00
|
|
|
/**
|
|
|
|
* LIST PAGES
|
|
|
|
*/
|
2019-04-07 22:10:44 +00:00
|
|
|
async list (obj, args, context, info) {
|
2019-09-09 01:31:22 +00:00
|
|
|
let results = await WIKI.models.pages.query().column([
|
|
|
|
'pages.id',
|
2019-04-07 22:10:44 +00:00
|
|
|
'path',
|
|
|
|
{ locale: 'localeCode' },
|
|
|
|
'title',
|
|
|
|
'description',
|
|
|
|
'isPublished',
|
|
|
|
'isPrivate',
|
|
|
|
'privateNS',
|
|
|
|
'contentType',
|
|
|
|
'createdAt',
|
|
|
|
'updatedAt'
|
2019-09-09 01:31:22 +00:00
|
|
|
])
|
|
|
|
.eagerAlgorithm(WIKI.models.Objection.Model.JoinEagerAlgorithm)
|
|
|
|
.eager('tags(selectTags)', {
|
|
|
|
selectTags: builder => {
|
|
|
|
builder.select('tag')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.modify(queryBuilder => {
|
|
|
|
if (args.limit) {
|
|
|
|
queryBuilder.limit(args.limit)
|
|
|
|
}
|
|
|
|
if (args.locale) {
|
|
|
|
queryBuilder.where('localeCode', args.locale)
|
|
|
|
}
|
|
|
|
if (args.tags && args.tags.length > 0) {
|
|
|
|
queryBuilder.whereIn('tags.tag', args.tags)
|
|
|
|
}
|
|
|
|
const orderDir = args.orderByDirection === 'DESC' ? 'desc' : 'asc'
|
|
|
|
switch (args.orderBy) {
|
|
|
|
case 'CREATED':
|
|
|
|
queryBuilder.orderBy('createdAt', orderDir)
|
|
|
|
break
|
|
|
|
case 'PATH':
|
|
|
|
queryBuilder.orderBy('path', orderDir)
|
|
|
|
break
|
|
|
|
case 'TITLE':
|
|
|
|
queryBuilder.orderBy('title', orderDir)
|
|
|
|
break
|
|
|
|
case 'UPDATED':
|
|
|
|
queryBuilder.orderBy('updatedAt', orderDir)
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
queryBuilder.orderBy('pages.id', orderDir)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
})
|
|
|
|
results = _.filter(results, r => {
|
|
|
|
return WIKI.auth.checkAccess(context.req.user, ['read:pages'], {
|
|
|
|
path: r.path,
|
|
|
|
locale: r.locale
|
|
|
|
})
|
|
|
|
}).map(r => ({
|
|
|
|
...r,
|
|
|
|
tags: _.map(r.tags, 'tag')
|
|
|
|
}))
|
|
|
|
if (args.tags && args.tags.length > 0) {
|
|
|
|
results = _.filter(results, r => _.every(args.tags, t => _.includes(r.tags, t)))
|
|
|
|
}
|
|
|
|
return results
|
2019-07-02 05:48:19 +00:00
|
|
|
},
|
2019-10-13 23:59:50 +00:00
|
|
|
/**
|
|
|
|
* FETCH SINGLE PAGE
|
|
|
|
*/
|
2019-07-02 05:48:19 +00:00
|
|
|
async single (obj, args, context, info) {
|
|
|
|
let page = await WIKI.models.pages.getPageFromDb(args.id)
|
|
|
|
if (page) {
|
|
|
|
return {
|
|
|
|
...page,
|
|
|
|
locale: page.localeCode,
|
|
|
|
editor: page.editorKey
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new WIKI.Error.PageNotFound()
|
|
|
|
}
|
2019-09-01 22:33:36 +00:00
|
|
|
},
|
2019-10-13 23:59:50 +00:00
|
|
|
/**
|
|
|
|
* FETCH TAGS
|
|
|
|
*/
|
2019-09-01 22:33:36 +00:00
|
|
|
async tags (obj, args, context, info) {
|
|
|
|
return WIKI.models.tags.query().orderBy('tag', 'asc')
|
2019-10-08 03:38:06 +00:00
|
|
|
},
|
2019-10-13 23:59:50 +00:00
|
|
|
/**
|
|
|
|
* FETCH PAGE TREE
|
|
|
|
*/
|
2019-10-08 03:38:06 +00:00
|
|
|
async tree (obj, args, context, info) {
|
|
|
|
let results = []
|
|
|
|
let conds = {
|
2020-01-05 20:04:16 +00:00
|
|
|
localeCode: args.locale
|
|
|
|
}
|
|
|
|
if (args.parent) {
|
|
|
|
conds.parent = (args.parent < 1) ? null : args.parent
|
|
|
|
} else if (args.path) {
|
|
|
|
// conds.parent = (args.parent < 1) ? null : args.parent
|
2019-10-08 03:38:06 +00:00
|
|
|
}
|
|
|
|
switch (args.mode) {
|
|
|
|
case 'FOLDERS':
|
|
|
|
conds.isFolder = true
|
|
|
|
results = await WIKI.models.knex('pageTree').where(conds)
|
|
|
|
break
|
|
|
|
case 'PAGES':
|
|
|
|
await WIKI.models.knex('pageTree').where(conds).andWhereNotNull('pageId')
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
results = await WIKI.models.knex('pageTree').where(conds)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return results.filter(r => {
|
|
|
|
return WIKI.auth.checkAccess(context.req.user, ['read:pages'], {
|
|
|
|
path: r.path,
|
|
|
|
locale: r.localeCode
|
|
|
|
})
|
|
|
|
}).map(r => ({
|
|
|
|
...r,
|
2019-10-12 04:55:43 +00:00
|
|
|
parent: r.parent || 0,
|
2019-10-08 03:38:06 +00:00
|
|
|
locale: r.localeCode
|
|
|
|
}))
|
2020-01-05 20:04:16 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* FETCH PAGE LINKS
|
|
|
|
*/
|
|
|
|
async links (obj, args, context, info) {
|
|
|
|
let results = []
|
|
|
|
|
|
|
|
results = await WIKI.models.knex('pages')
|
|
|
|
.column({ id: 'pages.id' }, { path: 'pages.path' }, 'title', { link: 'pageLinks.path' }, { locale: 'pageLinks.localeCode' })
|
|
|
|
.fullOuterJoin('pageLinks', 'pages.id', 'pageLinks.pageId')
|
|
|
|
.where({
|
|
|
|
'pages.localeCode': args.locale
|
|
|
|
})
|
|
|
|
|
|
|
|
return _.reduce(results, (result, val) => {
|
|
|
|
// -> Check if user has access to source and linked page
|
|
|
|
if (
|
|
|
|
!WIKI.auth.checkAccess(context.req.user, ['read:pages'], { path: val.path, locale: args.locale }) ||
|
|
|
|
!WIKI.auth.checkAccess(context.req.user, ['read:pages'], { path: val.link, locale: val.locale })
|
|
|
|
) {
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
const existingEntry = _.findIndex(result, ['id', val.id])
|
|
|
|
if (existingEntry >= 0) {
|
|
|
|
if (val.link) {
|
|
|
|
result[existingEntry].links.push(`${val.locale}/${val.link}`)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.push({
|
|
|
|
id: val.id,
|
|
|
|
title: val.title,
|
|
|
|
path: `${args.locale}/${val.path}`,
|
|
|
|
links: val.link ? [`${val.locale}/${val.link}`] : []
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}, [])
|
2019-04-07 22:10:44 +00:00
|
|
|
}
|
2018-05-20 22:50:51 +00:00
|
|
|
},
|
|
|
|
PageMutation: {
|
2019-10-13 23:59:50 +00:00
|
|
|
/**
|
|
|
|
* CREATE PAGE
|
|
|
|
*/
|
2018-07-22 04:29:39 +00:00
|
|
|
async create(obj, args, context) {
|
2019-10-13 23:59:50 +00:00
|
|
|
try {
|
|
|
|
const page = await WIKI.models.pages.createPage({
|
|
|
|
...args,
|
|
|
|
user: context.req.user
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Page created successfully.'),
|
|
|
|
page
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
2018-05-20 22:50:51 +00:00
|
|
|
}
|
|
|
|
},
|
2019-10-13 23:59:50 +00:00
|
|
|
/**
|
|
|
|
* UPDATE PAGE
|
|
|
|
*/
|
|
|
|
async update(obj, args, context) {
|
|
|
|
try {
|
|
|
|
const page = await WIKI.models.pages.updatePage({
|
|
|
|
...args,
|
|
|
|
user: context.req.user
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Page has been updated.'),
|
|
|
|
page
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
2018-05-20 22:50:51 +00:00
|
|
|
}
|
|
|
|
},
|
2019-10-13 23:59:50 +00:00
|
|
|
/**
|
|
|
|
* MOVE PAGE
|
|
|
|
*/
|
|
|
|
async move(obj, args, context) {
|
|
|
|
try {
|
|
|
|
await WIKI.models.pages.movePage({
|
|
|
|
...args,
|
|
|
|
user: context.req.user
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Page has been moved.')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* DELETE PAGE
|
|
|
|
*/
|
|
|
|
async delete(obj, args, context) {
|
|
|
|
try {
|
|
|
|
await WIKI.models.pages.deletePage({
|
|
|
|
...args,
|
|
|
|
user: context.req.user
|
|
|
|
})
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Page has been deleted.')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
2018-05-20 22:50:51 +00:00
|
|
|
}
|
2019-07-06 21:06:42 +00:00
|
|
|
},
|
2019-10-13 23:59:50 +00:00
|
|
|
/**
|
|
|
|
* FLUSH PAGE CACHE
|
|
|
|
*/
|
2019-07-06 21:06:42 +00:00
|
|
|
async flushCache(obj, args, context) {
|
|
|
|
try {
|
|
|
|
await WIKI.models.pages.flushCache()
|
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Pages Cache has been flushed successfully.')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
2019-07-13 03:56:44 +00:00
|
|
|
},
|
2019-10-13 23:59:50 +00:00
|
|
|
/**
|
|
|
|
* MIGRATE ALL PAGES FROM SOURCE LOCALE TO TARGET LOCALE
|
|
|
|
*/
|
2019-07-13 03:56:44 +00:00
|
|
|
async migrateToLocale(obj, args, context) {
|
|
|
|
try {
|
2019-07-14 20:12:26 +00:00
|
|
|
const count = await WIKI.models.pages.migrateToLocale(args)
|
2019-07-13 03:56:44 +00:00
|
|
|
return {
|
2019-07-14 20:12:26 +00:00
|
|
|
responseResult: graphHelper.generateSuccess('Migrated content to target locale successfully.'),
|
|
|
|
count
|
2019-07-13 03:56:44 +00:00
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
2019-10-13 23:59:50 +00:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* REBUILD TREE
|
|
|
|
*/
|
|
|
|
async rebuildTree(obj, args, context) {
|
|
|
|
try {
|
2019-10-14 02:35:42 +00:00
|
|
|
await WIKI.models.pages.rebuildTree()
|
2019-10-13 23:59:50 +00:00
|
|
|
return {
|
|
|
|
responseResult: graphHelper.generateSuccess('Page tree rebuilt successfully.')
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
return graphHelper.generateError(err)
|
|
|
|
}
|
2018-05-20 22:50:51 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Page: {
|
|
|
|
// comments(pg) {
|
|
|
|
// return pg.$relatedQuery('comments')
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
}
|