trilium/src/routes/api/notes.js

82 lines
2 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
2017-10-16 07:47:05 +08:00
const sql = require('../../services/sql');
const notes = require('../../services/notes');
const utils = require('../../services/utils');
2017-11-13 10:40:26 +08:00
const protected_session = require('../../services/protected_session');
const tree = require('../../services/tree');
2018-01-21 10:56:03 +08:00
const sync_table = require('../../services/sync_table');
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-03-31 00:57:22 +08:00
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
2018-03-31 00:57:22 +08:00
if (!note) {
return [404, "Note " + noteId + " has not been found."];
}
protected_session.decryptNote(note);
2017-11-13 10:40:26 +08:00
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-03-31 00:57:22 +08:00
async function createNote(req) {
const parentNoteId = req.params.parentNoteId;
const newNote = req.body;
const { noteId, branchId, note } = await notes.createNewNote(parentNoteId, newNote, req);
2018-03-31 00:57:22 +08:00
return {
'noteId': noteId,
'branchId': branchId,
'note': note
};
}
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 notes.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 tree.sortNotesAlphabetically(noteId);
2018-03-31 00:57:22 +08:00
}
2018-03-31 00:57:22 +08:00
async function protectBranch(req) {
const noteId = req.params.noteId;
const note = repository.getNote(noteId);
const protect = !!parseInt(req.params.isProtected);
await notes.protectNoteRecursively(note, protect);
2018-03-31 00:57:22 +08:00
}
2018-03-31 00:57:22 +08:00
async function setNoteTypeMime(req) {
2018-01-24 12:41:22 +08:00
const noteId = req.params[0];
const type = req.params[1];
const mime = req.params[2];
2018-01-21 10:56:03 +08:00
2018-03-31 00:57:22 +08:00
await sql.execute("UPDATE notes SET type = ?, mime = ?, dateModified = ? WHERE noteId = ?",
[type, mime, utils.nowDate(), noteId]);
2018-01-21 10:56:03 +08:00
await sync_table.addNoteSync(noteId);
2018-03-31 00:57:22 +08:00
}
2018-01-21 10:56:03 +08:00
2018-03-31 00:57:22 +08:00
module.exports = {
getNote,
updateNote,
createNote,
sortNotes,
protectBranch,
setNoteTypeMime
};