2018-03-24 11:08:29 +08:00
|
|
|
"use strict";
|
|
|
|
|
2018-04-02 09:27:46 +08:00
|
|
|
const noteService = require('../../services/notes');
|
2019-03-21 05:28:54 +08:00
|
|
|
const repository = require('../../services/repository');
|
2018-06-06 07:12:52 +08:00
|
|
|
const noteCacheService = require('../../services/note_cache');
|
2019-03-21 05:28:54 +08:00
|
|
|
const log = require('../../services/log');
|
|
|
|
const scriptService = require('../../services/script');
|
2019-03-17 05:19:01 +08:00
|
|
|
const searchService = require('../../services/search');
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
async function searchNotes(req) {
|
2019-03-17 05:19:01 +08:00
|
|
|
const noteIds = await searchService.searchForNoteIds(req.params.searchString);
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2019-03-17 05:19:01 +08:00
|
|
|
return noteIds.map(noteCacheService.getNotePath).filter(res => !!res);
|
2018-03-31 05:29:13 +08:00
|
|
|
}
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
async function saveSearchToNote(req) {
|
2018-03-24 11:08:29 +08:00
|
|
|
const noteContent = {
|
|
|
|
searchString: req.params.searchString
|
|
|
|
};
|
|
|
|
|
2018-06-06 07:12:52 +08:00
|
|
|
const {note} = await noteService.createNote('root', req.params.searchString, noteContent, {
|
2018-03-24 11:08:29 +08:00
|
|
|
json: true,
|
2018-03-26 11:25:17 +08:00
|
|
|
type: 'search',
|
|
|
|
mime: "application/json"
|
2018-03-24 11:08:29 +08:00
|
|
|
});
|
|
|
|
|
2018-04-04 10:15:28 +08:00
|
|
|
return { noteId: note.noteId };
|
2018-03-31 05:29:13 +08:00
|
|
|
}
|
2018-03-24 11:08:29 +08:00
|
|
|
|
2019-03-21 05:28:54 +08:00
|
|
|
async function searchFromNote(req) {
|
|
|
|
const note = await repository.getNote(req.params.noteId);
|
|
|
|
|
|
|
|
if (!note) {
|
|
|
|
return [404, `Note ${req.params.noteId} has not been found.`];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (note.type !== 'search') {
|
|
|
|
return [400, '`Note ${req.params.noteId} is not search note.`']
|
|
|
|
}
|
|
|
|
|
|
|
|
const json = await note.getJsonContent();
|
|
|
|
|
|
|
|
if (!json || !json.searchString) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
let noteIds;
|
|
|
|
|
|
|
|
if (json.searchString.startsWith('=')) {
|
|
|
|
const relationName = json.searchString.substr(1).trim();
|
|
|
|
|
|
|
|
noteIds = await searchFromRelation(note, relationName);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
noteIds = searchService.searchForNoteIds(json.searchString);
|
|
|
|
}
|
|
|
|
|
|
|
|
// we won't return search note's own noteId
|
|
|
|
noteIds = noteIds.filter(noteId => noteId !== note.noteId);
|
|
|
|
|
|
|
|
return noteIds.map(noteCacheService.getNotePath).filter(res => !!res);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function searchFromRelation(note, relationName) {
|
|
|
|
const scriptNote = await note.getRelationTarget(relationName);
|
|
|
|
|
|
|
|
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 [];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!note.isContentAvailable) {
|
|
|
|
log.info(`Note ${scriptNote.noteId} is not available outside of protected session.`);
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
const result = await scriptService.executeNote(scriptNote, { originEntity: note });
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2018-03-31 05:29:13 +08:00
|
|
|
module.exports = {
|
|
|
|
searchNotes,
|
2019-03-21 05:28:54 +08:00
|
|
|
saveSearchToNote,
|
|
|
|
searchFromNote
|
2018-03-31 05:29:13 +08:00
|
|
|
};
|