Notes/src/routes/api/date_notes.js

180 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;
if (hoistedNote) {
([inbox] = hoistedNote.getDescendantNotesWithLabel('hoistedInbox'));
if (!inbox) {
([inbox] = hoistedNote.getDescendantNotesWithLabel('inbox'));
}
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;
2021-06-05 23:35:47 +02:00
hidden.addLabel('archived', "", true);
}
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-05 23:35:47 +02:00
function createSearchNote(req) {
const params = req.body;
const searchString = params.searchString || "";
let ancestorNoteId = params.ancestorNoteId;
2021-03-12 21:29:50 +01:00
2021-06-05 23:35:47 +02:00
const hoistedNote = getHoistedNote();
let searchHome = getSearchRoot();
// if (hoistedNote) {
// ([searchHome] = hoistedNote.getDescendantNotesWithLabel('hoistedSearchHome'));
//
// if (!searchHome) {
// ([searchHome] = hoistedNote.getDescendantNotesWithLabel('searchHome'));
// }
//
// if (!searchHome) {
// searchHome = hoistedNote;
// }
//
// if (!ancestorNoteId) {
// ancestorNoteId = hoistedNote.noteId;
// }
// }
// else {
// const today = dateUtils.localNowDate();
//
// searchHome = attributeService.getNoteWithLabel('searchHome')
// || dateNoteService.getDateNote(today);
// }
const {note} = noteService.createNewNote({
2020-12-04 22:57:54 +01:00
parentNoteId: searchHome.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() {
return cls.getHoistedNoteId() && cls.getHoistedNoteId() !== 'root'
2021-05-02 11:23:58 +02:00
? becca.getNote(cls.getHoistedNoteId())
2021-03-12 21:29:50 +01:00
: null;
}
module.exports = {
2020-11-27 22:21:13 +01:00
getInboxNote,
getDateNote,
getMonthNote,
getYearNote,
getDateNotesForMonth,
createSqlConsole,
createSearchNote
2020-06-20 12:31:38 +02:00
};