Notes/src/routes/api/notes.js

92 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-10-21 21:10:33 -04:00
"use strict";
const express = require('express');
const router = express.Router();
const auth = require('../../services/auth');
2017-10-15 19:47:05 -04:00
const sql = require('../../services/sql');
const notes = require('../../services/notes');
const utils = require('../../services/utils');
2017-11-12 21:40:26 -05:00
const protected_session = require('../../services/protected_session');
const tree = require('../../services/tree');
2018-01-20 21:56:03 -05:00
const sync_table = require('../../services/sync_table');
const wrap = require('express-promise-wrap').wrap;
2018-03-30 12:57:22 -04:00
async function getNote(req) {
2017-11-15 00:04:26 -05:00
const noteId = req.params.noteId;
2018-03-30 12:57:22 -04:00
const note = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);
2018-03-30 12:57:22 -04:00
if (!note) {
return [404, "Note " + noteId + " has not been found."];
}
2018-03-30 12:57:22 -04:00
protected_session.decryptNote(req, note);
2017-11-12 21:40:26 -05:00
2018-03-30 12:57:22 -04:00
if (note.type === 'file') {
// no need to transfer (potentially large) file payload for this request
2018-03-30 12:57:22 -04:00
note.content = null;
}
2018-03-30 12:57:22 -04:00
return note;
}
2018-03-30 12:57:22 -04:00
async function createNote(req) {
2018-01-28 21:57:46 -05:00
const sourceId = req.headers.source_id;
const parentNoteId = req.params.parentNoteId;
const newNote = req.body;
2018-03-30 12:57:22 -04:00
const { noteId, branchId, note } = await notes.createNewNote(parentNoteId, newNote, req, sourceId);
2018-03-30 12:57:22 -04:00
return {
'noteId': noteId,
'branchId': branchId,
'note': note
};
}
2018-03-30 12:57:22 -04:00
async function updateNote(req) {
const note = req.body;
2017-11-15 00:04:26 -05:00
const noteId = req.params.noteId;
2018-01-28 21:57:46 -05:00
const sourceId = req.headers.source_id;
const dataKey = protected_session.getDataKey(req);
2018-03-30 12:57:22 -04:00
await notes.updateNote(noteId, note, dataKey, sourceId);
}
2018-03-30 12:57:22 -04:00
async function sortNotes(req) {
const noteId = req.params.noteId;
2018-01-28 21:57:46 -05:00
const sourceId = req.headers.source_id;
const dataKey = protected_session.getDataKey(req);
await tree.sortNotesAlphabetically(noteId, dataKey, sourceId);
2018-03-30 12:57:22 -04:00
}
2018-03-30 12:57:22 -04:00
async function protectBranch(req) {
const noteId = req.params.noteId;
const isProtected = !!parseInt(req.params.isProtected);
const dataKey = protected_session.getDataKey(req);
2018-01-28 21:57:46 -05:00
const sourceId = req.headers.source_id;
2018-03-30 12:57:22 -04:00
await notes.protectNoteRecursively(noteId, dataKey, isProtected, sourceId);
}
2018-03-30 12:57:22 -04:00
async function setNoteTypeMime(req) {
2018-01-23 23:41:22 -05:00
const noteId = req.params[0];
const type = req.params[1];
const mime = req.params[2];
2018-01-28 21:57:46 -05:00
const sourceId = req.headers.source_id;
2018-01-20 21:56:03 -05:00
2018-03-30 12:57:22 -04:00
await sql.execute("UPDATE notes SET type = ?, mime = ?, dateModified = ? WHERE noteId = ?",
[type, mime, utils.nowDate(), noteId]);
2018-01-20 21:56:03 -05:00
2018-03-30 12:57:22 -04:00
await sync_table.addNoteSync(noteId, sourceId);
}
2018-01-20 21:56:03 -05:00
2018-03-30 12:57:22 -04:00
module.exports = {
getNote,
updateNote,
createNote,
sortNotes,
protectBranch,
setNoteTypeMime
};