Noded/frontend#86 Store user bookmarks in their preferences
This commit is contained in:
parent
bebdd504dd
commit
2f3d94adf3
@ -26,9 +26,21 @@ class Menu extends Controller {
|
|||||||
.api()
|
.api()
|
||||||
}
|
}
|
||||||
|
|
||||||
const nodes = await this._build_menu_object(root_page, [], page_only)
|
const nodes = await this._build_menu_object(req.user, root_page, [], page_only)
|
||||||
const menu = []
|
const menu = []
|
||||||
|
|
||||||
|
if ( req.user.preferences?.bookmark_page_ids?.length ) {
|
||||||
|
menu.push({
|
||||||
|
id: 0,
|
||||||
|
name: 'My Bookmarks',
|
||||||
|
children: await this.get_bookmarks(req.user),
|
||||||
|
noDelete: true,
|
||||||
|
noChildren: true,
|
||||||
|
virtual: true,
|
||||||
|
type: PageType.Branch,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const virtual_page = {
|
const virtual_page = {
|
||||||
id: root_page.UUID,
|
id: root_page.UUID,
|
||||||
children: [...(await root_page.get_menu_items(page_only)), ...nodes],
|
children: [...(await root_page.get_menu_items(page_only)), ...nodes],
|
||||||
@ -57,9 +69,21 @@ class Menu extends Controller {
|
|||||||
|
|
||||||
// Build the "My Tree" option
|
// Build the "My Tree" option
|
||||||
const root_page = await req.user.get_root_page()
|
const root_page = await req.user.get_root_page()
|
||||||
const nodes = await this._build_menu_object(root_page, [], page_only)
|
const nodes = await this._build_menu_object(req.user, root_page, [], page_only)
|
||||||
const menu = []
|
const menu = []
|
||||||
|
|
||||||
|
if ( req.user.preferences?.bookmark_page_ids?.length ) {
|
||||||
|
menu.push({
|
||||||
|
id: 0,
|
||||||
|
name: 'My Bookmarks',
|
||||||
|
children: await this.get_bookmarks(req.user),
|
||||||
|
noDelete: true,
|
||||||
|
noChildren: true,
|
||||||
|
virtual: true,
|
||||||
|
type: PageType.Branch,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
menu.push({
|
menu.push({
|
||||||
id: 0,
|
id: 0,
|
||||||
name: 'My Info Tree',
|
name: 'My Info Tree',
|
||||||
@ -82,6 +106,7 @@ class Menu extends Controller {
|
|||||||
children: await this._build_secure_menu_object(tree, req.user),
|
children: await this._build_secure_menu_object(tree, req.user),
|
||||||
level: await tree.access_level_for(req.user),
|
level: await tree.access_level_for(req.user),
|
||||||
type: tree.PageType || PageType.Note,
|
type: tree.PageType || PageType.Note,
|
||||||
|
bookmark: (req.user.preferences.bookmark_page_ids || []).includes(tree.UUID),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,6 +121,7 @@ class Menu extends Controller {
|
|||||||
children: await this._build_secure_menu_object(tree, req.user),
|
children: await this._build_secure_menu_object(tree, req.user),
|
||||||
level: await tree.access_level_for(req.user),
|
level: await tree.access_level_for(req.user),
|
||||||
type: tree.PageType || PageType.Note,
|
type: tree.PageType || PageType.Note,
|
||||||
|
bookmark: (req.user.preferences.bookmark_page_ids || []).includes(tree.UUID),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,6 +136,7 @@ class Menu extends Controller {
|
|||||||
children: await this._build_secure_menu_object(tree, req.user),
|
children: await this._build_secure_menu_object(tree, req.user),
|
||||||
level: await tree.access_level_for(req.user),
|
level: await tree.access_level_for(req.user),
|
||||||
type: tree.PageType || PageType.Note,
|
type: tree.PageType || PageType.Note,
|
||||||
|
bookmark: (req.user.preferences.bookmark_page_ids || []).includes(tree.UUID),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -126,6 +153,24 @@ class Menu extends Controller {
|
|||||||
return res.api(menu)
|
return res.api(menu)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async get_bookmarks(user) {
|
||||||
|
const pages = await user.get_bookmarked_pages()
|
||||||
|
const items = []
|
||||||
|
|
||||||
|
for ( const page of pages ) {
|
||||||
|
items.push({
|
||||||
|
id: page.UUID,
|
||||||
|
name: page.is_shared() ? page.Name + ' ⁽ˢʰᵃʳᵉᵈ⁾' : page.Name,
|
||||||
|
shared: page.is_shared(),
|
||||||
|
bookmark: true,
|
||||||
|
type: page.PageType || PageType.Note,
|
||||||
|
children: [],
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return items
|
||||||
|
}
|
||||||
|
|
||||||
async move_node(req, res, next) {
|
async move_node(req, res, next) {
|
||||||
const Page = this.models.get('api:Page');
|
const Page = this.models.get('api:Page');
|
||||||
|
|
||||||
@ -181,7 +226,7 @@ class Menu extends Controller {
|
|||||||
return res.api();
|
return res.api();
|
||||||
}
|
}
|
||||||
|
|
||||||
async _build_menu_object(parent_node, arr= [], page_only = false) {
|
async _build_menu_object(user, parent_node, arr= [], page_only = false) {
|
||||||
const children = await this.models.get('api:Page').find({UUID: {$in: parent_node.ChildPageIds}})
|
const children = await this.models.get('api:Page').find({UUID: {$in: parent_node.ChildPageIds}})
|
||||||
if ( children ) {
|
if ( children ) {
|
||||||
for ( const child of children ) {
|
for ( const child of children ) {
|
||||||
@ -189,8 +234,9 @@ class Menu extends Controller {
|
|||||||
id: child.UUID,
|
id: child.UUID,
|
||||||
name: child.is_shared() ? child.Name + ' ⁽ˢʰᵃʳᵉᵈ⁾' : child.Name,
|
name: child.is_shared() ? child.Name + ' ⁽ˢʰᵃʳᵉᵈ⁾' : child.Name,
|
||||||
shared: child.is_shared(),
|
shared: child.is_shared(),
|
||||||
children: [...(await child.get_menu_items(page_only)), ...(await this._build_menu_object(child, [], page_only))],
|
children: [...(await child.get_menu_items(page_only)), ...(await this._build_menu_object(user, child, [], page_only))],
|
||||||
type: child.PageType || PageType.Note,
|
type: child.PageType || PageType.Note,
|
||||||
|
bookmark: (user.preferences.bookmark_page_ids || []).includes(child.UUID),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -209,6 +255,7 @@ class Menu extends Controller {
|
|||||||
children: await this._build_secure_menu_object(child, user),
|
children: await this._build_secure_menu_object(child, user),
|
||||||
level: await child.access_level_for(user),
|
level: await child.access_level_for(user),
|
||||||
type: child.PageType || PageType.Note,
|
type: child.PageType || PageType.Note,
|
||||||
|
bookmark: (user.preferences.bookmark_page_ids || []).includes(child.UUID),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,10 @@ class SessionController extends Controller {
|
|||||||
}
|
}
|
||||||
|
|
||||||
req.user.preferences.auto_prefetch = !!save_prefs.auto_prefetch
|
req.user.preferences.auto_prefetch = !!save_prefs.auto_prefetch
|
||||||
|
|
||||||
|
if ( Array.isArray(save_prefs.bookmark_page_ids) ) {
|
||||||
|
req.user.preferences.bookmark_page_ids = save_prefs.bookmark_page_ids
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await req.user.save()
|
await req.user.save()
|
||||||
@ -34,6 +38,7 @@ class SessionController extends Controller {
|
|||||||
username: user.uid,
|
username: user.uid,
|
||||||
preferences: user.preferences || {},
|
preferences: user.preferences || {},
|
||||||
is_public_user: user.is_public_user(),
|
is_public_user: user.is_public_user(),
|
||||||
|
bookmark_page_ids: user.preferences.bookmark_page_ids,
|
||||||
},
|
},
|
||||||
app: {
|
app: {
|
||||||
name: this.configs.get('app.name'),
|
name: this.configs.get('app.name'),
|
||||||
|
@ -18,6 +18,7 @@ class User extends AuthUser {
|
|||||||
dark_mode: { type: Boolean, default: false },
|
dark_mode: { type: Boolean, default: false },
|
||||||
auto_prefetch: { type: Boolean, default: false },
|
auto_prefetch: { type: Boolean, default: false },
|
||||||
default_page: String,
|
default_page: String,
|
||||||
|
bookmark_page_ids: [String],
|
||||||
},
|
},
|
||||||
}}
|
}}
|
||||||
}
|
}
|
||||||
@ -27,6 +28,20 @@ class User extends AuthUser {
|
|||||||
return Page.findOne({OrgUserId: this._id, ParentId: '0'})
|
return Page.findOne({OrgUserId: this._id, ParentId: '0'})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async get_bookmarked_pages() {
|
||||||
|
const Page = this.models.get('api:Page')
|
||||||
|
const pages = await Page.find({ UUID: { $in: this.preferences.bookmark_page_ids || [] }})
|
||||||
|
|
||||||
|
const visible = []
|
||||||
|
for ( const page of pages ) {
|
||||||
|
if ( await page.is_accessible_by(this) ) {
|
||||||
|
visible.push(page)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return visible
|
||||||
|
}
|
||||||
|
|
||||||
async get_accessible_pages() {
|
async get_accessible_pages() {
|
||||||
const Page = this.models.get('api:Page')
|
const Page = this.models.get('api:Page')
|
||||||
const user_page = await this.get_root_page()
|
const user_page = await this.get_root_page()
|
||||||
|
Loading…
Reference in New Issue
Block a user