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-10-16 07:47:05 +08:00
|
|
|
const sql = require('../../services/sql');
|
|
|
|
const utils = require('../../services/utils');
|
|
|
|
const auth = require('../../services/auth');
|
2017-11-17 10:50:00 +08:00
|
|
|
const sync_table = require('../../services/sync_table');
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-12-02 11:47:23 +08:00
|
|
|
router.put('/:noteTreeId/move-to/:parentNoteId', auth.checkApiAuth, async (req, res, next) => {
|
2017-11-20 07:16:50 +08:00
|
|
|
const noteTreeId = req.params.noteTreeId;
|
|
|
|
const parentNoteId = req.params.parentNoteId;
|
2017-12-17 09:48:34 +08:00
|
|
|
const sourceId = req.headers.source_id;
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-11-21 12:51:28 +08:00
|
|
|
const maxNotePos = await sql.getSingleValue('SELECT MAX(note_pos) FROM notes_tree WHERE note_pid = ? AND is_deleted = 0', [parentNoteId]);
|
2017-11-20 07:16:50 +08:00
|
|
|
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-12-11 01:56:59 +08:00
|
|
|
const now = utils.nowDate();
|
2017-10-23 10:56:42 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
await sql.execute("UPDATE notes_tree SET note_pid = ?, note_pos = ?, date_modified = ? WHERE note_tree_id = ?",
|
2017-11-20 07:16:50 +08:00
|
|
|
[parentNoteId, newNotePos, now, noteTreeId]);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addNoteTreeSync(noteTreeId, sourceId);
|
2017-10-30 06:50:28 +08:00
|
|
|
});
|
2017-10-15 11:31:44 +08:00
|
|
|
|
|
|
|
res.send({});
|
|
|
|
});
|
|
|
|
|
2017-12-02 11:47:23 +08:00
|
|
|
router.put('/:noteTreeId/move-before/:beforeNoteTreeId', async (req, res, next) => {
|
2017-11-20 07:16:50 +08:00
|
|
|
const noteTreeId = req.params.noteTreeId;
|
|
|
|
const beforeNoteTreeId = req.params.beforeNoteTreeId;
|
2017-12-17 09:48:34 +08:00
|
|
|
const sourceId = req.headers.source_id;
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-11-21 12:51:28 +08:00
|
|
|
const beforeNote = await sql.getSingleResult("SELECT * FROM notes_tree WHERE note_tree_id = ?", [beforeNoteTreeId]);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-10-23 10:56:42 +08:00
|
|
|
if (beforeNote) {
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2017-11-03 10:55:22 +08:00
|
|
|
// we don't change date_modified so other changes are prioritized in case of conflict
|
2017-12-19 09:58:13 +08:00
|
|
|
// also we would have to sync all those modified note trees otherwise hash checks would fail
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.execute("UPDATE notes_tree SET note_pos = note_pos + 1 WHERE note_pid = ? AND note_pos >= ? AND is_deleted = 0",
|
2017-11-17 11:18:25 +08:00
|
|
|
[beforeNote.note_pid, beforeNote.note_pos]);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addNoteReorderingSync(beforeNote.note_pid, sourceId);
|
2017-11-30 10:04:30 +08:00
|
|
|
|
2017-12-11 01:56:59 +08:00
|
|
|
const now = utils.nowDate();
|
2017-11-10 10:11:33 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.execute("UPDATE notes_tree SET note_pid = ?, note_pos = ?, date_modified = ? WHERE note_tree_id = ?",
|
2017-11-20 07:16:50 +08:00
|
|
|
[beforeNote.note_pid, beforeNote.note_pos, now, noteTreeId]);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addNoteTreeSync(noteTreeId, sourceId);
|
2017-10-30 06:50:28 +08:00
|
|
|
});
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-11-20 07:16:50 +08:00
|
|
|
res.send({});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.status(500).send("Before note " + beforeNoteTreeId + " doesn't exist.");
|
|
|
|
}
|
2017-10-15 11:31:44 +08:00
|
|
|
});
|
|
|
|
|
2017-12-02 11:47:23 +08:00
|
|
|
router.put('/:noteTreeId/move-after/:afterNoteTreeId', async (req, res, next) => {
|
2017-11-20 07:16:50 +08:00
|
|
|
const noteTreeId = req.params.noteTreeId;
|
|
|
|
const afterNoteTreeId = req.params.afterNoteTreeId;
|
2017-12-17 09:48:34 +08:00
|
|
|
const sourceId = req.headers.source_id;
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-11-21 12:51:28 +08:00
|
|
|
const afterNote = await sql.getSingleResult("SELECT * FROM notes_tree WHERE note_tree_id = ?", [afterNoteTreeId]);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-10-23 10:56:42 +08:00
|
|
|
if (afterNote) {
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2017-11-03 10:55:22 +08:00
|
|
|
// we don't change date_modified so other changes are prioritized in case of conflict
|
2017-12-19 09:58:13 +08:00
|
|
|
// also we would have to sync all those modified note trees otherwise hash checks would fail
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.execute("UPDATE notes_tree SET note_pos = note_pos + 1 WHERE note_pid = ? AND note_pos > ? AND is_deleted = 0",
|
2017-11-17 11:18:25 +08:00
|
|
|
[afterNote.note_pid, afterNote.note_pos]);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addNoteReorderingSync(afterNote.note_pid, sourceId);
|
2017-11-10 10:11:33 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.execute("UPDATE notes_tree SET note_pid = ?, note_pos = ?, date_modified = ? WHERE note_tree_id = ?",
|
2017-12-11 01:56:59 +08:00
|
|
|
[afterNote.note_pid, afterNote.note_pos + 1, utils.nowDate(), noteTreeId]);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addNoteTreeSync(noteTreeId, sourceId);
|
2017-10-30 06:50:28 +08:00
|
|
|
});
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-11-20 07:16:50 +08:00
|
|
|
res.send({});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res.status(500).send("After note " + afterNoteTreeId + " doesn't exist.");
|
|
|
|
}
|
2017-10-15 11:31:44 +08:00
|
|
|
});
|
|
|
|
|
2017-12-02 11:47:23 +08:00
|
|
|
router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, async (req, res, next) => {
|
2017-11-23 12:16:54 +08:00
|
|
|
const parentNoteId = req.params.parentNoteId;
|
|
|
|
const childNoteId = req.params.childNoteId;
|
2017-12-17 09:48:34 +08:00
|
|
|
const sourceId = req.headers.source_id;
|
2017-11-23 12:16:54 +08:00
|
|
|
|
|
|
|
const existing = await sql.getSingleValue('SELECT * FROM notes_tree WHERE note_id = ? AND note_pid = ?', [childNoteId, parentNoteId]);
|
|
|
|
|
2017-11-24 09:49:24 +08:00
|
|
|
if (existing && !existing.is_deleted) {
|
2017-11-24 10:50:12 +08:00
|
|
|
return res.send({
|
2017-11-24 09:49:24 +08:00
|
|
|
success: false,
|
|
|
|
message: 'This note already exists in target parent note.'
|
|
|
|
});
|
2017-11-24 10:50:12 +08:00
|
|
|
}
|
2017-11-23 12:16:54 +08:00
|
|
|
|
2017-11-24 10:50:12 +08:00
|
|
|
if (!await checkCycle(parentNoteId, childNoteId)) {
|
|
|
|
return res.send({
|
|
|
|
success: false,
|
|
|
|
message: 'Cloning note here would create cycle.'
|
|
|
|
});
|
2017-11-24 09:49:24 +08:00
|
|
|
}
|
2017-11-23 12:16:54 +08:00
|
|
|
|
2017-11-24 09:49:24 +08:00
|
|
|
const maxNotePos = await sql.getSingleValue('SELECT MAX(note_pos) FROM notes_tree WHERE note_pid = ? AND is_deleted = 0', [parentNoteId]);
|
|
|
|
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 1;
|
2017-11-23 12:16:54 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2017-11-24 09:49:24 +08:00
|
|
|
const noteTree = {
|
2017-12-11 01:56:59 +08:00
|
|
|
note_tree_id: utils.newNoteTreeId(),
|
|
|
|
note_id: childNoteId,
|
|
|
|
note_pid: parentNoteId,
|
|
|
|
note_pos: newNotePos,
|
|
|
|
is_expanded: 0,
|
|
|
|
date_modified: utils.nowDate(),
|
|
|
|
is_deleted: 0
|
2017-11-24 09:49:24 +08:00
|
|
|
};
|
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.replace("notes_tree", noteTree);
|
2017-11-24 09:49:24 +08:00
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addNoteTreeSync(noteTree.note_tree_id, sourceId);
|
2017-12-19 09:58:13 +08:00
|
|
|
});
|
2017-11-24 09:49:24 +08:00
|
|
|
|
2017-12-19 09:58:13 +08:00
|
|
|
res.send({
|
|
|
|
success: true
|
2017-11-24 09:49:24 +08:00
|
|
|
});
|
2017-11-23 12:16:54 +08:00
|
|
|
});
|
|
|
|
|
2017-12-02 11:47:23 +08:00
|
|
|
router.put('/:noteId/clone-after/:afterNoteTreeId', async (req, res, next) => {
|
2017-11-23 12:16:54 +08:00
|
|
|
const noteId = req.params.noteId;
|
|
|
|
const afterNoteTreeId = req.params.afterNoteTreeId;
|
2017-12-17 09:48:34 +08:00
|
|
|
const sourceId = req.headers.source_id;
|
2017-11-23 12:16:54 +08:00
|
|
|
|
|
|
|
const afterNote = await sql.getSingleResult("SELECT * FROM notes_tree WHERE note_tree_id = ?", [afterNoteTreeId]);
|
|
|
|
|
2017-11-24 09:49:24 +08:00
|
|
|
if (!afterNote) {
|
2017-11-24 10:50:12 +08:00
|
|
|
return res.status(500).send("After note " + afterNoteTreeId + " doesn't exist.");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!await checkCycle(afterNote.note_pid, noteId)) {
|
|
|
|
return res.send({
|
|
|
|
success: false,
|
|
|
|
message: 'Cloning note here would create cycle.'
|
|
|
|
});
|
2017-11-24 09:49:24 +08:00
|
|
|
}
|
2017-11-23 12:16:54 +08:00
|
|
|
|
2017-11-24 09:49:24 +08:00
|
|
|
const existing = await sql.getSingleValue('SELECT * FROM notes_tree WHERE note_id = ? AND note_pid = ?', [noteId, afterNote.note_pid]);
|
2017-11-23 12:16:54 +08:00
|
|
|
|
2017-11-24 09:49:24 +08:00
|
|
|
if (existing && !existing.is_deleted) {
|
2017-11-24 10:50:12 +08:00
|
|
|
return res.send({
|
2017-11-24 09:49:24 +08:00
|
|
|
success: false,
|
|
|
|
message: 'This note already exists in target parent note.'
|
2017-11-23 12:16:54 +08:00
|
|
|
});
|
|
|
|
}
|
2017-11-24 09:49:24 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2017-11-24 09:49:24 +08:00
|
|
|
// we don't change date_modified so other changes are prioritized in case of conflict
|
2017-12-19 09:58:13 +08:00
|
|
|
// also we would have to sync all those modified note trees otherwise hash checks would fail
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.execute("UPDATE notes_tree SET note_pos = note_pos + 1 WHERE note_pid = ? AND note_pos > ? AND is_deleted = 0",
|
2017-11-24 09:49:24 +08:00
|
|
|
[afterNote.note_pid, afterNote.note_pos]);
|
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addNoteReorderingSync(afterNote.note_pid, sourceId);
|
2017-11-30 10:04:30 +08:00
|
|
|
|
2017-11-24 09:49:24 +08:00
|
|
|
const noteTree = {
|
2017-12-11 01:56:59 +08:00
|
|
|
note_tree_id: utils.newNoteTreeId(),
|
|
|
|
note_id: noteId,
|
|
|
|
note_pid: afterNote.note_pid,
|
|
|
|
note_pos: afterNote.note_pos + 1,
|
|
|
|
is_expanded: 0,
|
|
|
|
date_modified: utils.nowDate(),
|
|
|
|
is_deleted: 0
|
2017-11-24 09:49:24 +08:00
|
|
|
};
|
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.replace("notes_tree", noteTree);
|
2017-11-24 09:49:24 +08:00
|
|
|
|
2017-12-17 09:48:34 +08:00
|
|
|
await sync_table.addNoteTreeSync(noteTree.note_tree_id, sourceId);
|
2017-12-19 09:58:13 +08:00
|
|
|
});
|
2017-11-24 09:49:24 +08:00
|
|
|
|
2017-12-19 09:58:13 +08:00
|
|
|
res.send({
|
|
|
|
success: true
|
2017-11-24 09:49:24 +08:00
|
|
|
});
|
2017-11-23 12:16:54 +08:00
|
|
|
});
|
|
|
|
|
2017-11-24 10:50:12 +08:00
|
|
|
async function checkCycle(parentNoteId, childNoteId) {
|
|
|
|
if (parentNoteId === 'root') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parentNoteId === childNoteId) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-15 11:16:26 +08:00
|
|
|
const parentNoteIds = await sql.getFlattenedResults("SELECT DISTINCT note_pid FROM notes_tree WHERE note_id = ?", [parentNoteId]);
|
2017-11-24 10:50:12 +08:00
|
|
|
|
|
|
|
for (const pid of parentNoteIds) {
|
|
|
|
if (!await checkCycle(pid, childNoteId)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-11-20 07:16:50 +08:00
|
|
|
router.put('/:noteTreeId/expanded/:expanded', async (req, res, next) => {
|
|
|
|
const noteTreeId = req.params.noteTreeId;
|
2017-10-23 10:56:42 +08:00
|
|
|
const expanded = req.params.expanded;
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2017-11-29 06:24:08 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
await sql.execute("UPDATE notes_tree SET is_expanded = ? WHERE note_tree_id = ?", [expanded, noteTreeId]);
|
2017-11-30 10:04:30 +08:00
|
|
|
|
|
|
|
// we don't sync expanded attribute
|
2017-10-30 06:50:28 +08:00
|
|
|
});
|
2017-10-15 11:31:44 +08:00
|
|
|
|
|
|
|
res.send({});
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = router;
|