2018-01-14 07:02:41 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const sql = require('./sql');
|
2018-01-14 09:53:00 +08:00
|
|
|
const sync_table = require('./sync_table');
|
2018-01-14 07:02:41 +08:00
|
|
|
|
|
|
|
async function validateParentChild(res, parentNoteId, childNoteId, noteTreeId = null) {
|
|
|
|
const existing = await getExistingNoteTree(parentNoteId, childNoteId);
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
if (existing && (noteTreeId === null || existing.noteTreeId !== noteTreeId)) {
|
2018-01-14 07:02:41 +08:00
|
|
|
res.send({
|
|
|
|
success: false,
|
|
|
|
message: 'This note already exists in the target.'
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!await checkTreeCycle(parentNoteId, childNoteId)) {
|
|
|
|
res.send({
|
|
|
|
success: false,
|
|
|
|
message: 'Moving note here would create cycle.'
|
|
|
|
});
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getExistingNoteTree(parentNoteId, childNoteId) {
|
2018-01-29 08:30:14 +08:00
|
|
|
return await sql.getFirst('SELECT * FROM notes_tree WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0', [childNoteId, parentNoteId]);
|
2018-01-14 07:02:41 +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 = [];
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
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
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
const parentNoteIds = await sql.getFirstColumn("SELECT DISTINCT parentNoteId FROM notes_tree WHERE noteId = ? AND isDeleted = 0", [parentNoteId]);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
|
|
|
for (const pid of parentNoteIds) {
|
|
|
|
if (!await checkTreeCycleInner(pid)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return await checkTreeCycleInner(parentNoteId);
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getNoteTree(noteTreeId) {
|
2018-01-29 08:30:14 +08:00
|
|
|
return sql.getFirst("SELECT * FROM notes_tree WHERE noteTreeId = ?", [noteTreeId]);
|
2018-01-14 07:02:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
async function loadSubTreeNoteIds(parentNoteId, subTreeNoteIds) {
|
|
|
|
subTreeNoteIds.push(parentNoteId);
|
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
const children = await sql.getFirstColumn("SELECT noteId FROM notes_tree WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId]);
|
2018-01-14 07:02:41 +08:00
|
|
|
|
|
|
|
for (const childNoteId of children) {
|
|
|
|
await loadSubTreeNoteIds(childNoteId, subTreeNoteIds);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-25 11:13:41 +08:00
|
|
|
async function sortNotesAlphabetically(parentNoteId, req, sourceId) {
|
2018-01-14 09:53:00 +08:00
|
|
|
await sql.doInTransaction(async () => {
|
2018-01-29 08:30:14 +08:00
|
|
|
const notes = await sql.getAll(`SELECT noteTreeId, noteId, title, isProtected
|
|
|
|
FROM notes JOIN notes_tree USING(noteId)
|
|
|
|
WHERE notes_tree.isDeleted = 0 AND parentNoteId = ?`, [parentNoteId]);
|
2018-01-14 09:53:00 +08:00
|
|
|
|
2018-01-25 11:13:41 +08:00
|
|
|
protected_session.decryptNotes(req, notes);
|
2018-01-14 09:53:00 +08:00
|
|
|
|
2018-01-29 08:30:14 +08:00
|
|
|
notes.sort((a, b) => a.title.toLowerCase() < b.title.toLowerCase() ? -1 : 1);
|
2018-01-14 09:53:00 +08:00
|
|
|
|
|
|
|
let position = 1;
|
|
|
|
|
|
|
|
for (const note of notes) {
|
2018-01-29 08:30:14 +08:00
|
|
|
await sql.execute("UPDATE notes_tree SET notePosition = ? WHERE noteTreeId = ?",
|
|
|
|
[position, note.noteTreeId]);
|
2018-01-14 09:53:00 +08:00
|
|
|
|
|
|
|
position++;
|
|
|
|
}
|
|
|
|
|
|
|
|
await sync_table.addNoteReorderingSync(parentNoteId, sourceId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-01-14 07:02:41 +08:00
|
|
|
module.exports = {
|
|
|
|
validateParentChild,
|
2018-01-14 09:53:00 +08:00
|
|
|
getNoteTree,
|
|
|
|
sortNotesAlphabetically
|
2018-01-14 07:02:41 +08:00
|
|
|
};
|