2020-02-08 18:36:42 +00:00
|
|
|
const Controller = require('libflitter/controller/Controller')
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Menu Controller
|
|
|
|
* -------------------------------------------------------------
|
|
|
|
* Put some description here!
|
|
|
|
*/
|
|
|
|
class Menu extends Controller {
|
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'models']
|
|
|
|
}
|
|
|
|
|
|
|
|
async get_items(req, res) {
|
2020-02-14 06:11:16 +00:00
|
|
|
const Page = this.models.get('api:Page')
|
|
|
|
|
2020-02-11 06:38:36 +00:00
|
|
|
// Build the "My Tree" option
|
2020-02-08 18:36:42 +00:00
|
|
|
const root_page = await req.user.get_root_page()
|
|
|
|
const nodes = await this._build_menu_object(root_page)
|
2020-02-11 06:38:36 +00:00
|
|
|
const menu = []
|
|
|
|
|
|
|
|
menu.push({
|
|
|
|
id: 0,
|
|
|
|
name: 'My Info Tree',
|
|
|
|
children: nodes,
|
|
|
|
noDelete: true,
|
|
|
|
virtual: true,
|
2020-10-13 14:58:36 +00:00
|
|
|
type: 'branch',
|
2020-02-11 06:38:36 +00:00
|
|
|
})
|
|
|
|
|
2020-02-14 06:11:16 +00:00
|
|
|
// Get view only shared trees
|
|
|
|
const view_only_trees = await Page.find({ shared_users_view: req.user._id })
|
|
|
|
const view_only_nodes = []
|
|
|
|
for ( const tree of view_only_trees ) {
|
|
|
|
if ( !(await tree.is_accessible_by(req.user)) ) continue
|
|
|
|
view_only_nodes.push({
|
|
|
|
id: tree.UUID,
|
|
|
|
name: tree.Name,
|
|
|
|
children: await this._build_secure_menu_object(tree, req.user),
|
|
|
|
level: await tree.access_level_for(req.user),
|
2020-10-13 14:58:36 +00:00
|
|
|
type: 'page',
|
2020-02-14 06:11:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get update, view shared trees
|
|
|
|
const update_trees = await Page.find({ shared_users_update: req.user._id })
|
|
|
|
const update_nodes = []
|
|
|
|
for ( const tree of update_trees ) {
|
|
|
|
if ( !(await tree.is_accessible_by(req.user)) ) continue
|
|
|
|
update_nodes.push({
|
|
|
|
id: tree.UUID,
|
|
|
|
name: tree.Name,
|
|
|
|
children: await this._build_secure_menu_object(tree, req.user),
|
|
|
|
level: await tree.access_level_for(req.user),
|
2020-10-13 14:58:36 +00:00
|
|
|
type: 'page',
|
2020-02-14 06:11:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get update, view, manage shared trees
|
|
|
|
const manage_trees = await Page.find({ shared_users_manage: req.user._id })
|
|
|
|
const manage_nodes = []
|
|
|
|
for ( const tree of manage_trees ) {
|
|
|
|
if ( !(await tree.is_accessible_by(req.user)) ) continue
|
|
|
|
manage_nodes.push({
|
|
|
|
id: tree.UUID,
|
|
|
|
name: tree.Name,
|
|
|
|
children: await this._build_secure_menu_object(tree, req.user),
|
|
|
|
level: await tree.access_level_for(req.user),
|
2020-10-13 14:58:36 +00:00
|
|
|
type: 'page',
|
2020-02-14 06:11:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
menu.push({
|
|
|
|
id: 0,
|
|
|
|
name: 'Trees Shared With Me',
|
|
|
|
children: [...view_only_nodes, ...update_nodes, ...manage_nodes],
|
|
|
|
noDelete: true,
|
2020-10-13 15:29:19 +00:00
|
|
|
noChildren: true,
|
2020-02-14 06:11:16 +00:00
|
|
|
virtual: true,
|
2020-10-13 14:58:36 +00:00
|
|
|
type: 'branch',
|
2020-02-14 06:11:16 +00:00
|
|
|
})
|
|
|
|
|
2020-02-11 06:38:36 +00:00
|
|
|
return res.api(menu)
|
2020-02-08 18:36:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async _build_menu_object(parent_node, arr= []) {
|
|
|
|
const children = await this.models.get('api:Page').find({UUID: {$in: parent_node.ChildPageIds}})
|
|
|
|
if ( children ) {
|
|
|
|
for ( const child of children ) {
|
|
|
|
arr.push({
|
|
|
|
id: child.UUID,
|
2020-02-14 06:11:16 +00:00
|
|
|
name: child.is_shared() ? child.Name + ' ⁽ˢʰᵃʳᵉᵈ⁾' : child.Name,
|
|
|
|
shared: child.is_shared(),
|
2020-10-13 14:58:36 +00:00
|
|
|
children: [...(await child.get_menu_items()), ...(await this._build_menu_object(child))],
|
|
|
|
type: 'page',
|
2020-02-08 18:36:42 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2020-02-14 06:11:16 +00:00
|
|
|
async _build_secure_menu_object(parent_node, user, arr = []) {
|
|
|
|
const children = await parent_node.childPages
|
|
|
|
if ( children ) {
|
|
|
|
for ( const child of children ) {
|
|
|
|
if ( !(await child.is_accessible_by(user)) ) continue
|
|
|
|
arr.push({
|
|
|
|
id: child.UUID,
|
|
|
|
name: child.Name,
|
|
|
|
children: await this._build_secure_menu_object(child, user),
|
|
|
|
level: await child.access_level_for(user),
|
2020-10-13 14:58:36 +00:00
|
|
|
type: 'page',
|
2020-02-14 06:11:16 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return arr
|
|
|
|
}
|
|
|
|
|
2020-02-08 18:36:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = Menu
|