Notes/src/routes/api/autocomplete.js

76 lines
1.9 KiB
JavaScript
Raw Normal View History

2018-04-18 00:26:42 -04:00
"use strict";
2021-05-17 22:09:49 +02:00
const beccaService = require('../../becca/becca_service.js');
2020-07-21 00:01:07 +02:00
const searchService = require('../../services/search/services/search.js');
2018-11-04 22:10:52 +01:00
const log = require('../../services/log');
const utils = require('../../services/utils');
2020-11-23 23:11:21 +01:00
const cls = require('../../services/cls');
2021-05-17 22:09:49 +02:00
const becca = require("../../becca/becca.js");
2018-04-18 00:26:42 -04:00
2020-06-20 12:31:38 +02:00
function getAutocomplete(req) {
2019-09-09 20:29:07 +02:00
const query = req.query.query.trim();
const activeNoteId = req.query.activeNoteId || 'none';
2018-04-18 00:26:42 -04:00
let results;
2018-11-04 22:10:52 +01:00
const timestampStarted = Date.now();
2019-09-09 20:29:07 +02:00
if (query.length === 0) {
2020-06-20 12:31:38 +02:00
results = getRecentNotes(activeNoteId);
}
else {
2020-06-20 12:31:38 +02:00
results = searchService.searchNotesForAutocomplete(query);
}
2018-04-18 00:26:42 -04:00
2018-11-04 22:10:52 +01:00
const msTaken = Date.now() - timestampStarted;
if (msTaken >= 100) {
log.info(`Slow autocomplete took ${msTaken}ms`);
}
return results;
2018-04-18 00:26:42 -04:00
}
2020-06-20 12:31:38 +02:00
function getRecentNotes(activeNoteId) {
let extraCondition = '';
const params = [activeNoteId];
2020-11-23 23:11:21 +01:00
const hoistedNoteId = cls.getHoistedNoteId();
if (hoistedNoteId !== 'root') {
extraCondition = `AND recent_notes.notePath LIKE ?`;
2020-11-24 22:32:22 +01:00
params.push('%' + hoistedNoteId + '%');
}
2021-05-02 19:59:16 +02:00
const recentNotes = becca.getRecentNotesFromQuery(`
SELECT
recent_notes.*
FROM
recent_notes
JOIN notes USING(noteId)
WHERE
2021-02-13 21:52:31 +01:00
notes.isDeleted = 0
AND notes.noteId != ?
${extraCondition}
ORDER BY
2019-03-12 20:58:31 +01:00
utcDateCreated DESC
LIMIT 200`, params);
return recentNotes.map(rn => {
const notePathArray = rn.notePath.split('/');
2021-04-16 23:00:08 +02:00
const noteTitle = beccaService.getNoteTitle(notePathArray[notePathArray.length - 1]);
const notePathTitle = beccaService.getNoteTitleForPath(notePathArray);
2018-11-07 17:16:33 +01:00
return {
2020-05-16 22:11:09 +02:00
notePath: rn.notePath,
noteTitle,
notePathTitle,
highlightedNotePathTitle: utils.escapeHtml(notePathTitle)
};
});
}
2018-04-18 00:26:42 -04:00
module.exports = {
getAutocomplete
};