mirror of
https://github.com/zadam/trilium.git
synced 2025-03-01 14:22:32 +01:00
refactoring of special notes route to a service
This commit is contained in:
parent
2d82da32d4
commit
06bcfe1aee
@ -2,34 +2,12 @@
|
|||||||
|
|
||||||
const dateNoteService = require('../../services/date_notes');
|
const dateNoteService = require('../../services/date_notes');
|
||||||
const sql = require('../../services/sql');
|
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 cls = require('../../services/cls');
|
||||||
|
const specialNotesService = require('../../services/special_notes');
|
||||||
const becca = require('../../becca/becca');
|
const becca = require('../../becca/becca');
|
||||||
|
|
||||||
function getInboxNote(req) {
|
function getInboxNote(req) {
|
||||||
const hoistedNote = getHoistedNote();
|
return specialNotesService.getInboxNote(req.params.date);
|
||||||
|
|
||||||
let inbox;
|
|
||||||
|
|
||||||
if (!hoistedNote.isRoot()) {
|
|
||||||
inbox = hoistedNote.searchNoteInSubtree('#hoistedInbox');
|
|
||||||
|
|
||||||
if (!inbox) {
|
|
||||||
inbox = hoistedNote.searchNoteInSubtree('#inbox');
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!inbox) {
|
|
||||||
inbox = hoistedNote;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
inbox = attributeService.getNoteWithLabel('inbox')
|
|
||||||
|| dateNoteService.getDateNote(req.params.date);
|
|
||||||
}
|
|
||||||
|
|
||||||
return inbox;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getDateNote(req) {
|
function getDateNote(req) {
|
||||||
@ -61,156 +39,23 @@ function getDateNotesForMonth(req) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function saveSqlConsole(req) {
|
function saveSqlConsole(req) {
|
||||||
const sqlConsoleNote = becca.getNote(req.body.sqlConsoleNoteId);
|
return specialNotesService.saveSqlConsole(req.body.sqlConsoleNoteId);
|
||||||
const today = dateUtils.localNowDate();
|
|
||||||
|
|
||||||
const sqlConsoleHome =
|
|
||||||
attributeService.getNoteWithLabel('sqlConsoleHome')
|
|
||||||
|| dateNoteService.getDateNote(today);
|
|
||||||
|
|
||||||
return sqlConsoleNote.cloneTo(sqlConsoleHome.noteId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createSqlConsole() {
|
function createSqlConsole() {
|
||||||
const {note} = noteService.createNewNote({
|
return specialNotesService.createSqlConsole();
|
||||||
parentNoteId: getSqlConsoleRoot().noteId,
|
|
||||||
title: 'SQL Console',
|
|
||||||
content: "SELECT title, isDeleted, isProtected FROM notes WHERE noteId = ''\n\n\n\n",
|
|
||||||
type: 'code',
|
|
||||||
mime: 'text/x-sqlite;schema=trilium'
|
|
||||||
});
|
|
||||||
|
|
||||||
note.setLabel("sqlConsole", dateUtils.localNowDate());
|
|
||||||
|
|
||||||
return note;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getHiddenRoot() {
|
|
||||||
let hidden = becca.getNote('hidden');
|
|
||||||
|
|
||||||
if (!hidden) {
|
|
||||||
hidden = noteService.createNewNote({
|
|
||||||
noteId: 'hidden',
|
|
||||||
title: 'hidden',
|
|
||||||
type: 'text',
|
|
||||||
content: '',
|
|
||||||
parentNoteId: 'root'
|
|
||||||
}).note;
|
|
||||||
|
|
||||||
// isInheritable: false means that this notePath is automatically not preffered but at the same time
|
|
||||||
// the flag is not inherited to the children
|
|
||||||
hidden.addLabel('archived', "", false);
|
|
||||||
}
|
|
||||||
|
|
||||||
return hidden;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getSearchRoot() {
|
|
||||||
let searchRoot = becca.getNote('search');
|
|
||||||
|
|
||||||
if (!searchRoot) {
|
|
||||||
searchRoot = noteService.createNewNote({
|
|
||||||
noteId: 'search',
|
|
||||||
title: 'search',
|
|
||||||
type: 'text',
|
|
||||||
content: '',
|
|
||||||
parentNoteId: getHiddenRoot().noteId
|
|
||||||
}).note;
|
|
||||||
}
|
|
||||||
|
|
||||||
return searchRoot;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getSpecialNoteRoot() {
|
|
||||||
let specialNoteRoot = becca.getNote('special');
|
|
||||||
|
|
||||||
if (!specialNoteRoot) {
|
|
||||||
specialNoteRoot = noteService.createNewNote({
|
|
||||||
noteId: 'special',
|
|
||||||
title: 'special',
|
|
||||||
type: 'text',
|
|
||||||
content: '',
|
|
||||||
parentNoteId: getHiddenRoot().noteId
|
|
||||||
}).note;
|
|
||||||
}
|
|
||||||
|
|
||||||
return specialNoteRoot;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getGlobalLinkMapNote() {
|
|
||||||
let globalLinkMapNote = becca.getNote('global-link-map');
|
|
||||||
|
|
||||||
if (!globalLinkMapNote) {
|
|
||||||
globalLinkMapNote = noteService.createNewNote({
|
|
||||||
noteId: 'global-link-map',
|
|
||||||
title: 'global-link-map',
|
|
||||||
type: 'global-link-map',
|
|
||||||
content: '',
|
|
||||||
parentNoteId: getSpecialNoteRoot().noteId
|
|
||||||
}).note;
|
|
||||||
}
|
|
||||||
|
|
||||||
return globalLinkMapNote;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getSqlConsoleRoot() {
|
|
||||||
let sqlConsoleRoot = becca.getNote('sqlconsole');
|
|
||||||
|
|
||||||
if (!sqlConsoleRoot) {
|
|
||||||
sqlConsoleRoot = noteService.createNewNote({
|
|
||||||
noteId: 'sqlconsole',
|
|
||||||
title: 'SQL Console',
|
|
||||||
type: 'text',
|
|
||||||
content: '',
|
|
||||||
parentNoteId: getHiddenRoot().noteId
|
|
||||||
}).note;
|
|
||||||
}
|
|
||||||
|
|
||||||
return sqlConsoleRoot;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function saveSearchNote(req) {
|
function saveSearchNote(req) {
|
||||||
const searchNote = becca.getNote(req.body.searchNoteId);
|
return specialNotesService.saveSearchNote(req.body.searchNoteId);
|
||||||
|
|
||||||
const hoistedNote = getHoistedNote();
|
|
||||||
let searchHome;
|
|
||||||
|
|
||||||
if (!hoistedNote.isRoot()) {
|
|
||||||
searchHome = hoistedNote.searchNoteInSubtree('#hoistedSearchHome')
|
|
||||||
|| hoistedNote.searchNoteInSubtree('#searchHome')
|
|
||||||
|| hoistedNote;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
const today = dateUtils.localNowDate();
|
|
||||||
|
|
||||||
searchHome = hoistedNote.searchNoteInSubtree('#searchHome')
|
|
||||||
|| dateNoteService.getDateNote(today);
|
|
||||||
}
|
|
||||||
|
|
||||||
return searchNote.cloneTo(searchHome.noteId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function createSearchNote(req) {
|
function createSearchNote(req) {
|
||||||
const params = req.body;
|
|
||||||
const searchString = params.searchString || "";
|
|
||||||
const hoistedNote = getHoistedNote();
|
const hoistedNote = getHoistedNote();
|
||||||
const ancestorNoteId = params.ancestorNoteId || hoistedNote.noteId;
|
const searchString = req.body.searchString || "";
|
||||||
|
const ancestorNoteId = req.body.ancestorNoteId || hoistedNote.noteId;
|
||||||
|
|
||||||
const {note} = noteService.createNewNote({
|
return specialNotesService.createSearchNote(searchString, ancestorNoteId);
|
||||||
parentNoteId: getSearchRoot().noteId,
|
|
||||||
title: 'Search: ' + searchString,
|
|
||||||
content: "",
|
|
||||||
type: 'search',
|
|
||||||
mime: 'application/json'
|
|
||||||
});
|
|
||||||
|
|
||||||
note.setLabel('searchString', searchString);
|
|
||||||
|
|
||||||
if (ancestorNoteId) {
|
|
||||||
note.setRelation('ancestor', ancestorNoteId);
|
|
||||||
}
|
|
||||||
|
|
||||||
return note;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getHoistedNote() {
|
function getHoistedNote() {
|
||||||
|
192
src/services/special_notes.js
Normal file
192
src/services/special_notes.js
Normal file
@ -0,0 +1,192 @@
|
|||||||
|
const attributeService = require("./attributes");
|
||||||
|
const dateNoteService = require("./date_notes");
|
||||||
|
const becca = require("../becca/becca");
|
||||||
|
const noteService = require("./notes");
|
||||||
|
const cls = require("./cls");
|
||||||
|
const dateUtils = require("./date_utils");
|
||||||
|
|
||||||
|
function getInboxNote(date) {
|
||||||
|
const hoistedNote = getHoistedNote();
|
||||||
|
|
||||||
|
let inbox;
|
||||||
|
|
||||||
|
if (!hoistedNote.isRoot()) {
|
||||||
|
inbox = hoistedNote.searchNoteInSubtree('#hoistedInbox');
|
||||||
|
|
||||||
|
if (!inbox) {
|
||||||
|
inbox = hoistedNote.searchNoteInSubtree('#inbox');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!inbox) {
|
||||||
|
inbox = hoistedNote;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
inbox = attributeService.getNoteWithLabel('inbox')
|
||||||
|
|| dateNoteService.getDateNote(date);
|
||||||
|
}
|
||||||
|
|
||||||
|
return inbox;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getHiddenRoot() {
|
||||||
|
let hidden = becca.getNote('hidden');
|
||||||
|
|
||||||
|
if (!hidden) {
|
||||||
|
hidden = noteService.createNewNote({
|
||||||
|
noteId: 'hidden',
|
||||||
|
title: 'hidden',
|
||||||
|
type: 'text',
|
||||||
|
content: '',
|
||||||
|
parentNoteId: 'root'
|
||||||
|
}).note;
|
||||||
|
|
||||||
|
// isInheritable: false means that this notePath is automatically not preffered but at the same time
|
||||||
|
// the flag is not inherited to the children
|
||||||
|
hidden.addLabel('archived', "", false);
|
||||||
|
}
|
||||||
|
|
||||||
|
return hidden;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSearchRoot() {
|
||||||
|
let searchRoot = becca.getNote('search');
|
||||||
|
|
||||||
|
if (!searchRoot) {
|
||||||
|
searchRoot = noteService.createNewNote({
|
||||||
|
noteId: 'search',
|
||||||
|
title: 'search',
|
||||||
|
type: 'text',
|
||||||
|
content: '',
|
||||||
|
parentNoteId: getHiddenRoot().noteId
|
||||||
|
}).note;
|
||||||
|
}
|
||||||
|
|
||||||
|
return searchRoot;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSpecialNoteRoot() {
|
||||||
|
let specialNoteRoot = becca.getNote('special');
|
||||||
|
|
||||||
|
if (!specialNoteRoot) {
|
||||||
|
specialNoteRoot = noteService.createNewNote({
|
||||||
|
noteId: 'special',
|
||||||
|
title: 'special',
|
||||||
|
type: 'text',
|
||||||
|
content: '',
|
||||||
|
parentNoteId: getHiddenRoot().noteId
|
||||||
|
}).note;
|
||||||
|
}
|
||||||
|
|
||||||
|
return specialNoteRoot;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getGlobalLinkMapNote() {
|
||||||
|
let globalLinkMapNote = becca.getNote('global-link-map');
|
||||||
|
|
||||||
|
if (!globalLinkMapNote) {
|
||||||
|
globalLinkMapNote = noteService.createNewNote({
|
||||||
|
noteId: 'global-link-map',
|
||||||
|
title: 'global-link-map',
|
||||||
|
type: 'global-link-map',
|
||||||
|
content: '',
|
||||||
|
parentNoteId: getSpecialNoteRoot().noteId
|
||||||
|
}).note;
|
||||||
|
}
|
||||||
|
|
||||||
|
return globalLinkMapNote;
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSqlConsoleRoot() {
|
||||||
|
let sqlConsoleRoot = becca.getNote('sqlconsole');
|
||||||
|
|
||||||
|
if (!sqlConsoleRoot) {
|
||||||
|
sqlConsoleRoot = noteService.createNewNote({
|
||||||
|
noteId: 'sqlconsole',
|
||||||
|
title: 'SQL Console',
|
||||||
|
type: 'text',
|
||||||
|
content: '',
|
||||||
|
parentNoteId: getHiddenRoot().noteId
|
||||||
|
}).note;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sqlConsoleRoot;
|
||||||
|
}
|
||||||
|
|
||||||
|
function createSqlConsole() {
|
||||||
|
const {note} = noteService.createNewNote({
|
||||||
|
parentNoteId: getSqlConsoleRoot().noteId,
|
||||||
|
title: 'SQL Console',
|
||||||
|
content: "SELECT title, isDeleted, isProtected FROM notes WHERE noteId = ''\n\n\n\n",
|
||||||
|
type: 'code',
|
||||||
|
mime: 'text/x-sqlite;schema=trilium'
|
||||||
|
});
|
||||||
|
|
||||||
|
note.setLabel("sqlConsole", dateUtils.localNowDate());
|
||||||
|
|
||||||
|
return note;
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveSqlConsole(sqlConsoleNoteId) {
|
||||||
|
const sqlConsoleNote = becca.getNote(sqlConsoleNoteId);
|
||||||
|
const today = dateUtils.localNowDate();
|
||||||
|
|
||||||
|
const sqlConsoleHome =
|
||||||
|
attributeService.getNoteWithLabel('sqlConsoleHome')
|
||||||
|
|| dateNoteService.getDateNote(today);
|
||||||
|
|
||||||
|
return sqlConsoleNote.cloneTo(sqlConsoleHome.noteId);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getHoistedNote() {
|
||||||
|
return becca.getNote(cls.getHoistedNoteId());
|
||||||
|
}
|
||||||
|
|
||||||
|
function createSearchNote(searchString, ancestorNoteId) {
|
||||||
|
const hoistedNote = getHoistedNote();
|
||||||
|
|
||||||
|
const {note} = noteService.createNewNote({
|
||||||
|
parentNoteId: getSearchRoot().noteId,
|
||||||
|
title: 'Search: ' + searchString,
|
||||||
|
content: "",
|
||||||
|
type: 'search',
|
||||||
|
mime: 'application/json'
|
||||||
|
});
|
||||||
|
|
||||||
|
note.setLabel('searchString', searchString);
|
||||||
|
|
||||||
|
if (ancestorNoteId) {
|
||||||
|
note.setRelation('ancestor', ancestorNoteId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return note;
|
||||||
|
}
|
||||||
|
|
||||||
|
function saveSearchNote(searchNoteId) {
|
||||||
|
const searchNote = becca.getNote(searchNoteId);
|
||||||
|
|
||||||
|
const hoistedNote = getHoistedNote();
|
||||||
|
let searchHome;
|
||||||
|
|
||||||
|
if (!hoistedNote.isRoot()) {
|
||||||
|
searchHome = hoistedNote.searchNoteInSubtree('#hoistedSearchHome')
|
||||||
|
|| hoistedNote.searchNoteInSubtree('#searchHome')
|
||||||
|
|| hoistedNote;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
const today = dateUtils.localNowDate();
|
||||||
|
|
||||||
|
searchHome = hoistedNote.searchNoteInSubtree('#searchHome')
|
||||||
|
|| dateNoteService.getDateNote(today);
|
||||||
|
}
|
||||||
|
|
||||||
|
return searchNote.cloneTo(searchHome.noteId);
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getInboxNote,
|
||||||
|
createSqlConsole,
|
||||||
|
saveSqlConsole,
|
||||||
|
createSearchNote,
|
||||||
|
saveSearchNote
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user