Notes/src/services/tree.js

200 lines
6.3 KiB
JavaScript
Raw Normal View History

2018-01-13 18:02:41 -05:00
"use strict";
const sql = require('./sql');
const repository = require('./repository');
const Branch = require('../entities/branch');
const entityChangesService = require('./entity_changes.js');
const protectedSessionService = require('./protected_session');
const noteCache = require('./note_cache/note_cache');
2020-06-20 12:31:38 +02:00
function getNotes(noteIds) {
// we return also deleted notes which have been specifically asked for
2020-06-20 12:31:38 +02:00
const notes = sql.getManyRows(`
SELECT
noteId,
title,
2019-12-30 19:32:45 +01:00
isProtected,
type,
mime,
isDeleted
FROM notes
WHERE noteId IN (???)`, noteIds);
protectedSessionService.decryptNotes(notes);
notes.forEach(note => {
note.isProtected = !!note.isProtected
});
return notes;
}
2018-01-13 18:02:41 -05:00
2020-06-20 12:31:38 +02:00
function validateParentChild(parentNoteId, childNoteId, branchId = null) {
if (childNoteId === 'root') {
return { success: false, message: 'Cannot move root note.'};
}
if (parentNoteId === 'none') {
// this shouldn't happen
return { success: false, message: 'Cannot move anything into root parent.' };
}
2020-06-20 12:31:38 +02:00
const existing = getExistingBranch(parentNoteId, childNoteId);
2018-01-13 18:02:41 -05:00
2018-03-24 21:39:15 -04:00
if (existing && (branchId === null || existing.branchId !== branchId)) {
2018-03-30 12:57:22 -04:00
return {
2018-01-13 18:02:41 -05:00
success: false,
message: 'This note already exists in the target.'
2018-03-30 12:57:22 -04:00
};
2018-01-13 18:02:41 -05:00
}
2020-06-20 12:31:38 +02:00
if (!checkTreeCycle(parentNoteId, childNoteId)) {
2018-03-30 12:57:22 -04:00
return {
2018-01-13 18:02:41 -05:00
success: false,
2018-08-30 22:38:34 +02:00
message: 'Moving/cloning note here would create cycle.'
2018-03-30 12:57:22 -04:00
};
2018-01-13 18:02:41 -05:00
}
2018-03-30 12:57:22 -04:00
return { success: true };
2018-01-13 18:02:41 -05:00
}
2020-06-20 12:31:38 +02:00
function getExistingBranch(parentNoteId, childNoteId) {
return repository.getEntity('SELECT * FROM branches WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0', [childNoteId, parentNoteId]);
2018-01-13 18:02:41 -05:00
}
/**
* Tree cycle can be created when cloning or when moving existing clone. This method should detect both cases.
*/
2020-06-20 12:31:38 +02:00
function checkTreeCycle(parentNoteId, childNoteId) {
const subtreeNoteIds = [];
2018-01-13 18:02:41 -05:00
// we'll load the whole sub tree - because the cycle can start in one of the notes in the sub tree
2020-06-20 12:31:38 +02:00
loadSubtreeNoteIds(childNoteId, subtreeNoteIds);
2018-01-13 18:02:41 -05:00
2020-06-20 12:31:38 +02:00
function checkTreeCycleInner(parentNoteId) {
2018-01-13 18:02:41 -05:00
if (parentNoteId === 'root') {
return true;
}
if (subtreeNoteIds.includes(parentNoteId)) {
2018-01-13 18:02:41 -05:00
// 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;
}
2020-06-20 12:31:38 +02:00
const parentNoteIds = sql.getColumn("SELECT DISTINCT parentNoteId FROM branches WHERE noteId = ? AND isDeleted = 0", [parentNoteId]);
2018-01-13 18:02:41 -05:00
for (const pid of parentNoteIds) {
2020-06-20 12:31:38 +02:00
if (!checkTreeCycleInner(pid)) {
2018-01-13 18:02:41 -05:00
return false;
}
}
return true;
}
2020-06-20 12:31:38 +02:00
return checkTreeCycleInner(parentNoteId);
2018-01-13 18:02:41 -05:00
}
2020-06-20 12:31:38 +02:00
function loadSubtreeNoteIds(parentNoteId, subtreeNoteIds) {
subtreeNoteIds.push(parentNoteId);
2018-01-13 18:02:41 -05:00
2020-06-20 12:31:38 +02:00
const children = sql.getColumn("SELECT noteId FROM branches WHERE parentNoteId = ? AND isDeleted = 0", [parentNoteId]);
2018-01-13 18:02:41 -05:00
for (const childNoteId of children) {
2020-06-20 12:31:38 +02:00
loadSubtreeNoteIds(childNoteId, subtreeNoteIds);
2018-01-13 18:02:41 -05:00
}
}
2020-06-20 12:31:38 +02:00
function sortNotesAlphabetically(parentNoteId, directoriesFirst = false) {
sql.transactional(() => {
const notes = sql.getRows(
`SELECT branches.branchId, notes.noteId, title, isProtected,
CASE WHEN COUNT(childBranches.noteId) > 0 THEN 1 ELSE 0 END AS hasChildren
FROM notes
JOIN branches ON branches.noteId = notes.noteId
LEFT JOIN branches childBranches ON childBranches.parentNoteId = notes.noteId AND childBranches.isDeleted = 0
WHERE branches.isDeleted = 0 AND branches.parentNoteId = ?
GROUP BY notes.noteId`, [parentNoteId]);
protectedSessionService.decryptNotes(notes);
notes.sort((a, b) => {
if (directoriesFirst && ((a.hasChildren && !b.hasChildren) || (!a.hasChildren && b.hasChildren))) {
// exactly one note of the two is a directory so the sorting will be done based on this status
return a.hasChildren ? -1 : 1;
}
else {
return a.title.toLowerCase() < b.title.toLowerCase() ? -1 : 1;
}
});
2019-10-19 12:36:16 +02:00
let position = 10;
for (const note of notes) {
2020-06-20 12:31:38 +02:00
sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?",
2018-03-24 21:39:15 -04:00
[position, note.branchId]);
noteCache.branches[note.branchId].notePosition = position;
2019-10-19 12:36:16 +02:00
position += 10;
}
entityChangesService.addNoteReorderingEntityChange(parentNoteId);
});
}
2019-11-10 19:29:51 +01:00
/**
* @deprecated - this will be removed in the future
*/
2020-06-20 12:31:38 +02:00
function setNoteToParent(noteId, prefix, parentNoteId) {
const parentNote = repository.getNote(parentNoteId);
if (parentNote && parentNote.isDeleted) {
throw new Error(`Cannot move note to deleted parent note ${parentNoteId}`);
}
// case where there might be more such branches is ignored. It's expected there should be just one
2020-06-20 12:31:38 +02:00
const branch = repository.getEntity("SELECT * FROM branches WHERE isDeleted = 0 AND noteId = ? AND prefix = ?", [noteId, prefix]);
if (branch) {
if (!parentNoteId) {
branch.isDeleted = true;
}
else {
branch.parentNoteId = parentNoteId;
2018-08-23 10:10:04 +02:00
branch.prefix = prefix;
}
2020-06-20 12:31:38 +02:00
branch.save();
}
else if (parentNoteId) {
2020-06-20 12:31:38 +02:00
const note = repository.getNote(noteId);
if (note.isDeleted) {
throw new Error(`Cannot create a branch for ${noteId} which is deleted.`);
}
2020-06-20 12:31:38 +02:00
const branch = repository.getEntity('SELECT * FROM branches WHERE isDeleted = 0 AND noteId = ? AND parentNoteId = ?', [noteId, parentNoteId]);
2019-11-10 19:29:51 +01:00
if (branch) {
branch.prefix = prefix;
2020-06-20 12:31:38 +02:00
branch.save();
2019-11-10 19:29:51 +01:00
}
else {
2020-06-20 12:31:38 +02:00
new Branch({
2019-11-10 19:29:51 +01:00
noteId: noteId,
parentNoteId: parentNoteId,
prefix: prefix
}).save();
}
}
}
2018-01-13 18:02:41 -05:00
module.exports = {
getNotes,
2018-01-13 18:02:41 -05:00
validateParentChild,
sortNotesAlphabetically,
setNoteToParent
2020-05-16 23:12:29 +02:00
};