Include uploaded files in search results endpoint
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone Build is passing

This commit is contained in:
Garrett Mills 2020-10-14 13:59:25 -05:00
parent 9beb0d2c3a
commit 4686e9d37c
Signed by: garrettmills
GPG Key ID: D2BF5FBA8298F246
3 changed files with 52 additions and 0 deletions

View File

@ -46,6 +46,9 @@ class IonicUnit extends Unit {
const codiums = this.scaffold.collection('api_Codium')
await codiums.createIndex({ code: 'text' })
const files = this.scaffold.collection('upload__File')
await files.createIndex({ original_name: 'text' })
}
}

View File

@ -34,6 +34,8 @@ class Misc extends Controller {
const Node = this.models.get('api:Node')
const Codium = this.models.get('api:Codium')
const Database = this.models.get('api:db:Database')
const FileGroup = this.models.get('api:FileGroup')
const File = this.models.get('upload::File')
const all_user_page_ids = (await req.user.get_accessible_pages()).map(x => x.UUID)
const results = []
@ -119,6 +121,49 @@ class Misc extends Controller {
})
}
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,
}
})
}
}
return res.api({results})
}
}

View File

@ -22,6 +22,10 @@ class FileGroup extends Model {
}
// Static and instance methods can go here
get page() {
const Page = require('./Page.model')
return this.belongs_to_one(Page, 'PageId', 'UUID')
}
}
module.exports = exports = FileGroup