move API routes into api subdir

This commit is contained in:
azivner
2017-10-15 17:09:41 -04:00
parent 597a9063e5
commit e8655a84e2
9 changed files with 32 additions and 32 deletions

19
node/routes/api/audit.js Normal file
View File

@@ -0,0 +1,19 @@
const express = require('express');
const router = express.Router();
const sql = require('../../sql');
const auth = require('../../auth');
router.get('/:full_load_time', auth.checkApiAuth, async (req, res, next) => {
const fullLoadTime = req.params.full_load_time;
const browserId = req.get('x-browser-id');
const count = await sql.getSingleResult("SELECT COUNT(*) AS 'count' FROM audit_log WHERE browser_id != ? " +
"AND date_modified >= ?", [browserId, fullLoadTime])['count'];
res.send({
'changed': count > 0
});
});
module.exports = router;

View File

@@ -0,0 +1,14 @@
const express = require('express');
const router = express.Router();
const sql = require('../../sql');
const auth = require('../../auth');
router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => {
const noteId = req.params.noteId;
const history = await sql.getResults("select * from notes_history where note_id = ? order by date_modified desc", [noteId]);
res.send(history);
});
module.exports = router;

204
node/routes/api/notes.js Normal file
View File

@@ -0,0 +1,204 @@
const express = require('express');
const router = express.Router();
const sql = require('../../sql');
const utils = require('../../utils');
const audit_category = require('../../audit_category');
const auth = require('../../auth');
router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => {
let noteId = req.params.noteId;
await sql.execute("update options set opt_value = ? where opt_name = 'start_node'", [noteId]);
let detail = await sql.getSingleResult("select * from notes where note_id = ?", [noteId]);
if (detail['note_clone_id']) {
noteId = detail['note_clone_id'];
detail = sql.getSingleResult("select * from notes where note_id = ?", [noteId]);
}
res.send({
'detail': detail,
'formatting': await sql.getResults("select * from formatting where note_id = ? order by note_offset", [noteId]),
'links': await sql.getResults("select * from links where note_id = ? order by note_offset", [noteId]),
'images': await sql.getResults("select * from images where note_id = ? order by note_offset", [noteId])
});
});
router.put('/:noteId', async (req, res, next) => {
let noteId = req.params.noteId;
const detail = await sql.getSingleResult("select * from notes where note_id = ?", [noteId]);
if (detail['note_clone_id']) {
noteId = detail['note_clone_id'];
}
const note = req.body;
const now = utils.nowTimestamp();
const historySnapshotTimeInterval = parseInt(await sql.getOption('history_snapshot_time_interval'));
const historyCutoff = now - historySnapshotTimeInterval;
const history = await sql.getSingleResult("select id from notes_history where note_id = ? and date_modified >= ?", [noteId, historyCutoff]);
await sql.beginTransaction();
if (history) {
await sql.execute("update notes_history set note_title = ?, note_text = ?, encryption = ? where id = ?", [
note['detail']['note_title'],
note['detail']['note_text'],
note['detail']['encryption'],
history['id']
]);
}
else {
await sql.execute("insert into notes_history (note_id, note_title, note_text, encryption, date_modified) values (?, ?, ?, ?, ?)", [
noteId,
note['detail']['note_title'],
note['detail']['note_text'],
note['detail']['encryption'],
now
]);
}
if (note['detail']['note_title'] !== detail['note_title']) {
await sql.deleteRecentAudits(audit_category.UPDATE_TITLE, req, noteId);
await sql.addAudit(audit_category.UPDATE_TITLE, req, noteId);
}
if (note['detail']['note_text'] !== detail['note_text']) {
await sql.deleteRecentAudits(audit_category.UPDATE_CONTENT, req, noteId);
await sql.addAudit(audit_category.UPDATE_CONTENT, req, noteId);
}
if (note['detail']['encryption'] !== detail['encryption']) {
await sql.addAudit(audit_category.ENCRYPTION, req, noteId, detail['encryption'], note['detail']['encryption']);
}
await sql.execute("update notes set note_title = ?, note_text = ?, encryption = ?, date_modified = ? where note_id = ?", [
note['detail']['note_title'],
note['detail']['note_text'],
note['detail']['encryption'],
now,
noteId]);
await sql.remove("images", noteId);
for (const img of note['images']) {
img['image_data'] = atob(img['image_data']);
await sql.insert("images", img);
}
await sql.remove("links", noteId);
for (const link in note['links'])
await sql.insert("links", link);
await sql.commit();
res.send({});
});
router.delete('/:noteId', async (req, res, next) => {
await deleteNote(req.params.noteId);
await sql.commit();
res.send({});
});
async function deleteNote(noteId) {
const children = await sql.getResults("select note_id from notes_tree where note_pid = ?", [noteId]);
for (const child of children) {
await deleteNote(child['note_id']);
}
await sql.delete("notes_tree", noteId);
await sql.delete("notes", noteId);
await sql.addAudit(audit_category.DELETE_NOTE, req, noteId);
}
router.post('/:parentNoteId/children', async (req, res, next) => {
let parentNoteId = req.params.parentNoteId;
const note = req.body;
const noteId = utils.newNoteId();
if (parentNoteId === "root") {
parentNoteId = "";
}
let newNotePos = 0;
if (note['target'] === 'into') {
const res = await sql.getSingleResult('select max(note_pos) as max_note_pos from notes_tree where note_pid = ?', [parentNoteId]);
const maxNotePos = res['max_note_pos'];
if (maxNotePos === null) // no children yet
newNotePos = 0;
else
newNotePos = maxNotePos + 1
}
else if (note['target'] === 'after') {
const afterNote = await sql.getSingleResult('select note_pos from notes_tree where note_id = ?', [note['target_note_id']]);
newNotePos = afterNote['note_pos'] + 1;
await sql.execute('update notes_tree set note_pos = note_pos + 1 where note_pid = ? and note_pos > ?', [parentNoteId, afterNote['note_pos']]);
}
else {
throw new ('Unknown target: ' + note['target']);
}
await sql.addAudit(audit_category.CREATE_NOTE, req, noteId);
const now = utils.nowTimestamp();
await sql.insert("notes", {
'note_id': noteId,
'note_title': note['note_title'],
'note_text': '',
'note_clone_id': '',
'date_created': now,
'date_modified': now,
'icon_info': 'pencil',
'is_finished': 0,
'encryption': note['encryption']
});
await sql.insert("notes_tree", {
'note_id': noteId,
'note_pid': parentNoteId,
'note_pos': newNotePos,
'is_expanded': 0
});
await sql.commit();
res.send({
'note_id': noteId
});
});
router.get('/', async (req, res, next) => {
const search = '%' + req.query.search + '%';
const result = await sql.getResults("select note_id from notes where note_title like ? or note_text like ?", [search, search]);
const noteIdList = [];
for (const res of result) {
noteIdList.push(res['note_id']);
}
res.send(noteIdList);
});
module.exports = router;

View File

@@ -0,0 +1,85 @@
const express = require('express');
const router = express.Router();
const sql = require('../../sql');
const utils = require('../../utils');
const audit_category = require('../../audit_category');
const auth = require('../../auth');
router.put('/:noteId/moveTo/:parentId', auth.checkApiAuth, async (req, res, next) => {
let noteId = req.params.noteId;
let parentId = req.params.parentId;
const row = await sql.getSingleResult('select max(note_pos) as max_note_pos from notes_tree where note_pid = ?', [parentId]);
const maxNotePos = row['max_note_pos'];
let newNotePos = 0;
if (maxNotePos === null) // no children yet
newNotePos = 0;
else
newNotePos = maxNotePos + 1;
await sql.beginTransaction();
await sql.execute("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?", [parentId, newNotePos, noteId]);
await sql.addAudit(audit_category.CHANGE_PARENT, req, noteId);
await sql.commit();
res.send({});
});
router.put('/:noteId/moveBefore/:beforeNoteId', async (req, res, next) => {
let noteId = req.params.noteId;
let beforeNoteId = req.params.beforeNoteId;
const beforeNote = await sql.getSingleResult("select * from notes_tree where note_id = ?", [beforeNoteId]);
if (beforeNote !== null) {
await sql.beginTransaction();
await sql.execute("update notes_tree set note_pos = note_pos + 1 where note_id = ?", [beforeNoteId]);
await sql.execute("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?", [beforeNote['note_pid'], beforeNote['note_pos'], noteId]);
await sql.addAudit(audit_category.CHANGE_POSITION, req, noteId);
await sql.commit();
}
res.send({});
});
router.put('/:noteId/moveAfter/:afterNoteId', async (req, res, next) => {
let noteId = req.params.noteId;
let afterNoteId = req.params.afterNoteId;
const afterNote = await sql.getSingleResult("select * from notes_tree where note_id = ?", [afterNoteId]);
if (afterNote !== null) {
await sql.beginTransaction();
await sql.execute("update notes_tree set note_pos = note_pos + 1 where note_pid = ? and note_pos > ?", [afterNote['note_pid'], afterNote['note_pos']]);
await sql.execute("update notes_tree set note_pid = ?, note_pos = ? where note_id = ?", [afterNote['note_pid'], afterNote['note_pos'] + 1, noteId]);
await sql.addAudit(audit_category.CHANGE_POSITION, req, noteId);
await sql.commit();
}
res.send({});
});
router.put('/:noteId/expanded/:expanded', async (req, res, next) => {
let noteId = req.params.noteId;
let expanded = req.params.expanded;
await sql.execute("update notes_tree set is_expanded = ? where note_id = ?", [expanded, noteId]);
// no audit here, not really important
res.send({});
});
module.exports = router;

View File

@@ -0,0 +1,13 @@
const express = require('express');
const router = express.Router();
const sql = require('../../sql');
const changePassword = require('../../change_password');
const auth = require('../../auth');
router.post('/change', auth.checkApiAuth, async (req, res, next) => {
const result = await changePassword.changePassword(req.body['current_password'], req.body['new_password']);
res.send(result);
});
module.exports = router;

View File

@@ -0,0 +1,12 @@
const express = require('express');
const router = express.Router();
const sql = require('../../sql');
const auth = require('../../auth');
router.get('/', auth.checkApiAuth, async (req, res, next) => {
const recentChanges = await sql.getResults("select * from notes_history order by date_modified desc limit 1000");
res.send(recentChanges);
});
module.exports = router;

View File

@@ -0,0 +1,43 @@
const express = require('express');
const router = express.Router();
const sql = require('../../sql');
const audit_category = require('../../audit_category');
const auth = require('../../auth');
const ALLOWED_OPTIONS = ['encryption_session_timeout', 'history_snapshot_time_interval'];
router.get('/', auth.checkApiAuth, async (req, res, next) => {
const dict = {};
const settings = await sql.getResults("SELECT opt_name, opt_value FROM options WHERE opt_name IN ("
+ ALLOWED_OPTIONS.map(x => '?').join(",") + ")", ALLOWED_OPTIONS);
for (const set of settings) {
dict[set['opt_name']] = set['opt_value'];
}
res.send(dict);
});
router.post('/', async (req, res, next) => {
body = req.body;
if (ALLOWED_OPTIONS.includes(body['name'])) {
const optionName = await sql.getOption(body['name']);
await sql.beginTransaction();
await sql.addAudit(audit_category.SETTINGS, req, null, optionName, body['value'], body['name']);
await sql.setOption(body['name'], body['value']);
await sql.commit();
res.send({});
}
else {
res.send("not allowed option to set");
}
});
module.exports = router;

56
node/routes/api/tree.js Normal file
View File

@@ -0,0 +1,56 @@
const express = require('express');
const router = express.Router();
const sql = require('../../sql');
const utils = require('../../utils');
const backup = require('../../backup');
const auth = require('../../auth');
router.get('/', auth.checkApiAuth, async (req, res, next) => {
await backup.regularBackup();
const notes = await sql.getResults("select "
+ "notes_tree.*, "
+ "COALESCE(clone.note_title, notes.note_title) as note_title, "
+ "notes.note_clone_id, "
+ "notes.encryption, "
+ "case when notes.note_clone_id is null or notes.note_clone_id = '' then 0 else 1 end as is_clone "
+ "from notes_tree "
+ "join notes on notes.note_id = notes_tree.note_id "
+ "left join notes as clone on notes.note_clone_id = clone.note_id "
+ "order by note_pid, note_pos");
const root_notes = [];
const notes_map = {};
for (const note of notes) {
note['children'] = [];
if (!note['note_pid']) {
root_notes.push(note);
}
notes_map[note['note_id']] = note;
}
for (const note of notes) {
if (note['note_pid'] !== "") {
const parent = notes_map[note['note_pid']];
parent['children'].push(note);
parent['folder'] = true;
}
}
res.send({
'notes': root_notes,
'start_note_id': await sql.getOption('start_node'),
'password_verification_salt': await sql.getOption('password_verification_salt'),
'password_derived_key_salt': await sql.getOption('password_derived_key_salt'),
'encrypted_data_key': await sql.getOption('encrypted_data_key'),
'encryption_session_timeout': await sql.getOption('encryption_session_timeout'),
'browser_id': utils.randomToken(8),
'full_load_time': utils.nowTimestamp()
});
});
module.exports = router;