| 
									
										
										
										
											2019-04-14 12:18:52 +02:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const dateNoteService = require('../../services/date_notes'); | 
					
						
							| 
									
										
										
										
											2019-09-08 16:30:33 +02:00
										 |  |  | const sql = require('../../services/sql'); | 
					
						
							| 
									
										
										
										
											2020-05-08 23:39:46 +02:00
										 |  |  | const dateUtils = require('../../services/date_utils'); | 
					
						
							|  |  |  | const noteService = require('../../services/notes'); | 
					
						
							| 
									
										
										
										
											2020-11-27 22:21:13 +01:00
										 |  |  | const attributeService = require('../../services/attributes'); | 
					
						
							| 
									
										
										
										
											2021-02-26 23:33:22 +01:00
										 |  |  | const cls = require('../../services/cls'); | 
					
						
							|  |  |  | const repository = require('../../services/repository'); | 
					
						
							| 
									
										
										
										
											2020-11-27 22:21:13 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | function getInboxNote(req) { | 
					
						
							|  |  |  |     return attributeService.getNoteWithLabel('inbox') | 
					
						
							|  |  |  |         || dateNoteService.getDateNote(req.params.date); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-04-14 12:18:52 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function getDateNote(req) { | 
					
						
							|  |  |  |     return dateNoteService.getDateNote(req.params.date); | 
					
						
							| 
									
										
										
										
											2019-04-14 12:18:52 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function getMonthNote(req) { | 
					
						
							|  |  |  |     return dateNoteService.getMonthNote(req.params.month); | 
					
						
							| 
									
										
										
										
											2019-04-14 12:18:52 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function getYearNote(req) { | 
					
						
							|  |  |  |     return dateNoteService.getYearNote(req.params.year); | 
					
						
							| 
									
										
										
										
											2019-04-14 12:18:52 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function getDateNotesForMonth(req) { | 
					
						
							| 
									
										
										
										
											2019-09-08 16:30:33 +02:00
										 |  |  |     const month = req.params.month; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return sql.getMap(`
 | 
					
						
							|  |  |  |         SELECT | 
					
						
							|  |  |  |             attr.value AS date, | 
					
						
							|  |  |  |             notes.noteId | 
					
						
							|  |  |  |         FROM notes | 
					
						
							|  |  |  |         JOIN attributes attr USING(noteId) | 
					
						
							|  |  |  |         WHERE notes.isDeleted = 0 | 
					
						
							|  |  |  |             AND attr.isDeleted = 0 | 
					
						
							|  |  |  |             AND attr.type = 'label' | 
					
						
							|  |  |  |             AND attr.name = 'dateNote' | 
					
						
							|  |  |  |             AND attr.value LIKE '${month}%'`);
 | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | function createSqlConsole() { | 
					
						
							| 
									
										
										
										
											2020-05-08 23:39:46 +02:00
										 |  |  |     const today = dateUtils.localNowDate(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-07 09:19:27 +01:00
										 |  |  |     const sqlConsoleHome = | 
					
						
							|  |  |  |         attributeService.getNoteWithLabel('sqlConsoleHome') | 
					
						
							|  |  |  |         || dateNoteService.getDateNote(today); | 
					
						
							| 
									
										
										
										
											2020-05-08 23:39:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     const {note} = noteService.createNewNote({ | 
					
						
							| 
									
										
										
										
											2020-12-07 09:19:27 +01:00
										 |  |  |         parentNoteId: sqlConsoleHome.noteId, | 
					
						
							| 
									
										
										
										
											2020-05-08 23:39:46 +02:00
										 |  |  |         title: 'SQL Console', | 
					
						
							|  |  |  |         content: "SELECT title, isDeleted, isProtected FROM notes WHERE noteId = ''\n\n\n\n", | 
					
						
							|  |  |  |         type: 'code', | 
					
						
							|  |  |  |         mime: 'text/x-sqlite;schema=trilium' | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  |     note.setLabel("sqlConsole", today); | 
					
						
							| 
									
										
										
										
											2020-05-08 23:39:46 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return note; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-25 20:02:49 +01:00
										 |  |  | function createSearchNote(req) { | 
					
						
							|  |  |  |     const params = req.body; | 
					
						
							|  |  |  |     const searchString = params.searchString || ""; | 
					
						
							| 
									
										
										
										
											2021-02-26 23:33:22 +01:00
										 |  |  |     let ancestorNoteId = params.ancestorNoteId; | 
					
						
							| 
									
										
										
										
											2021-02-25 20:02:49 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-27 21:19:54 +01:00
										 |  |  |     const hoistedNote = cls.getHoistedNoteId() && cls.getHoistedNoteId() !== 'root' | 
					
						
							|  |  |  |         ? repository.getNote(cls.getHoistedNoteId()) | 
					
						
							|  |  |  |         : null; | 
					
						
							| 
									
										
										
										
											2020-10-25 23:02:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-27 21:18:10 +01:00
										 |  |  |     let searchHome; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (hoistedNote) { | 
					
						
							|  |  |  |         ([searchHome] = hoistedNote.getDescendantNotesWithLabel('hoistedSearchHome')); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!searchHome) { | 
					
						
							|  |  |  |         const today = dateUtils.localNowDate(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         searchHome = attributeService.getNoteWithLabel('searchHome') | 
					
						
							|  |  |  |                   || dateNoteService.getDateNote(today); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-25 23:02:12 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-27 21:19:54 +01:00
										 |  |  |     if (hoistedNote) { | 
					
						
							| 
									
										
										
										
											2021-02-26 23:33:22 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         if (!hoistedNote.getDescendantNoteIds().includes(searchHome.noteId)) { | 
					
						
							|  |  |  |             // otherwise the note would be saved outside of the hoisted context which is weird
 | 
					
						
							|  |  |  |             searchHome = hoistedNote; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         if (!ancestorNoteId) { | 
					
						
							| 
									
										
										
										
											2021-02-27 21:18:10 +01:00
										 |  |  |             ancestorNoteId = hoistedNote.noteId; | 
					
						
							| 
									
										
										
										
											2021-02-26 23:33:22 +01:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-25 23:02:12 +01:00
										 |  |  |     const {note} = noteService.createNewNote({ | 
					
						
							| 
									
										
										
										
											2020-12-04 22:57:54 +01:00
										 |  |  |         parentNoteId: searchHome.noteId, | 
					
						
							| 
									
										
										
										
											2021-02-25 20:02:49 +01:00
										 |  |  |         title: 'Search: ' + searchString, | 
					
						
							| 
									
										
										
										
											2020-10-25 23:02:12 +01:00
										 |  |  |         content: "", | 
					
						
							|  |  |  |         type: 'search', | 
					
						
							|  |  |  |         mime: 'application/json' | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-25 20:02:49 +01:00
										 |  |  |     note.setLabel('searchString', searchString); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-26 23:33:22 +01:00
										 |  |  |     if (ancestorNoteId) { | 
					
						
							|  |  |  |         note.setRelation('ancestor', ancestorNoteId); | 
					
						
							| 
									
										
										
										
											2021-02-25 20:02:49 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-25 23:02:12 +01:00
										 |  |  |     return note; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-04-14 12:18:52 +02:00
										 |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2020-11-27 22:21:13 +01:00
										 |  |  |     getInboxNote, | 
					
						
							| 
									
										
										
										
											2019-04-14 12:18:52 +02:00
										 |  |  |     getDateNote, | 
					
						
							|  |  |  |     getMonthNote, | 
					
						
							| 
									
										
										
										
											2019-09-08 16:30:33 +02:00
										 |  |  |     getYearNote, | 
					
						
							| 
									
										
										
										
											2020-05-08 23:39:46 +02:00
										 |  |  |     getDateNotesForMonth, | 
					
						
							| 
									
										
										
										
											2020-10-25 23:02:12 +01:00
										 |  |  |     createSqlConsole, | 
					
						
							|  |  |  |     createSearchNote | 
					
						
							| 
									
										
										
										
											2020-06-20 12:31:38 +02:00
										 |  |  | }; |