2018-04-18 12:26:42 +08:00
|
|
|
"use strict";
|
|
|
|
|
2020-05-17 05:12:29 +08:00
|
|
|
const noteCacheService = require('../../services/note_cache/note_cache.js');
|
2018-07-26 22:05:09 +08:00
|
|
|
const repository = require('../../services/repository');
|
2018-11-05 05:10:52 +08:00
|
|
|
const log = require('../../services/log');
|
2018-12-13 04:28:38 +08:00
|
|
|
const utils = require('../../services/utils');
|
|
|
|
const optionService = require('../../services/options');
|
2018-04-18 12:26:42 +08:00
|
|
|
|
|
|
|
async function getAutocomplete(req) {
|
2019-09-10 02:29:07 +08:00
|
|
|
const query = req.query.query.trim();
|
2019-03-15 03:21:27 +08:00
|
|
|
const activeNoteId = req.query.activeNoteId || 'none';
|
2018-04-18 12:26:42 +08:00
|
|
|
|
2018-07-26 22:05:09 +08:00
|
|
|
let results;
|
|
|
|
|
2018-11-05 05:10:52 +08:00
|
|
|
const timestampStarted = Date.now();
|
|
|
|
|
2019-09-10 02:29:07 +08:00
|
|
|
if (query.length === 0) {
|
2019-03-15 03:21:27 +08:00
|
|
|
results = await getRecentNotes(activeNoteId);
|
2018-07-26 22:05:09 +08:00
|
|
|
}
|
|
|
|
else {
|
2020-05-17 04:11:09 +08:00
|
|
|
results = await noteCacheService.findNotesForAutocomplete(query);
|
2018-07-26 22:05:09 +08:00
|
|
|
}
|
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`);
|
|
|
|
}
|
|
|
|
|
2018-11-07 16:35:29 +08:00
|
|
|
return results;
|
2018-04-18 12:26:42 +08:00
|
|
|
}
|
|
|
|
|
2019-03-15 03:21:27 +08:00
|
|
|
async function getRecentNotes(activeNoteId) {
|
2018-12-13 04:28:38 +08:00
|
|
|
let extraCondition = '';
|
|
|
|
|
|
|
|
const hoistedNoteId = await optionService.getOption('hoistedNoteId');
|
|
|
|
if (hoistedNoteId !== 'root') {
|
|
|
|
extraCondition = `AND recent_notes.notePath LIKE '%${utils.sanitizeSql(hoistedNoteId)}%'`;
|
|
|
|
}
|
|
|
|
|
2018-07-26 22:05:09 +08:00
|
|
|
const recentNotes = await repository.getEntities(`
|
|
|
|
SELECT
|
|
|
|
recent_notes.*
|
|
|
|
FROM
|
|
|
|
recent_notes
|
2019-05-22 03:47:28 +08:00
|
|
|
JOIN notes USING(noteId)
|
2018-07-26 22:05:09 +08:00
|
|
|
WHERE
|
|
|
|
recent_notes.isDeleted = 0
|
2019-05-22 03:47:28 +08:00
|
|
|
AND notes.isDeleted = 0
|
|
|
|
AND notes.noteId != ?
|
2018-12-13 04:28:38 +08:00
|
|
|
${extraCondition}
|
2018-07-26 22:05:09 +08:00
|
|
|
ORDER BY
|
2019-03-13 03:58:31 +08:00
|
|
|
utcDateCreated DESC
|
2019-03-15 03:21:27 +08:00
|
|
|
LIMIT 200`, [activeNoteId]);
|
2018-07-26 22:05:09 +08:00
|
|
|
|
|
|
|
return recentNotes.map(rn => {
|
2018-11-08 00:16:33 +08:00
|
|
|
const title = noteCacheService.getNoteTitleForPath(rn.notePath.split('/'));
|
|
|
|
|
2018-07-26 22:05:09 +08:00
|
|
|
return {
|
2020-05-17 04:11:09 +08:00
|
|
|
notePath: rn.notePath,
|
|
|
|
notePathTitle: title,
|
|
|
|
highlightedNotePathTitle: utils.escapeHtml(title)
|
2018-07-26 22:05:09 +08:00
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-04-18 12:26:42 +08:00
|
|
|
module.exports = {
|
|
|
|
getAutocomplete
|
2020-05-15 17:04:55 +08:00
|
|
|
};
|