2020-02-08 08:39:33 +00:00
|
|
|
const Controller = require('libflitter/controller/Controller')
|
2020-02-08 11:06:18 +00:00
|
|
|
const { ObjectId } = require("mongodb");
|
|
|
|
const Page = require("../../../models/api/Page.model")
|
2020-02-08 08:39:33 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Misc Controller
|
|
|
|
* -------------------------------------------------------------
|
|
|
|
* Put some description here!
|
|
|
|
*/
|
|
|
|
class Misc extends Controller {
|
2020-03-01 21:37:52 +00:00
|
|
|
#default_token_grants = ['database']
|
2020-02-08 08:39:33 +00:00
|
|
|
|
2020-03-01 21:37:52 +00:00
|
|
|
static get services() {
|
|
|
|
return [...super.services, 'models']
|
2020-02-08 08:39:33 +00:00
|
|
|
}
|
2020-03-01 21:37:52 +00:00
|
|
|
|
|
|
|
async get_token(req, res, next) {
|
|
|
|
const Token = this.models.get('api:Token')
|
|
|
|
const token = await Token.for_user(req.user)
|
|
|
|
return res.api(token.token)
|
|
|
|
}
|
|
|
|
|
2020-10-13 02:46:50 +00:00
|
|
|
async get_search(req, res, next) {
|
|
|
|
if ( !req.query.query ) {
|
|
|
|
return res.status(400)
|
|
|
|
.message('Missing query.')
|
|
|
|
.api()
|
|
|
|
}
|
|
|
|
|
|
|
|
const query = String(req.query.query)
|
2020-10-13 12:53:25 +00:00
|
|
|
const snip_length = 50
|
2020-10-13 02:46:50 +00:00
|
|
|
|
|
|
|
const Page = this.models.get('api:Page')
|
|
|
|
const Node = this.models.get('api:Node')
|
2020-10-13 14:24:28 +00:00
|
|
|
const Codium = this.models.get('api:Codium')
|
2020-10-13 15:23:58 +00:00
|
|
|
const Database = this.models.get('api:db:Database')
|
2020-10-14 18:59:25 +00:00
|
|
|
const FileGroup = this.models.get('api:FileGroup')
|
|
|
|
const File = this.models.get('upload::File')
|
2020-10-13 02:46:50 +00:00
|
|
|
|
|
|
|
const all_user_page_ids = (await req.user.get_accessible_pages()).map(x => x.UUID)
|
|
|
|
const results = []
|
|
|
|
|
|
|
|
const matching_pages = await Page.find({
|
|
|
|
UUID: { $in: all_user_page_ids },
|
|
|
|
Active: true,
|
|
|
|
$text: { $search: query },
|
|
|
|
})
|
|
|
|
|
|
|
|
for ( const page of matching_pages ) {
|
|
|
|
results.push({
|
|
|
|
title: page.Name,
|
2020-10-13 12:53:25 +00:00
|
|
|
short_title: `${page.Name.slice(0, snip_length)}${page.Name.length > snip_length ? '...' : ''}`,
|
2020-10-13 02:46:50 +00:00
|
|
|
type: 'page',
|
|
|
|
id: page.UUID,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const matching_nodes = await Node.find({
|
|
|
|
PageId: { $in: all_user_page_ids },
|
2020-10-16 17:31:51 +00:00
|
|
|
'Value.Mode': { $in: ['norm', 'markdown'] },
|
2020-10-13 02:46:50 +00:00
|
|
|
$text: { $search: query }
|
|
|
|
})
|
|
|
|
|
|
|
|
for ( const node of matching_nodes ) {
|
|
|
|
const page = await node.page
|
|
|
|
results.push({
|
|
|
|
title: node.Value.Value,
|
2020-10-13 12:53:25 +00:00
|
|
|
short_title: `${node.Value.Value.slice(0, snip_length)}${node.Value.Value.length > snip_length ? '...' : ''}`,
|
2020-10-16 17:31:51 +00:00
|
|
|
type: node.Value.Mode === 'norm' ? 'node' : 'markdown',
|
2020-10-13 02:46:50 +00:00
|
|
|
id: node.UUID,
|
|
|
|
associated: {
|
|
|
|
title: page.Name,
|
2020-10-13 12:53:25 +00:00
|
|
|
short_title: `${page.Name.slice(0, snip_length)}${page.Name.length > snip_length ? '...' : ''}`,
|
2020-10-13 02:46:50 +00:00
|
|
|
type: 'page',
|
|
|
|
id: page.UUID,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-13 14:24:28 +00:00
|
|
|
const matching_codiums = await Codium.find({
|
2020-10-13 15:23:58 +00:00
|
|
|
Active: true,
|
2020-10-13 14:24:28 +00:00
|
|
|
PageId: { $in: all_user_page_ids },
|
|
|
|
$text: { $search: query },
|
|
|
|
});
|
|
|
|
|
|
|
|
for ( const codium of matching_codiums ) {
|
|
|
|
const page = await codium.page
|
|
|
|
results.push({
|
|
|
|
title: codium.code,
|
|
|
|
short_title: `${codium.code.slice(0, snip_length)}${codium.code.length > snip_length ? '...' : ''}`,
|
|
|
|
type: 'code',
|
|
|
|
id: codium.NodeId,
|
|
|
|
associated: {
|
|
|
|
title: page.Name,
|
|
|
|
short_title: `${page.Name.slice(0, snip_length)}${page.Name.length > snip_length ? '...' : ''}`,
|
|
|
|
type: 'page',
|
|
|
|
id: page.UUID,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-13 15:23:58 +00:00
|
|
|
const matching_dbs = await Database.find({
|
|
|
|
Active: true,
|
|
|
|
PageId: { $in: all_user_page_ids },
|
|
|
|
$text: { $search: query },
|
|
|
|
})
|
|
|
|
|
|
|
|
for ( const db of matching_dbs ) {
|
|
|
|
const page = await db.page
|
|
|
|
results.push({
|
|
|
|
title: db.Name,
|
|
|
|
short_title: `${db.Name.slice(0, snip_length)}${db.Name.length > snip_length ? '...' : ''}`,
|
|
|
|
type: 'db',
|
|
|
|
id: db.NodeId,
|
|
|
|
associated: {
|
|
|
|
title: page.Name,
|
|
|
|
short_title: `${page.Name.slice(0, snip_length)}${page.Name.length > snip_length ? '...' : ''}`,
|
|
|
|
type: 'page',
|
|
|
|
id: page.UUID,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-14 18:59:25 +00:00
|
|
|
const unique = (value, index, self) => {
|
|
|
|
return self.indexOf(value) === index
|
|
|
|
}
|
|
|
|
|
|
|
|
const all_file_groups = await FileGroup.find({
|
|
|
|
PageId: { $in: all_user_page_ids },
|
|
|
|
})
|
|
|
|
|
|
|
|
let all_file_ids = []
|
|
|
|
const file_id_x_group = {}
|
|
|
|
for ( const group of all_file_groups ) {
|
|
|
|
group.FileIds.forEach(id => {
|
|
|
|
all_file_ids.push(id)
|
|
|
|
file_id_x_group[id] = group
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
const unique_file_ids = all_file_ids.filter(unique).map(x => File.ObjectId(x))
|
|
|
|
|
|
|
|
const matching_files = await File.find({
|
|
|
|
_id: { $in: unique_file_ids },
|
|
|
|
$text: { $search: query },
|
|
|
|
})
|
|
|
|
|
|
|
|
for ( const file of matching_files ) {
|
|
|
|
const group = file_id_x_group[file.id]
|
|
|
|
if ( group ) {
|
|
|
|
const page = await group.page
|
|
|
|
results.push({
|
|
|
|
title: file.original_name,
|
|
|
|
short_title: `${file.original_name.slice(0, snip_length)}${file.original_name.length > snip_length ? '...' : ''}`,
|
|
|
|
type: 'files',
|
|
|
|
id: group.NodeId,
|
|
|
|
associated: {
|
|
|
|
title: page.Name,
|
|
|
|
short_title: `${page.Name.slice(0, snip_length)}${page.Name.length > snip_length ? '...' : ''}`,
|
|
|
|
type: 'page',
|
|
|
|
id: page.UUID,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-13 02:46:50 +00:00
|
|
|
return res.api({results})
|
|
|
|
}
|
2020-02-08 08:39:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = exports = Misc
|