Notes/src/services/tree.js

265 lines
7.8 KiB
JavaScript
Raw Normal View History

2018-01-13 18:02:41 -05:00
"use strict";
const sql = require('./sql');
const log = require('./log');
2021-06-29 22:15:57 +02:00
const Branch = require('../becca/entities/branch');
const entityChangesService = require('./entity_changes');
const protectedSessionService = require('./protected_session');
2021-06-29 22:15:57 +02:00
const becca = require('../becca/becca');
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)) {
2021-12-23 20:54:48 +01:00
const parentNote = becca.getNote(parentNoteId);
const childNote = becca.getNote(childNoteId);
2018-03-30 12:57:22 -04:00
return {
2018-01-13 18:02:41 -05:00
success: false,
2021-12-23 20:54:48 +01:00
message: `Note "${childNote.title}" note already exists in the "${parentNote.title}".`
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) {
2021-12-23 20:54:48 +01:00
const branchId = sql.getValue(`
SELECT branchId
FROM branches
WHERE noteId = ?
AND parentNoteId = ?
AND isDeleted = 0`, [childNoteId, parentNoteId]);
2021-05-11 22:00:16 +02:00
return becca.getBranch(branchId);
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
}
}
function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, foldersFirst = false) {
if (!customSortBy) {
customSortBy = 'title';
}
2020-06-20 12:31:38 +02:00
sql.transactional(() => {
const notes = becca.getNote(parentNoteId).getChildNotes();
const normalize = obj => (obj && typeof obj === 'string') ? obj.toLowerCase() : obj;
notes.sort((a, b) => {
if (foldersFirst) {
const aHasChildren = a.hasChildren();
const bHasChildren = b.hasChildren();
if ((aHasChildren && !bHasChildren) || (!aHasChildren && bHasChildren)) {
// exactly one note of the two is a directory so the sorting will be done based on this status
return aHasChildren ? -1 : 1;
}
}
function fetchValue(note, key) {
const rawValue = ['title', 'dateCreated', 'dateModified'].includes(key)
? note[key]
: note.getLabelValue(key);
return normalize(rawValue);
}
function compare(a, b) {
return b === null || b === undefined || a < b ? -1 : 1;
}
const topAEl = fetchValue(a, 'top');
const topBEl = fetchValue(b, 'top');
if (topAEl !== topBEl) {
// since "top" should not be reversible, we'll reverse it once more to nullify this effect
return compare(topAEl, topBEl) * (reverse ? -1 : 1);
}
const customAEl = fetchValue(a, customSortBy);
const customBEl = fetchValue(b, customSortBy);
if (customAEl !== customBEl) {
return compare(customAEl, customBEl);
}
const titleAEl = fetchValue(a, 'title');
const titleBEl = fetchValue(b, 'title');
return compare(titleAEl, titleBEl);
});
2021-02-28 23:40:15 +01:00
if (reverse) {
notes.reverse();
}
2019-10-19 12:36:16 +02:00
let position = 10;
for (const note of notes) {
2021-12-20 17:30:47 +01:00
const branch = note.getParentBranches().find(b => b.parentNoteId === parentNoteId);
2020-06-20 12:31:38 +02:00
sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?",
[position, branch.branchId]);
becca.branches[branch.branchId].notePosition = position;
2019-10-19 12:36:16 +02:00
position += 10;
}
entityChangesService.addNoteReorderingEntityChange(parentNoteId);
});
}
function sortNotesIfNeeded(parentNoteId) {
const parentNote = becca.getNote(parentNoteId);
2021-02-28 23:40:15 +01:00
if (!parentNote) {
return;
}
2021-02-28 23:40:15 +01:00
const sortedLabel = parentNote.getLabel('sorted');
2021-02-28 23:40:15 +01:00
if (!sortedLabel || sortedLabel.value === 'off') {
return;
}
2021-02-28 23:40:15 +01:00
sortNotes(parentNoteId, sortedLabel.value);
2021-02-28 23:40:15 +01:00
}
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) {
2021-05-02 11:23:58 +02:00
const parentNote = becca.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
2021-05-11 22:00:16 +02:00
const branchId = sql.getValue("SELECT branchId FROM branches WHERE isDeleted = 0 AND noteId = ? AND prefix = ?", [noteId, prefix]);
const branch = becca.getBranch(branchId);
if (branch) {
if (!parentNoteId) {
branch.markAsDeleted();
}
else {
branch.parentNoteId = parentNoteId;
2018-08-23 10:10:04 +02:00
branch.prefix = prefix;
branch.save();
}
}
else if (parentNoteId) {
2021-05-02 11:23:58 +02:00
const note = becca.getNote(noteId);
if (note.isDeleted) {
throw new Error(`Cannot create a branch for ${noteId} which is deleted.`);
}
2021-05-11 22:00:16 +02:00
const branchId = sql.getValue('SELECT branchId FROM branches WHERE isDeleted = 0 AND noteId = ? AND parentNoteId = ?', [noteId, parentNoteId]);
const branch = becca.getBranch(branchId);
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,
2021-02-28 23:40:15 +01:00
sortNotes,
sortNotesIfNeeded,
setNoteToParent
2020-05-16 23:12:29 +02:00
};