2017-10-22 09:10:33 +08:00
|
|
|
"use strict";
|
|
|
|
|
2017-10-15 11:31:44 +08:00
|
|
|
const express = require('express');
|
|
|
|
const router = express.Router();
|
2017-11-05 23:41:54 +08:00
|
|
|
const auth = require('../../services/auth');
|
2017-10-16 07:47:05 +08:00
|
|
|
const sql = require('../../services/sql');
|
2017-11-05 23:41:54 +08:00
|
|
|
const notes = require('../../services/notes');
|
2018-01-16 09:54:22 +08:00
|
|
|
const utils = require('../../services/utils');
|
2017-11-13 10:40:26 +08:00
|
|
|
const protected_session = require('../../services/protected_session');
|
2018-01-14 09:53:00 +08:00
|
|
|
const tree = require('../../services/tree');
|
2018-01-21 10:56:03 +08:00
|
|
|
const sync_table = require('../../services/sync_table');
|
2018-01-07 22:35:44 +08:00
|
|
|
const wrap = require('express-promise-wrap').wrap;
|
2017-10-15 11:31:44 +08:00
|
|
|
|
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;
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
if (!note) {
|
|
|
|
return [404, "Note " + noteId + " has not been found."];
|
2017-11-27 12:10:23 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 20:53:52 +08:00
|
|
|
protected_session.decryptNote(note);
|
2017-11-13 10:40:26 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
if (note.type === 'file') {
|
2018-03-28 10:11:06 +08:00
|
|
|
// no need to transfer (potentially large) file payload for this request
|
2018-03-31 00:57:22 +08:00
|
|
|
note.content = null;
|
2018-02-19 10:28:24 +08:00
|
|
|
}
|
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
return note;
|
|
|
|
}
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
async function createNote(req) {
|
2017-11-23 12:16:54 +08:00
|
|
|
const parentNoteId = req.params.parentNoteId;
|
2018-01-28 23:37:43 +08:00
|
|
|
const newNote = req.body;
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-03-31 07:41:54 +08:00
|
|
|
const { noteId, branchId, note } = await notes.createNewNote(parentNoteId, newNote, req);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
return {
|
|
|
|
'noteId': noteId,
|
|
|
|
'branchId': branchId,
|
|
|
|
'note': note
|
|
|
|
};
|
|
|
|
}
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
async function updateNote(req) {
|
2017-11-15 10:54:12 +08:00
|
|
|
const note = req.body;
|
2017-11-15 13:04:26 +08:00
|
|
|
const noteId = req.params.noteId;
|
2017-11-05 23:41:54 +08:00
|
|
|
|
2018-03-31 20:53:52 +08:00
|
|
|
await notes.updateNote(noteId, note);
|
2018-03-31 00:57:22 +08:00
|
|
|
}
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
async function sortNotes(req) {
|
2018-01-14 06:00:40 +08:00
|
|
|
const noteId = req.params.noteId;
|
|
|
|
|
2018-03-31 20:53:52 +08:00
|
|
|
await tree.sortNotesAlphabetically(noteId);
|
2018-03-31 00:57:22 +08:00
|
|
|
}
|
2018-01-14 06:00:40 +08:00
|
|
|
|
2018-03-31 00:57:22 +08:00
|
|
|
async function protectBranch(req) {
|
2018-01-14 09:53:00 +08:00
|
|
|
const noteId = req.params.noteId;
|
|
|
|
const isProtected = !!parseInt(req.params.isProtected);
|
2018-01-14 06:00:40 +08:00
|
|
|
|
2018-03-31 20:53:52 +08:00
|
|
|
await notes.protectNoteRecursively(noteId, isProtected);
|
2018-03-31 00:57:22 +08:00
|
|
|
}
|
2018-01-14 06:00:40 +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
|
|
|
|
2018-03-31 07:41:54 +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
|
|
|
|
};
|