From 3fe87259e22651e6c4c761e6de8b7ab127f656f9 Mon Sep 17 00:00:00 2001 From: zadam Date: Fri, 26 Feb 2021 23:33:22 +0100 Subject: [PATCH] if search note would end up outside of current hoisting, save it under the hoisted note, closes #1694 --- src/routes/api/date_notes.js | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/src/routes/api/date_notes.js b/src/routes/api/date_notes.js index 72e8cc2fd..b48232ad4 100644 --- a/src/routes/api/date_notes.js +++ b/src/routes/api/date_notes.js @@ -5,6 +5,8 @@ const sql = require('../../services/sql'); const dateUtils = require('../../services/date_utils'); const noteService = require('../../services/notes'); const attributeService = require('../../services/attributes'); +const cls = require('../../services/cls'); +const repository = require('../../services/repository'); function getInboxNote(req) { return attributeService.getNoteWithLabel('inbox') @@ -62,13 +64,27 @@ function createSqlConsole() { function createSearchNote(req) { const params = req.body; const searchString = params.searchString || ""; + let ancestorNoteId = params.ancestorNoteId; const today = dateUtils.localNowDate(); - const searchHome = + let searchHome = attributeService.getNoteWithLabel('searchHome') || dateNoteService.getDateNote(today); + if (cls.getHoistedNoteId() && cls.getHoistedNoteId() !== 'root') { + const hoistedNote = repository.getNote(cls.getHoistedNoteId()); + + if (!hoistedNote.getDescendantNoteIds().includes(searchHome.noteId)) { + // otherwise the note would be saved outside of the hoisted context which is weird + searchHome = hoistedNote; + } + + if (!ancestorNoteId) { + ancestorNoteId = cls.getHoistedNoteId(); + } + } + const {note} = noteService.createNewNote({ parentNoteId: searchHome.noteId, title: 'Search: ' + searchString, @@ -79,8 +95,8 @@ function createSearchNote(req) { note.setLabel('searchString', searchString); - if (params.ancestorNoteId) { - note.setRelation('ancestor', params.ancestorNoteId); + if (ancestorNoteId) { + note.setRelation('ancestor', ancestorNoteId); } return note;