trilium/src/routes/api/notes.js

134 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
const noteService = require('../../services/notes');
const treeService = require('../../services/tree');
const repository = require('../../services/repository');
2018-03-31 00:57:22 +08:00
async function getNote(req) {
2017-11-15 13:04:26 +08:00
const noteId = req.params.noteId;
2018-04-01 23:42:12 +08:00
const note = await repository.getNote(noteId);
2018-03-31 00:57:22 +08:00
if (!note) {
return [404, "Note " + noteId + " has not been found."];
}
2018-03-31 00:57:22 +08:00
if (note.type === 'file') {
// no need to transfer (potentially large) file payload for this request
2018-03-31 00:57:22 +08:00
note.content = null;
}
2018-03-31 00:57:22 +08:00
return note;
}
2018-10-21 16:26:14 +08:00
async function getChildren(req) {
const parentNoteId = req.params.parentNoteId;
const parentNote = await repository.getNote(parentNoteId);
if (!parentNote) {
return [404, `Note ${parentNoteId} has not been found.`];
}
const ret = [];
for (const childNote of await parentNote.getChildNotes()) {
ret.push({
noteId: childNote.noteId,
title: childNote.title,
relations: (await childNote.getRelations()).map(relation => { return {
attributeId: relation.attributeId,
name: relation.name,
targetNoteId: relation.value
}; })
});
}
return ret;
}
2018-03-31 00:57:22 +08:00
async function createNote(req) {
const parentNoteId = req.params.parentNoteId;
const newNote = req.body;
const { note, branch } = await noteService.createNewNote(parentNoteId, newNote, req);
note.cssClass = (await note.getLabels("cssClass")).map(label => label.value).join(" ");
2018-03-31 00:57:22 +08:00
return {
2018-04-01 23:42:12 +08:00
note,
branch
2018-03-31 00:57:22 +08:00
};
}
2018-03-31 00:57:22 +08:00
async function updateNote(req) {
const note = req.body;
2017-11-15 13:04:26 +08:00
const noteId = req.params.noteId;
await noteService.updateNote(noteId, note);
2018-03-31 00:57:22 +08:00
}
2018-03-31 00:57:22 +08:00
async function sortNotes(req) {
const noteId = req.params.noteId;
await treeService.sortNotesAlphabetically(noteId);
2018-03-31 00:57:22 +08:00
}
async function protectSubtree(req) {
const noteId = req.params.noteId;
2018-04-20 10:18:19 +08:00
const note = await repository.getNote(noteId);
const protect = !!parseInt(req.params.isProtected);
await noteService.protectNoteRecursively(note, protect);
2018-03-31 00:57:22 +08:00
}
2018-03-31 00:57:22 +08:00
async function setNoteTypeMime(req) {
2018-04-05 11:04:31 +08:00
// can't use [] destructuring because req.params is not iterable
const noteId = req.params[0];
const type = req.params[1];
const mime = req.params[2];
2018-01-21 10:56:03 +08:00
2018-04-02 05:38:24 +08:00
const note = await repository.getNote(noteId);
note.type = type;
note.mime = mime;
await note.save();
2018-03-31 00:57:22 +08:00
}
2018-01-21 10:56:03 +08:00
2018-10-25 18:06:36 +08:00
async function getRelationMap(req) {
const noteIds = req.body.noteIds;
const resp = {
noteTitles: {},
relations: []
};
if (noteIds.length === 0) {
return resp;
}
const questionMarks = noteIds.map(noteId => '?').join(',');
2018-10-25 20:01:03 +08:00
(await repository.getEntities(`SELECT * FROM notes WHERE isDeleted = 0 AND noteId IN (${questionMarks})`, noteIds))
2018-10-25 18:06:36 +08:00
.forEach(note => resp.noteTitles[note.noteId] = note.title);
// FIXME: this actually doesn't take into account inherited relations! But maybe it is better this way?
2018-10-25 20:01:03 +08:00
resp.relations = (await repository.getEntities(`SELECT * FROM attributes WHERE isDeleted = 0 AND type = 'relation' AND noteId IN (${questionMarks})`, noteIds))
2018-10-25 18:06:36 +08:00
.map(relation => { return {
attributeId: relation.attributeId,
2018-10-25 18:06:36 +08:00
sourceNoteId: relation.noteId,
targetNoteId: relation.value,
name: relation.name
}; })
// both sourceNoteId and targetNoteId has to be in the included notes, but source was already checked in the SQL query
.filter(relation => noteIds.includes(relation.targetNoteId));
return resp;
}
2018-03-31 00:57:22 +08:00
module.exports = {
getNote,
updateNote,
createNote,
sortNotes,
protectSubtree,
2018-10-21 16:26:14 +08:00
setNoteTypeMime,
2018-10-25 18:06:36 +08:00
getChildren,
getRelationMap
2018-03-31 00:57:22 +08:00
};