2020-02-08 12:57:38 +00:00
|
|
|
const Controller = require('libflitter/controller/Controller')
|
|
|
|
const PageModel = require('../../../models/api/Page.model')
|
2020-02-08 17:16:04 +00:00
|
|
|
const Node = require('../../../models/api/Node.model')
|
2020-11-17 04:49:19 +00:00
|
|
|
const { PageType } = require('../../../enum')
|
2020-02-08 12:57:38 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Page Controller
|
|
|
|
* -------------------------------------------------------------
|
|
|
|
* Put some description here!
|
|
|
|
*/
|
|
|
|
class Page extends Controller {
|
2020-11-03 02:22:24 +00:00
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'models']
|
|
|
|
}
|
|
|
|
|
2020-11-06 18:42:52 +00:00
|
|
|
async revert_version(req, res, next) {
|
2020-11-13 03:26:36 +00:00
|
|
|
const { page } = req.form
|
2020-11-06 18:42:52 +00:00
|
|
|
const level = await page.access_level_for(req.user)
|
|
|
|
|
|
|
|
if ( !req.body.version_num ) {
|
|
|
|
return res.status(400)
|
|
|
|
.message('Missing required field: version_num')
|
|
|
|
.api()
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !page.has_version(req.body.version_num) ) {
|
|
|
|
return res.status(400)
|
|
|
|
.message('Invalid version_num')
|
|
|
|
.api()
|
|
|
|
}
|
|
|
|
|
|
|
|
const reverted_page = await page.revert_to_version(req.body.version_num, req.user.id)
|
|
|
|
await reverted_page.save()
|
|
|
|
|
|
|
|
const data = reverted_page.toJSON()
|
|
|
|
delete data.version_archive
|
|
|
|
data.level = level
|
|
|
|
data._id = reverted_page.id
|
|
|
|
return res.api(data)
|
|
|
|
}
|
|
|
|
|
2020-11-03 02:22:24 +00:00
|
|
|
async get_page_versions(req, res, next) {
|
|
|
|
const User = this.models.get('auth:User')
|
2020-11-13 03:26:36 +00:00
|
|
|
const { page } = req.form
|
2020-11-03 02:22:24 +00:00
|
|
|
|
|
|
|
const versions = page.version_archive.map(version_data => {
|
|
|
|
return {
|
|
|
|
current_version: Number(version_data.version_num) === Number(page.version_num),
|
|
|
|
version_num: version_data.version_num,
|
|
|
|
version_user_id: version_data.version_user_id,
|
|
|
|
version_message: version_data.version_message,
|
|
|
|
version_UUID: version_data.version_UUID,
|
2020-11-03 03:47:17 +00:00
|
|
|
version_create_date: version_data.version_create_date,
|
2020-11-03 02:22:24 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
for ( const version of versions ) {
|
|
|
|
version.user_display = (await User.findById(version.version_user_id))?.uid || 'Unknown User'
|
|
|
|
}
|
|
|
|
|
|
|
|
return res.api(versions.reverse())
|
|
|
|
}
|
|
|
|
|
2020-02-08 12:57:38 +00:00
|
|
|
async get_page(req, res) {
|
|
|
|
const user = req.user
|
2020-11-13 03:26:36 +00:00
|
|
|
let { page } = req.form
|
2020-02-08 12:57:38 +00:00
|
|
|
|
2020-11-03 02:04:10 +00:00
|
|
|
const level = await page.access_level_for(req.user)
|
2020-02-08 12:57:38 +00:00
|
|
|
|
2020-11-03 02:04:10 +00:00
|
|
|
const version_num = req.body.version || req.query.version
|
|
|
|
if ( version_num ) {
|
|
|
|
if ( page.has_version(version_num) ) {
|
|
|
|
page = await page.as_version(version_num)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const data = page.toJSON()
|
|
|
|
delete data.version_archive
|
|
|
|
data.level = level
|
|
|
|
data._id = page.id
|
|
|
|
return res.api(data)
|
2020-02-08 12:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async save_page(req, res) {
|
|
|
|
const PageId = req.params.PageId
|
|
|
|
|
|
|
|
let page;
|
2020-11-13 03:26:36 +00:00
|
|
|
if ( req.form.page ) {
|
|
|
|
page = req.form.page
|
|
|
|
} else if ( PageId ) {
|
2020-02-08 12:57:38 +00:00
|
|
|
page = await PageModel.findOne({UUID: PageId})
|
|
|
|
if ( !page ) return res.status(404).message('Page not found with that ID.').api({})
|
2020-10-29 03:55:04 +00:00
|
|
|
if ( !(await page.is_accessible_by(req.user, 'update')) ) return res.security.deny()
|
2020-02-08 12:57:38 +00:00
|
|
|
} else {
|
|
|
|
page = new PageModel
|
|
|
|
page.CreatedUserId = req.user.id
|
2020-02-08 17:16:04 +00:00
|
|
|
page.OrgUserId = req.user._id
|
2020-02-08 12:57:38 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 04:44:54 +00:00
|
|
|
let save_message
|
2020-11-02 17:59:57 +00:00
|
|
|
|
2020-02-08 12:57:38 +00:00
|
|
|
if ( !req.body.Name ) return res.status(400).message('Missing required: Name').api({})
|
2020-11-02 17:59:57 +00:00
|
|
|
|
|
|
|
if ( page.Name !== req.body.Name ) {
|
|
|
|
page.Name = req.body.Name
|
|
|
|
save_message = `Changed page name to ${page.Name}`
|
|
|
|
}
|
2020-02-08 12:57:38 +00:00
|
|
|
|
|
|
|
if ( 'IsPublic' in req.body ) {
|
2020-11-02 17:59:57 +00:00
|
|
|
if ( Boolean(page.IsPublic) !== Boolean(req.body.IsPublic) ) {
|
|
|
|
save_message = `Made page ${req.body.IsPublic ? 'public' : 'private'}`
|
|
|
|
}
|
|
|
|
|
2020-02-08 12:57:38 +00:00
|
|
|
page.IsPublic = !!req.body.IsPublic
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'IsVisibleInMenu' in req.body ) {
|
2020-11-02 17:59:57 +00:00
|
|
|
if ( Boolean(page.IsVisibleInMenu) !== Boolean(req.body.IsVisibleInMenu) ) {
|
|
|
|
save_message = `Made page ${req.body.IsVisibleInMenu ? 'visible' : 'hidden'} in menu`
|
|
|
|
}
|
|
|
|
|
2020-02-08 12:57:38 +00:00
|
|
|
page.IsVisibleInMenu = !!req.body.IsVisibleInMenu
|
|
|
|
}
|
|
|
|
|
2020-11-23 16:22:04 +00:00
|
|
|
if ( 'AdditionalData' in req.body ) {
|
|
|
|
page.AdditionalData = req.body.AdditionalData;
|
|
|
|
}
|
|
|
|
|
2020-02-14 06:11:16 +00:00
|
|
|
/*let parent;
|
2020-02-08 12:57:38 +00:00
|
|
|
if ( !req.body.ParentId ) return res.status(400).message('Missing required: ParentId').api({})
|
|
|
|
else {
|
|
|
|
parent = await PageModel.findOne({UUID: req.body.ParentId})
|
|
|
|
if ( !parent ) return res.status(404).message('Parent page not found with that ID.').api({})
|
2020-02-14 06:11:16 +00:00
|
|
|
if ( !(await parent.is_accessible_by(req.user, 'update')) ) return req.security.kickout()
|
|
|
|
}*/ // TODO re-implement this to account for sharing
|
2020-02-08 12:57:38 +00:00
|
|
|
|
|
|
|
page.UpdatedAt = new Date
|
2020-02-08 17:16:04 +00:00
|
|
|
page.UpdateUserId = req.user._id
|
2020-11-03 04:44:54 +00:00
|
|
|
if ( save_message ) {
|
|
|
|
await page.version_save(save_message, req.user.id)
|
|
|
|
} else {
|
|
|
|
await page.save()
|
|
|
|
}
|
2020-02-08 17:16:04 +00:00
|
|
|
return res.api(page)
|
|
|
|
}
|
|
|
|
|
|
|
|
async get_nodes(req, res) {
|
2020-11-13 03:26:36 +00:00
|
|
|
let { page } = req.form
|
2020-02-08 17:16:04 +00:00
|
|
|
|
2020-11-03 02:04:10 +00:00
|
|
|
const version_num = req.body.version || req.query.version
|
|
|
|
if ( version_num ) {
|
|
|
|
if ( page.has_version(version_num) ) {
|
|
|
|
page = await page.as_version(version_num)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const nodes = await page.nodes
|
2020-02-08 17:16:04 +00:00
|
|
|
const assoc_nodes = {}
|
|
|
|
nodes.forEach(node => {
|
|
|
|
assoc_nodes[node.UUID] = node
|
|
|
|
})
|
|
|
|
|
2020-11-03 02:04:10 +00:00
|
|
|
const return_nodes = []
|
2020-02-08 17:16:04 +00:00
|
|
|
for ( const uuid of page.NodeIds ) {
|
|
|
|
return_nodes.push(assoc_nodes[uuid])
|
|
|
|
}
|
2020-11-03 02:04:10 +00:00
|
|
|
return res.api(return_nodes)
|
2020-02-08 17:16:04 +00:00
|
|
|
}
|
|
|
|
|
2020-10-14 14:23:44 +00:00
|
|
|
async save_node_to_page(req, res) {
|
2020-11-13 03:26:36 +00:00
|
|
|
const { page } = req.form
|
2020-10-14 14:23:44 +00:00
|
|
|
|
|
|
|
const nodes = await Node.find({PageId: page.UUID})
|
|
|
|
const assoc_nodes = {}
|
|
|
|
nodes.forEach(node => {
|
|
|
|
assoc_nodes[node.UUID] = node
|
|
|
|
})
|
|
|
|
|
|
|
|
if ( !req.body.nodeData ) {
|
|
|
|
return res.status(400).message('Missing nodeData body').api()
|
|
|
|
}
|
|
|
|
|
|
|
|
const node = req.body.nodeData;
|
|
|
|
if ( node.UUID && assoc_nodes[node.UUID] ) {
|
|
|
|
assoc_nodes[node.UUID].update_from_raw(node)
|
|
|
|
assoc_nodes[node.UUID].UpdatedAt = new Date
|
|
|
|
assoc_nodes[node.UUID].UpdateUserId = req.user._id
|
2020-11-02 18:31:05 +00:00
|
|
|
await assoc_nodes[node.UUID].version_save(`Updated in page`, req.user.id)
|
2020-10-14 14:23:44 +00:00
|
|
|
return res.api(assoc_nodes[node.UUID])
|
|
|
|
} else {
|
|
|
|
const node_obj = new Node({
|
|
|
|
Type: node.Type,
|
|
|
|
Value: node.Value,
|
2020-11-15 16:59:03 +00:00
|
|
|
PageId: page.UUID,
|
2020-10-14 14:23:44 +00:00
|
|
|
CreatedUserId: req.user._id,
|
|
|
|
UpdateUserId: req.user._id,
|
|
|
|
})
|
2020-10-29 03:55:04 +00:00
|
|
|
|
|
|
|
if ( node.UUID ) {
|
|
|
|
const existingUUID = await Node.findOne({ UUID: node.UUID })
|
|
|
|
if ( !existingUUID ) {
|
|
|
|
node_obj.UUID = node.UUID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 18:31:05 +00:00
|
|
|
await node_obj.version_save(`Added to page "${page.Name}"`)
|
2020-10-14 14:23:44 +00:00
|
|
|
|
|
|
|
page.NodeIds.push(node_obj.UUID);
|
2020-11-02 18:10:52 +00:00
|
|
|
await page.version_save(`Added ${node_obj.Type.split('_')[0]} node to page`, req.user.id);
|
2020-10-14 14:23:44 +00:00
|
|
|
return res.api(node_obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-08 17:16:04 +00:00
|
|
|
async save_nodes(req, res) {
|
2020-11-13 03:26:36 +00:00
|
|
|
const { page } = req.form
|
2020-02-08 17:16:04 +00:00
|
|
|
|
|
|
|
const nodes = await Node.find({PageId: page.UUID})
|
|
|
|
const assoc_nodes = {}
|
|
|
|
nodes.forEach(node => {
|
|
|
|
assoc_nodes[node.UUID] = node
|
|
|
|
})
|
|
|
|
|
|
|
|
if ( !Array.isArray(req.body) ) return res.status(400).message('Invalid request body. Should be array of nodes.').api({})
|
|
|
|
|
|
|
|
const updated_node_ids = []
|
|
|
|
for ( let node of req.body ) {
|
|
|
|
if ( node.UUID && Object.keys(assoc_nodes).includes(node.UUID) ) updated_node_ids.push(node.UUID)
|
|
|
|
}
|
|
|
|
|
|
|
|
const updated_nodes = []
|
|
|
|
for ( let node of req.body ) {
|
|
|
|
if ( node.UUID && assoc_nodes[node.UUID] ) {
|
|
|
|
assoc_nodes[node.UUID].update_from_raw(node)
|
|
|
|
assoc_nodes[node.UUID].UpdatedAt = new Date
|
|
|
|
assoc_nodes[node.UUID].UpdateUserId = req.user._id
|
|
|
|
updated_nodes.push(assoc_nodes[node.UUID])
|
|
|
|
} else {
|
|
|
|
const node_obj = new Node({
|
|
|
|
Type: node.Type,
|
|
|
|
Value: node.Value,
|
2020-11-15 16:59:03 +00:00
|
|
|
PageId: page.UUID,
|
2020-02-08 17:16:04 +00:00
|
|
|
CreatedUserId: req.user._id,
|
|
|
|
UpdateUserId: req.user._id,
|
|
|
|
})
|
|
|
|
|
|
|
|
updated_nodes.push(node_obj)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// console.log('updated nodes', updated_nodes)
|
|
|
|
|
|
|
|
let delete_nodes = nodes
|
|
|
|
for ( const node of updated_nodes ) {
|
2020-11-03 04:44:54 +00:00
|
|
|
await node.version_save(`Contents saved in page "${page.Name}"`, req.user.id)
|
2020-02-08 17:16:04 +00:00
|
|
|
delete_nodes = delete_nodes.filter(n => {
|
|
|
|
return n.UUID !== node.UUID
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
for ( const node of delete_nodes ) {
|
|
|
|
await node.delete()
|
|
|
|
}
|
|
|
|
|
|
|
|
page.NodeIds = updated_nodes.map(x => x.UUID)
|
2020-11-03 04:44:54 +00:00
|
|
|
await page.version_save('Saved page contents', req.user.id)
|
2020-02-08 17:16:04 +00:00
|
|
|
res.api(updated_nodes)
|
2020-02-08 12:57:38 +00:00
|
|
|
}
|
2020-02-08 21:27:53 +00:00
|
|
|
|
|
|
|
async create_top_level(req, res) {
|
|
|
|
if ( !req.body.name ) {
|
|
|
|
return res.status(400).message('Missing required field: name').api({})
|
|
|
|
}
|
|
|
|
|
|
|
|
const root_page = await req.user.get_root_page()
|
2020-11-17 04:49:19 +00:00
|
|
|
const page_type = req.body.pageType || PageType.Note
|
|
|
|
|
|
|
|
if ( !PageType._isValid(page_type) ) {
|
|
|
|
return res.status(400)
|
|
|
|
.message('Invalid PageType.')
|
|
|
|
.api()
|
|
|
|
}
|
2020-02-08 21:27:53 +00:00
|
|
|
|
|
|
|
const new_page = new PageModel({
|
|
|
|
Name: req.body.name,
|
|
|
|
OrgUserId: req.user._id,
|
|
|
|
ParentId: root_page.UUID,
|
|
|
|
CreatedUserId: req.user.id,
|
2020-11-17 04:49:19 +00:00
|
|
|
UpdateUserId: req.user.id,
|
|
|
|
PageType: page_type,
|
2020-02-08 21:27:53 +00:00
|
|
|
})
|
|
|
|
|
2020-10-29 03:55:04 +00:00
|
|
|
if ( req.body.UUID ) {
|
|
|
|
const existingUUID = await PageModel.findOne({UUID: req.body.UUID})
|
|
|
|
if ( !existingUUID ) {
|
|
|
|
new_page.UUID = req.body.UUID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 18:10:52 +00:00
|
|
|
await new_page.version_save('Created new top-level page', req.user.id)
|
2020-02-08 21:27:53 +00:00
|
|
|
|
|
|
|
root_page.ChildPageIds.push(new_page.UUID)
|
2020-11-02 18:10:52 +00:00
|
|
|
await root_page.version_save(`Added new top-level page "${new_page.Name}"`, req.user.id)
|
2020-02-08 21:27:53 +00:00
|
|
|
|
|
|
|
req.user.allow(`page:${new_page.UUID}`)
|
|
|
|
await req.user.save()
|
|
|
|
|
2020-10-29 03:55:04 +00:00
|
|
|
if ( !req.body.omit_starter ) {
|
|
|
|
const starter_node = new Node({
|
|
|
|
Type: 'paragraph',
|
|
|
|
Value: {
|
|
|
|
Value: 'Double-click to edit...',
|
|
|
|
},
|
|
|
|
PageId: new_page.UUID,
|
|
|
|
CreatedUserId: req.user.id,
|
|
|
|
UpdateUserId: req.user.id
|
|
|
|
})
|
2020-02-08 21:27:53 +00:00
|
|
|
|
2020-11-02 18:31:05 +00:00
|
|
|
await starter_node.version_save(`Added to page "${new_page.Name}"`, req.user.id)
|
2020-10-29 03:55:04 +00:00
|
|
|
new_page.NodeIds.push(starter_node.UUID)
|
2020-11-02 18:10:52 +00:00
|
|
|
await new_page.version_save('Added paragraph node to page', req.user.id)
|
2020-10-29 03:55:04 +00:00
|
|
|
}
|
2020-02-08 21:27:53 +00:00
|
|
|
|
|
|
|
return res.api(new_page)
|
|
|
|
}
|
|
|
|
|
|
|
|
async create_child(req, res) {
|
|
|
|
if ( !req.body.name ) {
|
|
|
|
return res.status(400).message('Missing required field: name').api({})
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !req.body.parentId ) {
|
|
|
|
return res.status(400).message('Missing required field: parentId').api({})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the parent
|
|
|
|
const parent = await PageModel.findOne({UUID: req.body.parentId})
|
|
|
|
if ( !parent ) {
|
|
|
|
return res.status(404).message('Unable to find parent with that ID.').api({})
|
2020-02-14 06:11:16 +00:00
|
|
|
} else if ( !(await parent.is_accessible_by(req.user, 'manage')) ) {
|
2020-02-08 21:27:53 +00:00
|
|
|
return res.security.deny()
|
|
|
|
}
|
|
|
|
|
2020-11-17 04:49:19 +00:00
|
|
|
const page_type = req.body.pageType || PageType.Note
|
|
|
|
|
|
|
|
if ( !PageType._isValid(page_type) ) {
|
|
|
|
return res.status(400)
|
|
|
|
.message('Invalid PageType.')
|
|
|
|
.api()
|
|
|
|
}
|
|
|
|
|
2020-02-08 21:27:53 +00:00
|
|
|
const new_page = new PageModel({
|
|
|
|
Name: req.body.name,
|
|
|
|
OrgUserId: req.user._id,
|
|
|
|
ParentId: parent.UUID,
|
|
|
|
CreatedUserId: req.user.id,
|
|
|
|
UpdateUserId: req.user.id,
|
2020-11-17 04:49:19 +00:00
|
|
|
PageType: page_type,
|
2020-02-08 21:27:53 +00:00
|
|
|
})
|
|
|
|
|
2020-10-29 03:55:04 +00:00
|
|
|
if ( req.body.UUID ) {
|
|
|
|
const existingUUID = await PageModel.findOne({UUID: req.body.UUID})
|
|
|
|
if ( !existingUUID ) {
|
|
|
|
new_page.UUID = req.body.UUID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-02 18:10:52 +00:00
|
|
|
await new_page.version_save(`Created new page as child of "${parent.Name}"`, req.user.id)
|
2020-02-08 21:27:53 +00:00
|
|
|
|
|
|
|
parent.ChildPageIds.push(new_page.UUID)
|
2020-11-02 18:10:52 +00:00
|
|
|
await parent.version_save(`Created new child page "${new_page.Name}"`, req.user.id)
|
2020-02-08 21:27:53 +00:00
|
|
|
|
|
|
|
req.user.allow(`page:${new_page.UUID}`)
|
|
|
|
await req.user.save()
|
|
|
|
|
2020-10-29 03:55:04 +00:00
|
|
|
if ( !req.body.omit_starter ) {
|
|
|
|
const starter_node = new Node({
|
|
|
|
Type: 'paragraph',
|
|
|
|
Value: {
|
|
|
|
Value: 'Click to edit...',
|
|
|
|
},
|
|
|
|
PageId: new_page.UUID,
|
|
|
|
CreatedUserId: req.user.id,
|
|
|
|
UpdateUserId: req.user.id
|
|
|
|
})
|
2020-02-08 21:27:53 +00:00
|
|
|
|
2020-11-02 18:31:05 +00:00
|
|
|
await starter_node.version_save(`Added to page "${new_page.Name}"`, req.user.id)
|
2020-10-29 03:55:04 +00:00
|
|
|
new_page.NodeIds.push(starter_node.UUID)
|
2020-11-02 18:10:52 +00:00
|
|
|
await new_page.version_save('Added paragraph node to page', req.user.id)
|
2020-10-29 03:55:04 +00:00
|
|
|
}
|
2020-02-08 21:27:53 +00:00
|
|
|
|
|
|
|
return res.api(new_page)
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete_page(req, res) {
|
2020-11-13 03:26:36 +00:00
|
|
|
const { page } = req.form
|
|
|
|
if ( page.ParentId === '0' ) return req.security.kickout()
|
2020-02-08 21:27:53 +00:00
|
|
|
|
|
|
|
page.Active = false
|
|
|
|
page.DeletedAt = new Date
|
2020-11-02 18:10:52 +00:00
|
|
|
await page.version_save('Deleted page', req.user.id)
|
2020-02-08 21:27:53 +00:00
|
|
|
return res.api({})
|
|
|
|
}
|
2020-02-08 12:57:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = Page
|