Notes/src/routes/api/date_notes.js

177 lines
4.4 KiB
JavaScript
Raw Normal View History

"use strict";
const dateNoteService = require('../../services/date_notes');
const sql = require('../../services/sql');
const dateUtils = require('../../services/date_utils');
const noteService = require('../../services/notes');
2020-11-27 22:21:13 +01:00
const attributeService = require('../../services/attributes');
const cls = require('../../services/cls');
2021-06-05 23:35:47 +02:00
const becca = require('../../becca/becca');
2020-11-27 22:21:13 +01:00
function getInboxNote(req) {
2021-03-12 21:29:50 +01:00
const hoistedNote = getHoistedNote();
let inbox;
2021-06-06 11:01:10 +02:00
if (!hoistedNote.isRoot()) {
inbox = hoistedNote.searchNoteInSubtree('#hoistedInbox');
2021-03-12 21:29:50 +01:00
if (!inbox) {
2021-06-06 11:01:10 +02:00
inbox = hoistedNote.searchNoteInSubtree('#inbox');
2021-03-12 21:29:50 +01:00
}
if (!inbox) {
inbox = hoistedNote;
}
}
else {
inbox = attributeService.getNoteWithLabel('inbox')
|| dateNoteService.getDateNote(req.params.date);
}
return inbox;
2020-11-27 22:21:13 +01:00
}
2020-06-20 12:31:38 +02:00
function getDateNote(req) {
return dateNoteService.getDateNote(req.params.date);
}
2020-06-20 12:31:38 +02:00
function getMonthNote(req) {
return dateNoteService.getMonthNote(req.params.month);
}
2020-06-20 12:31:38 +02:00
function getYearNote(req) {
return dateNoteService.getYearNote(req.params.year);
}
2020-06-20 12:31:38 +02:00
function getDateNotesForMonth(req) {
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() {
const today = dateUtils.localNowDate();
2020-12-07 09:19:27 +01:00
const sqlConsoleHome =
attributeService.getNoteWithLabel('sqlConsoleHome')
|| dateNoteService.getDateNote(today);
2020-06-20 12:31:38 +02:00
const {note} = noteService.createNewNote({
2020-12-07 09:19:27 +01:00
parentNoteId: sqlConsoleHome.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'
});
2020-06-20 12:31:38 +02:00
note.setLabel("sqlConsole", today);
return note;
}
2021-06-05 23:35:47 +02:00
function getHiddenRoot() {
let hidden = becca.getNote('hidden');
2021-06-05 23:35:47 +02:00
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);
2021-06-05 23:35:47 +02:00
}
2021-06-05 23:35:47 +02:00
return hidden;
}
2021-06-05 23:35:47 +02:00
function getSearchRoot() {
let searchRoot = becca.getNote('search');
if (!searchRoot) {
searchRoot = noteService.createNewNote({
noteId: 'search',
title: 'search',
type: 'text',
content: '',
parentNoteId: getHiddenRoot().noteId
}).note;
}
2021-06-05 23:35:47 +02:00
return searchRoot;
}
2021-06-06 11:01:10 +02:00
function saveSearchNote(req) {
const searchNote = becca.getNote(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);
}
2021-06-05 23:35:47 +02:00
function createSearchNote(req) {
const params = req.body;
const searchString = params.searchString || "";
const hoistedNote = getHoistedNote();
2021-06-06 11:01:10 +02:00
const ancestorNoteId = params.ancestorNoteId || hoistedNote.noteId;
const {note} = noteService.createNewNote({
2021-06-06 11:01:10 +02:00
parentNoteId: getSearchRoot().noteId,
title: 'Search: ' + searchString,
content: "",
type: 'search',
mime: 'application/json'
});
note.setLabel('searchString', searchString);
if (ancestorNoteId) {
note.setRelation('ancestor', ancestorNoteId);
}
return note;
}
2021-03-12 21:29:50 +01:00
function getHoistedNote() {
2021-06-06 11:01:10 +02:00
return becca.getNote(cls.getHoistedNoteId());
2021-03-12 21:29:50 +01:00
}
module.exports = {
2020-11-27 22:21:13 +01:00
getInboxNote,
getDateNote,
getMonthNote,
getYearNote,
getDateNotesForMonth,
createSqlConsole,
2021-06-06 11:01:10 +02:00
createSearchNote,
saveSearchNote
2020-06-20 12:31:38 +02:00
};