2018-03-23 23:08:29 -04:00
|
|
|
"use strict";
|
|
|
|
|
2021-06-29 22:15:57 +02:00
|
|
|
const becca = require('../../becca/becca');
|
2021-05-11 22:00:16 +02:00
|
|
|
const SearchContext = require('../../services/search/search_context');
|
2019-03-20 22:28:54 +01:00
|
|
|
const log = require('../../services/log');
|
|
|
|
const scriptService = require('../../services/script');
|
2020-08-06 23:55:17 +02:00
|
|
|
const searchService = require('../../services/search/services/search');
|
2022-06-05 23:36:46 +02:00
|
|
|
const bulkActionService = require("../../services/bulk_actions");
|
2022-09-16 22:44:52 +02:00
|
|
|
const cls = require("../../services/cls");
|
2021-05-11 22:00:16 +02:00
|
|
|
const {formatAttrForSearch} = require("../../services/attribute_formatter");
|
2018-03-23 23:08:29 -04:00
|
|
|
|
2022-06-08 22:25:00 +02:00
|
|
|
function searchFromNoteInt(note) {
|
2022-07-10 15:52:02 +02:00
|
|
|
let searchResultNoteIds, highlightedTokens;
|
2019-03-20 22:28:54 +01:00
|
|
|
|
2021-01-26 22:22:17 +01:00
|
|
|
const searchScript = note.getRelationValue('searchScript');
|
|
|
|
const searchString = note.getLabelValue('searchString');
|
2019-03-20 22:28:54 +01:00
|
|
|
|
2021-01-26 22:22:17 +01:00
|
|
|
if (searchScript) {
|
2022-06-08 22:25:00 +02:00
|
|
|
searchResultNoteIds = searchFromRelation(note, 'searchScript');
|
2022-07-10 15:52:02 +02:00
|
|
|
highlightedTokens = [];
|
2021-01-26 22:22:17 +01:00
|
|
|
} else {
|
|
|
|
const searchContext = new SearchContext({
|
|
|
|
fastSearch: note.hasLabel('fastSearch'),
|
|
|
|
ancestorNoteId: note.getRelationValue('ancestor'),
|
2021-01-26 23:25:18 +01:00
|
|
|
ancestorDepth: note.getLabelValue('ancestorDepth'),
|
2021-01-26 22:22:17 +01:00
|
|
|
includeArchivedNotes: note.hasLabel('includeArchivedNotes'),
|
|
|
|
orderBy: note.getLabelValue('orderBy'),
|
|
|
|
orderDirection: note.getLabelValue('orderDirection'),
|
2021-02-13 23:52:52 +01:00
|
|
|
limit: note.getLabelValue('limit'),
|
2021-02-18 22:10:49 +01:00
|
|
|
debug: note.hasLabel('debug'),
|
2021-01-26 22:22:17 +01:00
|
|
|
fuzzyAttributeSearch: false
|
|
|
|
});
|
2020-09-14 20:00:36 +02:00
|
|
|
|
2021-04-17 20:52:46 +02:00
|
|
|
searchResultNoteIds = searchService.findResultsWithQuery(searchString, searchContext)
|
2021-01-26 22:22:17 +01:00
|
|
|
.map(sr => sr.noteId);
|
2022-07-10 15:52:02 +02:00
|
|
|
|
|
|
|
highlightedTokens = searchContext.highlightedTokens;
|
2019-03-20 22:28:54 +01:00
|
|
|
}
|
2021-01-26 22:22:17 +01:00
|
|
|
|
|
|
|
// we won't return search note's own noteId
|
|
|
|
// also don't allow root since that would force infinite cycle
|
2022-07-10 15:52:02 +02:00
|
|
|
return {
|
|
|
|
searchResultNoteIds: searchResultNoteIds.filter(resultNoteId => !['root', note.noteId].includes(resultNoteId)),
|
|
|
|
highlightedTokens
|
|
|
|
};
|
2021-01-20 20:31:24 +01:00
|
|
|
}
|
2019-03-20 22:28:54 +01:00
|
|
|
|
2022-07-10 15:52:02 +02:00
|
|
|
function searchFromNote(req) {
|
2021-05-02 11:23:58 +02:00
|
|
|
const note = becca.getNote(req.params.noteId);
|
2021-01-20 20:31:24 +01:00
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
return [404, `Note ${req.params.noteId} has not been found.`];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (note.isDeleted) {
|
2022-07-10 15:52:02 +02:00
|
|
|
// this can be triggered from recent changes, and it's harmless to return empty list rather than fail
|
2021-01-20 20:31:24 +01:00
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (note.type !== 'search') {
|
|
|
|
return [400, `Note ${req.params.noteId} is not a search note.`]
|
|
|
|
}
|
|
|
|
|
2022-07-10 15:52:02 +02:00
|
|
|
return searchFromNoteInt(note);
|
2019-03-20 22:28:54 +01:00
|
|
|
}
|
|
|
|
|
2022-06-08 22:25:00 +02:00
|
|
|
function searchAndExecute(req) {
|
2021-05-02 11:23:58 +02:00
|
|
|
const note = becca.getNote(req.params.noteId);
|
2021-01-20 20:31:24 +01:00
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
return [404, `Note ${req.params.noteId} has not been found.`];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (note.isDeleted) {
|
|
|
|
// this can be triggered from recent changes and it's harmless to return empty list rather than fail
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (note.type !== 'search') {
|
|
|
|
return [400, `Note ${req.params.noteId} is not a search note.`]
|
|
|
|
}
|
|
|
|
|
2022-07-10 15:52:02 +02:00
|
|
|
const {searchResultNoteIds} = searchFromNoteInt(note);
|
2021-01-20 20:31:24 +01:00
|
|
|
|
2022-06-05 23:36:46 +02:00
|
|
|
bulkActionService.executeActions(note, searchResultNoteIds);
|
2021-01-20 20:31:24 +01:00
|
|
|
}
|
|
|
|
|
2021-05-15 22:57:23 +02:00
|
|
|
function searchFromRelation(note, relationName) {
|
2020-06-20 12:31:38 +02:00
|
|
|
const scriptNote = note.getRelationTarget(relationName);
|
2019-03-20 22:28:54 +01:00
|
|
|
|
|
|
|
if (!scriptNote) {
|
|
|
|
log.info(`Search note's relation ${relationName} has not been found.`);
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!scriptNote.isJavaScript() || scriptNote.getScriptEnv() !== 'backend') {
|
|
|
|
log.info(`Note ${scriptNote.noteId} is not executable.`);
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-05-17 22:35:36 +02:00
|
|
|
if (!note.isContentAvailable()) {
|
2019-03-20 22:28:54 +01:00
|
|
|
log.info(`Note ${scriptNote.noteId} is not available outside of protected session.`);
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2021-05-15 22:57:23 +02:00
|
|
|
const result = scriptService.executeNote(scriptNote, { originEntity: note });
|
2019-03-20 22:28:54 +01:00
|
|
|
|
|
|
|
if (!Array.isArray(result)) {
|
|
|
|
log.info(`Result from ${scriptNote.noteId} is not an array.`);
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
// we expect either array of noteIds (strings) or notes, in that case we extract noteIds ourselves
|
|
|
|
return typeof result[0] === 'string' ? result : result.map(item => item.noteId);
|
|
|
|
}
|
|
|
|
|
2021-01-31 22:45:45 +01:00
|
|
|
function quickSearch(req) {
|
|
|
|
const {searchString} = req.params;
|
|
|
|
|
|
|
|
const searchContext = new SearchContext({
|
|
|
|
fastSearch: false,
|
|
|
|
includeArchivedNotes: false,
|
|
|
|
fuzzyAttributeSearch: false
|
|
|
|
});
|
|
|
|
|
2021-04-17 20:52:46 +02:00
|
|
|
return searchService.findResultsWithQuery(searchString, searchContext)
|
2021-01-31 22:45:45 +01:00
|
|
|
.map(sr => sr.noteId);
|
|
|
|
}
|
|
|
|
|
2021-03-28 00:07:38 +01:00
|
|
|
function search(req) {
|
|
|
|
const {searchString} = req.params;
|
|
|
|
|
|
|
|
const searchContext = new SearchContext({
|
|
|
|
fastSearch: false,
|
|
|
|
includeArchivedNotes: true,
|
2021-04-06 21:56:46 +02:00
|
|
|
fuzzyAttributeSearch: false,
|
|
|
|
ignoreHoistedNote: true
|
2021-03-28 00:07:38 +01:00
|
|
|
});
|
|
|
|
|
2021-04-17 20:52:46 +02:00
|
|
|
return searchService.findResultsWithQuery(searchString, searchContext)
|
2021-03-28 00:07:38 +01:00
|
|
|
.map(sr => sr.noteId);
|
|
|
|
}
|
|
|
|
|
2020-06-25 23:56:06 +02:00
|
|
|
function getRelatedNotes(req) {
|
|
|
|
const attr = req.body;
|
|
|
|
|
2020-09-13 22:23:03 +02:00
|
|
|
const searchSettings = {
|
2021-01-18 22:52:07 +01:00
|
|
|
fastSearch: true,
|
|
|
|
includeArchivedNotes: false,
|
2020-09-13 22:23:03 +02:00
|
|
|
fuzzyAttributeSearch: false
|
|
|
|
};
|
|
|
|
|
2021-04-17 20:52:46 +02:00
|
|
|
const matchingNameAndValue = searchService.findResultsWithQuery(formatAttrForSearch(attr, true), new SearchContext(searchSettings));
|
|
|
|
const matchingName = searchService.findResultsWithQuery(formatAttrForSearch(attr, false), new SearchContext(searchSettings));
|
2020-06-25 23:56:06 +02:00
|
|
|
|
|
|
|
const results = [];
|
|
|
|
|
2021-02-16 23:07:40 +01:00
|
|
|
const allResults = matchingNameAndValue.concat(matchingName);
|
|
|
|
|
2021-10-29 21:36:57 +02:00
|
|
|
const allResultNoteIds = new Set();
|
|
|
|
|
|
|
|
for (const record of allResults) {
|
|
|
|
allResultNoteIds.add(record.noteId);
|
|
|
|
}
|
|
|
|
|
2021-02-16 23:07:40 +01:00
|
|
|
for (const record of allResults) {
|
2020-06-25 23:56:06 +02:00
|
|
|
if (results.length >= 20) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (results.find(res => res.noteId === record.noteId)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.push(record);
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2021-10-29 21:36:57 +02:00
|
|
|
count: allResultNoteIds.size,
|
2020-06-25 23:56:06 +02:00
|
|
|
results
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-31 22:45:57 +02:00
|
|
|
function searchTemplates() {
|
2022-09-16 22:44:52 +02:00
|
|
|
const query = cls.getHoistedNoteId() === 'root'
|
|
|
|
? '#template'
|
|
|
|
: '#template OR #workspaceTemplate';
|
2022-05-31 22:45:57 +02:00
|
|
|
|
|
|
|
return searchService.searchNotes(query, {
|
|
|
|
includeArchivedNotes: true,
|
|
|
|
ignoreHoistedNote: false
|
|
|
|
}).map(note => note.noteId);
|
|
|
|
}
|
|
|
|
|
2018-03-30 17:29:13 -04:00
|
|
|
module.exports = {
|
2020-06-25 23:56:06 +02:00
|
|
|
searchFromNote,
|
2021-01-20 20:31:24 +01:00
|
|
|
searchAndExecute,
|
2021-01-31 22:45:45 +01:00
|
|
|
getRelatedNotes,
|
2021-03-28 00:07:38 +01:00
|
|
|
quickSearch,
|
2022-05-31 22:45:57 +02:00
|
|
|
search,
|
|
|
|
searchTemplates
|
2020-05-16 23:12:29 +02:00
|
|
|
};
|