Notes/src/services/search/expressions/note_cache_flat_text.js

142 lines
4.6 KiB
JavaScript
Raw Normal View History

2020-05-17 09:48:24 +02:00
"use strict";
2020-05-22 09:38:30 +02:00
const Expression = require('./expression');
2020-05-17 10:11:19 +02:00
const NoteSet = require('../note_set');
const noteCache = require('../../note_cache/note_cache');
2020-08-06 23:55:17 +02:00
class NoteCacheFlatTextExp extends Expression {
2020-05-16 23:12:29 +02:00
constructor(tokens) {
2020-05-22 09:38:30 +02:00
super();
2020-05-16 23:12:29 +02:00
this.tokens = tokens;
}
execute(inputNoteSet, executionContext) {
2020-05-20 00:03:33 +02:00
// has deps on SQL which breaks unit test so needs to be dynamically required
const noteCacheService = require('../../note_cache/note_cache_service');
2020-05-16 23:12:29 +02:00
const resultNoteSet = new NoteSet();
function searchDownThePath(note, tokens, path) {
if (tokens.length === 0) {
const retPath = noteCacheService.getSomePath(note, path);
if (retPath) {
const noteId = retPath[retPath.length - 1];
executionContext.noteIdToNotePath[noteId] = retPath;
resultNoteSet.add(noteCache.notes[noteId]);
}
return;
}
if (!note.parents.length === 0 || note.noteId === 'root') {
return;
}
const foundAttrTokens = [];
for (const token of tokens) {
if (note.type.includes(token) || note.mime.includes(token)) {
foundAttrTokens.push(token);
}
}
for (const attribute of note.ownedAttributes) {
for (const token of tokens) {
if (attribute.name.toLowerCase().includes(token)
|| attribute.value.toLowerCase().includes(token)) {
foundAttrTokens.push(token);
}
}
}
for (const parentNote of note.parents) {
const title = noteCacheService.getNoteTitle(note.noteId, parentNote.noteId).toLowerCase();
const foundTokens = foundAttrTokens.slice();
for (const token of tokens) {
if (title.includes(token)) {
foundTokens.push(token);
}
}
if (foundTokens.length > 0) {
const remainingTokens = tokens.filter(token => !foundTokens.includes(token));
searchDownThePath(parentNote, remainingTokens, path.concat([note.noteId]));
}
else {
searchDownThePath(parentNote, tokens, path.concat([note.noteId]));
}
}
}
const candidateNotes = this.getCandidateNotes(inputNoteSet);
2020-05-16 23:12:29 +02:00
for (const note of candidateNotes) {
// autocomplete should be able to find notes by their noteIds as well (only leafs)
2020-08-06 23:55:17 +02:00
if (this.tokens.length === 1 && note.noteId.toLowerCase() === this.tokens[0]) {
searchDownThePath(note, [], []);
2020-05-16 23:12:29 +02:00
continue;
}
const foundAttrTokens = [];
for (const token of this.tokens) {
if (note.type.includes(token) || note.mime.includes(token)) {
foundAttrTokens.push(token);
}
2020-08-30 23:19:55 +02:00
for (const attribute of note.ownedAttributes) {
if (attribute.name.includes(token) || attribute.value.includes(token)) {
2020-05-16 23:12:29 +02:00
foundAttrTokens.push(token);
}
}
}
for (const parentNote of note.parents) {
2020-05-17 10:11:19 +02:00
const title = noteCacheService.getNoteTitle(note.noteId, parentNote.noteId).toLowerCase();
2020-05-16 23:12:29 +02:00
const foundTokens = foundAttrTokens.slice();
for (const token of this.tokens) {
if (title.includes(token)) {
foundTokens.push(token);
}
}
if (foundTokens.length > 0) {
const remainingTokens = this.tokens.filter(token => !foundTokens.includes(token));
searchDownThePath(parentNote, remainingTokens, [note.noteId]);
2020-05-16 23:12:29 +02:00
}
}
}
return resultNoteSet;
}
/**
* Returns noteIds which have at least one matching tokens
*
* @param {NoteSet} noteSet
* @return {String[]}
*/
getCandidateNotes(noteSet) {
const candidateNotes = [];
for (const note of noteSet.notes) {
for (const token of this.tokens) {
if (note.flatText.includes(token)) {
candidateNotes.push(note);
break;
}
}
}
return candidateNotes;
}
}
2020-05-17 09:48:24 +02:00
2020-08-06 23:55:17 +02:00
module.exports = NoteCacheFlatTextExp;