backend/app/controllers/api/v1/Misc.controller.js
garrettmills 604753b3ff
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing
Include code snippets in full-text search (#6)
2020-10-13 09:24:28 -05:00

103 lines
3.2 KiB
JavaScript

const Controller = require('libflitter/controller/Controller')
const { ObjectId } = require("mongodb");
const Page = require("../../../models/api/Page.model")
/*
* Misc Controller
* -------------------------------------------------------------
* Put some description here!
*/
class Misc extends Controller {
#default_token_grants = ['database']
static get services() {
return [...super.services, 'models']
}
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)
}
async get_search(req, res, next) {
if ( !req.query.query ) {
return res.status(400)
.message('Missing query.')
.api()
}
const query = String(req.query.query)
const snip_length = 50
const Page = this.models.get('api:Page')
const Node = this.models.get('api:Node')
const Codium = this.models.get('api:Codium')
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,
short_title: `${page.Name.slice(0, snip_length)}${page.Name.length > snip_length ? '...' : ''}`,
type: 'page',
id: page.UUID,
})
}
const matching_nodes = await Node.find({
PageId: { $in: all_user_page_ids },
'Value.Mode': 'norm',
$text: { $search: query }
})
for ( const node of matching_nodes ) {
const page = await node.page
results.push({
title: node.Value.Value,
short_title: `${node.Value.Value.slice(0, snip_length)}${node.Value.Value.length > snip_length ? '...' : ''}`,
type: 'node',
id: node.UUID,
associated: {
title: page.Name,
short_title: `${page.Name.slice(0, snip_length)}${page.Name.length > snip_length ? '...' : ''}`,
type: 'page',
id: page.UUID,
}
})
}
const matching_codiums = await Codium.find({
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,
}
})
}
return res.api({results})
}
}
module.exports = exports = Misc