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 auth = require('../../services/auth');
|
2018-01-04 11:43:01 +08:00
|
|
|
const utils = require('../../services/utils');
|
|
|
|
const sync_table = require('../../services/sync_table');
|
2018-01-07 22:35:44 +08:00
|
|
|
const wrap = require('express-promise-wrap').wrap;
|
2018-01-04 11:36:27 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Code in this file deals with moving and cloning note tree rows. Relationship between note and parent note is unique
|
|
|
|
* for not deleted note trees. There may be multiple deleted note-parent note relationships.
|
|
|
|
*/
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.put('/:noteTreeId/move-to/:parentNoteId', auth.checkApiAuth, wrap(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
|
|
|
|
2018-01-04 11:13:02 +08:00
|
|
|
const noteToMove = await getNoteTree(noteTreeId);
|
2018-01-02 11:28:19 +08:00
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
if (!await validateParentChild(res, parentNoteId, noteToMove.note_id, noteTreeId)) {
|
|
|
|
return;
|
2018-01-02 11:28:19 +08:00
|
|
|
}
|
|
|
|
|
2017-12-24 00:02:38 +08:00
|
|
|
const maxNotePos = await sql.getFirstValue('SELECT MAX(note_position) FROM notes_tree WHERE parent_note_id = ? 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 () => {
|
2017-12-20 10:40:48 +08:00
|
|
|
await sql.execute("UPDATE notes_tree SET parent_note_id = ?, note_position = ?, 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
|
|
|
|
2018-01-02 11:28:19 +08:00
|
|
|
res.send({ success: true });
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.put('/:noteTreeId/move-before/:beforeNoteTreeId', auth.checkApiAuth, wrap(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
|
|
|
|
2018-01-04 11:13:02 +08:00
|
|
|
const noteToMove = await getNoteTree(noteTreeId);
|
|
|
|
const beforeNote = await getNoteTree(beforeNoteTreeId);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
if (!await validateParentChild(res, beforeNote.parent_note_id, noteToMove.note_id, noteTreeId)) {
|
|
|
|
return;
|
2018-01-02 11:28:19 +08:00
|
|
|
}
|
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
// we don't change date_modified so other changes are prioritized in case of conflict
|
|
|
|
// also we would have to sync all those modified note trees otherwise hash checks would fail
|
|
|
|
await sql.execute("UPDATE notes_tree SET note_position = note_position + 1 WHERE parent_note_id = ? AND note_position >= ? AND is_deleted = 0",
|
|
|
|
[beforeNote.parent_note_id, beforeNote.note_position]);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
await sync_table.addNoteReorderingSync(beforeNote.parent_note_id, sourceId);
|
2017-11-30 10:04:30 +08:00
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
const now = utils.nowDate();
|
2017-11-10 10:11:33 +08:00
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
await sql.execute("UPDATE notes_tree SET parent_note_id = ?, note_position = ?, date_modified = ? WHERE note_tree_id = ?",
|
|
|
|
[beforeNote.parent_note_id, beforeNote.note_position, now, noteTreeId]);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
await sync_table.addNoteTreeSync(noteTreeId, sourceId);
|
|
|
|
});
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
res.send({ success: true });
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.put('/:noteTreeId/move-after/:afterNoteTreeId', auth.checkApiAuth, wrap(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
|
|
|
|
2018-01-04 11:13:02 +08:00
|
|
|
const noteToMove = await getNoteTree(noteTreeId);
|
|
|
|
const afterNote = await getNoteTree(afterNoteTreeId);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
if (!await validateParentChild(res, afterNote.parent_note_id, noteToMove.note_id, noteTreeId)) {
|
|
|
|
return;
|
2018-01-02 11:28:19 +08:00
|
|
|
}
|
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
|
|
|
// we don't change date_modified so other changes are prioritized in case of conflict
|
|
|
|
// also we would have to sync all those modified note trees otherwise hash checks would fail
|
|
|
|
await sql.execute("UPDATE notes_tree SET note_position = note_position + 1 WHERE parent_note_id = ? AND note_position > ? AND is_deleted = 0",
|
|
|
|
[afterNote.parent_note_id, afterNote.note_position]);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
await sync_table.addNoteReorderingSync(afterNote.parent_note_id, sourceId);
|
2017-11-10 10:11:33 +08:00
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
await sql.execute("UPDATE notes_tree SET parent_note_id = ?, note_position = ?, date_modified = ? WHERE note_tree_id = ?",
|
|
|
|
[afterNote.parent_note_id, afterNote.note_position + 1, utils.nowDate(), noteTreeId]);
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
await sync_table.addNoteTreeSync(noteTreeId, sourceId);
|
|
|
|
});
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
res.send({ success: true });
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-10-15 11:31:44 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-11-23 12:16:54 +08:00
|
|
|
const parentNoteId = req.params.parentNoteId;
|
|
|
|
const childNoteId = req.params.childNoteId;
|
2017-12-22 10:54:25 +08:00
|
|
|
const prefix = req.body.prefix;
|
2017-12-17 09:48:34 +08:00
|
|
|
const sourceId = req.headers.source_id;
|
2017-11-23 12:16:54 +08:00
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
if (!await validateParentChild(res, parentNoteId, childNoteId)) {
|
|
|
|
return;
|
2017-11-24 09:49:24 +08:00
|
|
|
}
|
2017-11-23 12:16:54 +08:00
|
|
|
|
2017-12-24 00:02:38 +08:00
|
|
|
const maxNotePos = await sql.getFirstValue('SELECT MAX(note_position) FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0', [parentNoteId]);
|
2017-11-24 09:49:24 +08:00
|
|
|
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,
|
2017-12-20 10:40:48 +08:00
|
|
|
parent_note_id: parentNoteId,
|
2017-12-22 10:54:25 +08:00
|
|
|
prefix: prefix,
|
2017-12-20 10:40:48 +08:00
|
|
|
note_position: newNotePos,
|
2017-12-11 01:56:59 +08:00
|
|
|
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-23 21:24:03 +08:00
|
|
|
|
|
|
|
await sql.execute("UPDATE notes_tree SET is_expanded = 1 WHERE note_id = ?", [parentNoteId]);
|
2017-12-19 09:58:13 +08:00
|
|
|
});
|
2017-11-24 09:49:24 +08:00
|
|
|
|
2018-01-02 11:28:19 +08:00
|
|
|
res.send({ success: true });
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-11-23 12:16:54 +08:00
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.put('/:noteId/clone-after/:afterNoteTreeId', auth.checkApiAuth, wrap(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
|
|
|
|
2018-01-04 11:13:02 +08:00
|
|
|
const afterNote = await getNoteTree(afterNoteTreeId);
|
2017-11-23 12:16:54 +08:00
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
if (!await validateParentChild(res, afterNote.parent_note_id, noteId)) {
|
|
|
|
return;
|
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-12-20 10:40:48 +08:00
|
|
|
await sql.execute("UPDATE notes_tree SET note_position = note_position + 1 WHERE parent_note_id = ? AND note_position > ? AND is_deleted = 0",
|
|
|
|
[afterNote.parent_note_id, afterNote.note_position]);
|
2017-11-24 09:49:24 +08:00
|
|
|
|
2017-12-20 10:40:48 +08:00
|
|
|
await sync_table.addNoteReorderingSync(afterNote.parent_note_id, 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,
|
2017-12-20 10:40:48 +08:00
|
|
|
parent_note_id: afterNote.parent_note_id,
|
|
|
|
note_position: afterNote.note_position + 1,
|
2017-12-11 01:56:59 +08:00
|
|
|
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
|
|
|
|
2018-01-02 11:28:19 +08:00
|
|
|
res.send({ success: true });
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-11-23 12:16:54 +08:00
|
|
|
|
2018-01-02 11:28:19 +08:00
|
|
|
async function loadSubTreeNoteIds(parentNoteId, subTreeNoteIds) {
|
|
|
|
subTreeNoteIds.push(parentNoteId);
|
|
|
|
|
2018-01-04 11:13:02 +08:00
|
|
|
const children = await sql.getFirstColumn("SELECT note_id FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0", [parentNoteId]);
|
2017-11-24 10:50:12 +08:00
|
|
|
|
2018-01-02 11:28:19 +08:00
|
|
|
for (const childNoteId of children) {
|
|
|
|
await loadSubTreeNoteIds(childNoteId, subTreeNoteIds);
|
2017-11-24 10:50:12 +08:00
|
|
|
}
|
2018-01-02 11:28:19 +08:00
|
|
|
}
|
|
|
|
|
2018-01-04 11:13:02 +08:00
|
|
|
async function getNoteTree(noteTreeId) {
|
|
|
|
return sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]);
|
|
|
|
}
|
|
|
|
|
2018-01-04 10:29:13 +08:00
|
|
|
async function validateParentChild(res, parentNoteId, childNoteId, noteTreeId = null) {
|
|
|
|
const existing = await getExistingNoteTree(parentNoteId, childNoteId);
|
|
|
|
|
|
|
|
if (existing && (noteTreeId === null || existing.note_tree_id !== noteTreeId)) {
|
|
|
|
res.send({
|
|
|
|
success: false,
|
2018-01-05 10:37:36 +08:00
|
|
|
message: 'This note already exists in the target.'
|
2018-01-04 10:29:13 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!await checkTreeCycle(parentNoteId, childNoteId)) {
|
|
|
|
res.send({
|
|
|
|
success: false,
|
|
|
|
message: 'Moving note here would create cycle.'
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-01-03 08:56:45 +08:00
|
|
|
async function getExistingNoteTree(parentNoteId, childNoteId) {
|
2018-01-04 10:29:13 +08:00
|
|
|
return await sql.getFirst('SELECT * FROM notes_tree WHERE note_id = ? AND parent_note_id = ? AND is_deleted = 0', [childNoteId, parentNoteId]);
|
2018-01-03 08:56:45 +08:00
|
|
|
}
|
|
|
|
|
2018-01-02 11:28:19 +08:00
|
|
|
/**
|
|
|
|
* Tree cycle can be created when cloning or when moving existing clone. This method should detect both cases.
|
|
|
|
*/
|
|
|
|
async function checkTreeCycle(parentNoteId, childNoteId) {
|
|
|
|
const subTreeNoteIds = [];
|
2017-11-24 10:50:12 +08:00
|
|
|
|
2018-01-02 11:28:19 +08:00
|
|
|
// we'll load the whole sub tree - because the cycle can start in one of the notes in the sub tree
|
|
|
|
await loadSubTreeNoteIds(childNoteId, subTreeNoteIds);
|
2017-11-24 10:50:12 +08:00
|
|
|
|
2018-01-02 11:28:19 +08:00
|
|
|
async function checkTreeCycleInner(parentNoteId) {
|
|
|
|
if (parentNoteId === 'root') {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (subTreeNoteIds.includes(parentNoteId)) {
|
|
|
|
// while towards the root of the tree we encountered noteId which is already present in the subtree
|
|
|
|
// joining parentNoteId with childNoteId would then clearly create a cycle
|
2017-11-24 10:50:12 +08:00
|
|
|
return false;
|
|
|
|
}
|
2018-01-02 11:28:19 +08:00
|
|
|
|
2018-01-04 11:13:02 +08:00
|
|
|
const parentNoteIds = await sql.getFirstColumn("SELECT DISTINCT parent_note_id FROM notes_tree WHERE note_id = ? AND is_deleted = 0", [parentNoteId]);
|
2018-01-02 11:28:19 +08:00
|
|
|
|
|
|
|
for (const pid of parentNoteIds) {
|
|
|
|
if (!await checkTreeCycleInner(pid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2017-11-24 10:50:12 +08:00
|
|
|
}
|
|
|
|
|
2018-01-02 11:28:19 +08:00
|
|
|
return await checkTreeCycleInner(parentNoteId);
|
2017-11-24 10:50:12 +08:00
|
|
|
}
|
|
|
|
|
2018-01-07 22:35:44 +08:00
|
|
|
router.put('/:noteTreeId/expanded/:expanded', auth.checkApiAuth, wrap(async (req, res, next) => {
|
2017-11-20 07:16:50 +08:00
|
|
|
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({});
|
2018-01-07 22:35:44 +08:00
|
|
|
}));
|
2017-10-15 11:31:44 +08:00
|
|
|
|
|
|
|
module.exports = router;
|