trilium/src/routes/api/autocomplete.js

59 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-04-18 12:26:42 +08:00
"use strict";
2018-06-06 07:12:52 +08:00
const noteCacheService = require('../../services/note_cache');
const repository = require('../../services/repository');
2018-11-05 05:10:52 +08:00
const log = require('../../services/log');
2018-04-18 12:26:42 +08:00
async function getAutocomplete(req) {
const query = req.query.query;
const currentNoteId = req.query.currentNoteId || 'none';
2018-04-18 12:26:42 +08:00
let results;
2018-11-05 05:10:52 +08:00
const timestampStarted = Date.now();
if (query.trim().length === 0) {
results = await getRecentNotes(currentNoteId);
}
else {
results = noteCacheService.findNotes(query);
}
2018-04-18 12:26:42 +08:00
2018-11-05 05:10:52 +08:00
const msTaken = Date.now() - timestampStarted;
if (msTaken >= 100) {
log.info(`Slow autocomplete took ${msTaken}ms`);
}
return results;
2018-04-18 12:26:42 +08:00
}
async function getRecentNotes(currentNoteId) {
const recentNotes = await repository.getEntities(`
SELECT
recent_notes.*
FROM
recent_notes
JOIN branches USING(branchId)
WHERE
recent_notes.isDeleted = 0
AND branches.isDeleted = 0
AND branches.noteId != ?
ORDER BY
dateCreated DESC
LIMIT 200`, [currentNoteId]);
return recentNotes.map(rn => {
2018-11-08 00:16:33 +08:00
const title = noteCacheService.getNoteTitleForPath(rn.notePath.split('/'));
return {
path: rn.notePath,
2018-11-08 00:16:33 +08:00
title: title,
highlighted: title
};
});
}
2018-04-18 12:26:42 +08:00
module.exports = {
getAutocomplete
};