2018-01-13 18:02:41 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const sql = require('./sql');
|
2021-03-31 22:35:10 +02:00
|
|
|
const log = require('./log');
|
2023-01-03 13:52:37 +01:00
|
|
|
const BBranch = require('../becca/entities/bbranch');
|
2021-06-29 22:15:57 +02:00
|
|
|
const entityChangesService = require('./entity_changes');
|
2018-04-01 21:27:46 -04:00
|
|
|
const protectedSessionService = require('./protected_session');
|
2021-06-29 22:15:57 +02:00
|
|
|
const becca = require('../becca/becca');
|
2019-11-16 11:23:28 +01:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getNotes(noteIds) {
|
2019-11-16 11:23:28 +01:00
|
|
|
// we return also deleted notes which have been specifically asked for
|
2020-06-20 12:31:38 +02:00
|
|
|
const notes = sql.getManyRows(`
|
2019-11-16 11:23:28 +01:00
|
|
|
SELECT
|
|
|
|
noteId,
|
|
|
|
title,
|
2019-12-30 19:32:45 +01:00
|
|
|
isProtected,
|
2019-11-16 11:23:28 +01:00
|
|
|
type,
|
|
|
|
mime,
|
|
|
|
isDeleted
|
|
|
|
FROM notes
|
|
|
|
WHERE noteId IN (???)`, noteIds);
|
|
|
|
|
|
|
|
protectedSessionService.decryptNotes(notes);
|
|
|
|
|
|
|
|
notes.forEach(note => {
|
2020-01-25 11:52:45 +01:00
|
|
|
note.isProtected = !!note.isProtected
|
2019-11-16 11:23:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return notes;
|
|
|
|
}
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function validateParentChild(parentNoteId, childNoteId, branchId = null) {
|
2022-12-21 16:11:00 +01:00
|
|
|
if (['root', '_hidden', '_share', '_lbRoot', '_lbAvailableLaunchers', '_lbVisibleLaunchers'].includes(childNoteId)) {
|
2022-11-26 14:57:39 +01:00
|
|
|
return { success: false, message: `Cannot change this note's location.`};
|
2018-10-21 22:42:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parentNoteId === 'none') {
|
|
|
|
// this shouldn't happen
|
2022-11-26 14:57:39 +01:00
|
|
|
return { success: false, message: `Cannot move anything into 'none' parent.` };
|
2018-10-21 22:42:20 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-24 12:26:32 +01:00
|
|
|
if (parentNoteId !== '_lbBookmarks' && becca.getNote(parentNoteId).type === 'launcher') {
|
2022-08-08 23:13:31 +02:00
|
|
|
return {
|
|
|
|
success: false,
|
2022-12-01 10:16:57 +01:00
|
|
|
message: 'Launcher note cannot have any children.'
|
2022-08-08 23:13:31 +02: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) {
|
2018-08-31 18:22:53 +02:00
|
|
|
const subtreeNoteIds = [];
|
2018-01-13 18:02:41 -05:00
|
|
|
|
2023-01-15 21:04:17 +01:00
|
|
|
// we'll load the whole subtree - because the cycle can start in one of the notes in the subtree
|
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;
|
|
|
|
}
|
|
|
|
|
2018-08-31 18:22:53 +02:00
|
|
|
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) {
|
2018-08-31 18:22:53 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-30 11:06:10 +08:00
|
|
|
function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, foldersFirst = false, sortNatural = false, sortLocale) {
|
2021-11-23 23:09:29 +01:00
|
|
|
if (!customSortBy) {
|
|
|
|
customSortBy = 'title';
|
2021-09-03 22:33:40 +02:00
|
|
|
}
|
|
|
|
|
2023-03-30 11:06:10 +08:00
|
|
|
if (!sortLocale) {
|
|
|
|
// sortLocale can not be empty string or null value, default value must be set to undefined.
|
|
|
|
sortLocale = undefined;
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
sql.transactional(() => {
|
2021-09-03 22:33:40 +02:00
|
|
|
const notes = becca.getNote(parentNoteId).getChildNotes();
|
2018-01-13 20:53:00 -05:00
|
|
|
|
2021-09-03 22:33:40 +02:00
|
|
|
const normalize = obj => (obj && typeof obj === 'string') ? obj.toLowerCase() : obj;
|
2018-01-13 20:53:00 -05:00
|
|
|
|
2019-10-06 09:49:47 +02:00
|
|
|
notes.sort((a, b) => {
|
2021-09-03 22:33:40 +02:00
|
|
|
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;
|
|
|
|
}
|
2019-10-06 09:49:47 +02:00
|
|
|
}
|
2021-09-03 22:33:40 +02:00
|
|
|
|
2021-11-23 23:09:29 +01:00
|
|
|
function fetchValue(note, key) {
|
|
|
|
const rawValue = ['title', 'dateCreated', 'dateModified'].includes(key)
|
|
|
|
? note[key]
|
|
|
|
: note.getLabelValue(key);
|
2021-09-03 22:33:40 +02:00
|
|
|
|
2021-11-23 23:09:29 +01:00
|
|
|
return normalize(rawValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
function compare(a, b) {
|
2023-03-10 14:35:36 +08:00
|
|
|
if (!sortNatural){
|
|
|
|
// alphabetical sort
|
|
|
|
return b === null || b === undefined || a < b ? -1 : 1;
|
|
|
|
} else {
|
2023-03-30 11:06:10 +08:00
|
|
|
// natural sort
|
|
|
|
return a.localeCompare(b, sortLocale, {numeric: true, sensitivity: 'base'});
|
2023-03-10 14:35:36 +08:00
|
|
|
}
|
|
|
|
|
2021-11-23 23:09:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
2019-10-06 09:49:47 +02:00
|
|
|
});
|
2018-01-13 20:53:00 -05:00
|
|
|
|
2021-02-28 23:40:15 +01:00
|
|
|
if (reverse) {
|
|
|
|
notes.reverse();
|
|
|
|
}
|
|
|
|
|
2019-10-19 12:36:16 +02:00
|
|
|
let position = 10;
|
2022-12-14 23:44:26 +01:00
|
|
|
let someBranchUpdated = false;
|
2018-01-13 20:53:00 -05:00
|
|
|
|
|
|
|
for (const note of notes) {
|
2021-12-20 17:30:47 +01:00
|
|
|
const branch = note.getParentBranches().find(b => b.parentNoteId === parentNoteId);
|
2021-09-03 22:33:40 +02:00
|
|
|
|
2022-12-23 20:40:58 +01:00
|
|
|
if (branch.noteId === '_hidden') {
|
2022-11-26 14:57:39 +01:00
|
|
|
position = 999_999_999;
|
|
|
|
}
|
|
|
|
|
2022-12-14 23:44:26 +01:00
|
|
|
if (branch.notePosition !== position) {
|
|
|
|
sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?",
|
|
|
|
[position, branch.branchId]);
|
2018-01-13 20:53:00 -05:00
|
|
|
|
2022-12-14 23:44:26 +01:00
|
|
|
branch.notePosition = position;
|
|
|
|
someBranchUpdated = true;
|
|
|
|
}
|
2021-02-20 21:28:22 +01:00
|
|
|
|
2019-10-19 12:36:16 +02:00
|
|
|
position += 10;
|
2018-01-13 20:53:00 -05:00
|
|
|
}
|
|
|
|
|
2022-12-14 23:44:26 +01:00
|
|
|
if (someBranchUpdated) {
|
|
|
|
entityChangesService.addNoteReorderingEntityChange(parentNoteId);
|
|
|
|
}
|
2018-01-13 20:53:00 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-09-03 22:33:40 +02:00
|
|
|
function sortNotesIfNeeded(parentNoteId) {
|
|
|
|
const parentNote = becca.getNote(parentNoteId);
|
2021-02-28 23:40:15 +01:00
|
|
|
|
2021-09-03 22:33:40 +02:00
|
|
|
if (!parentNote) {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-28 23:40:15 +01:00
|
|
|
|
2021-09-03 22:33:40 +02:00
|
|
|
const sortedLabel = parentNote.getLabel('sorted');
|
2021-02-28 23:40:15 +01:00
|
|
|
|
2021-09-03 22:33:40 +02:00
|
|
|
if (!sortedLabel || sortedLabel.value === 'off') {
|
|
|
|
return;
|
|
|
|
}
|
2021-02-28 23:40:15 +01:00
|
|
|
|
2022-08-07 23:51:23 +02:00
|
|
|
const sortReversed = parentNote.getLabelValue('sortDirection')?.toLowerCase() === "desc";
|
|
|
|
const sortFoldersFirstLabel = parentNote.getLabel('sortFoldersFirst');
|
|
|
|
const sortFoldersFirst = sortFoldersFirstLabel && sortFoldersFirstLabel.value.toLowerCase() !== "false";
|
2023-03-10 14:35:36 +08:00
|
|
|
const sortNaturalLabel = parentNote.getLabel('sortNatural');
|
|
|
|
const sortNatural = sortNaturalLabel && sortNaturalLabel.value.toLowerCase() !== "false";
|
2023-03-30 11:06:10 +08:00
|
|
|
const sortLocale = parentNote.getLabelValue('sortLocale');
|
|
|
|
|
|
|
|
sortNotes(parentNoteId, sortedLabel.value, sortReversed, sortFoldersFirst, sortNatural, sortLocale);
|
2021-02-28 23:40:15 +01:00
|
|
|
}
|
|
|
|
|
2019-11-10 19:29:51 +01:00
|
|
|
/**
|
2023-01-05 23:38:41 +01:00
|
|
|
* @deprecated this will be removed in the future
|
2019-11-10 19:29:51 +01:00
|
|
|
*/
|
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);
|
2019-01-17 23:24:59 +01:00
|
|
|
|
2019-01-20 16:33:09 +01:00
|
|
|
if (parentNote && parentNote.isDeleted) {
|
|
|
|
throw new Error(`Cannot move note to deleted parent note ${parentNoteId}`);
|
2019-01-17 23:24:59 +01:00
|
|
|
}
|
|
|
|
|
2018-08-13 13:53:08 +02:00
|
|
|
// 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);
|
2018-08-13 13:53:08 +02:00
|
|
|
|
|
|
|
if (branch) {
|
|
|
|
if (!parentNoteId) {
|
2022-01-31 22:09:39 +01:00
|
|
|
log.info(`Removing note ${noteId} from parent ${parentNoteId}`);
|
|
|
|
|
2021-05-02 20:32:50 +02:00
|
|
|
branch.markAsDeleted();
|
2018-08-13 13:53:08 +02:00
|
|
|
}
|
|
|
|
else {
|
2022-01-31 22:09:39 +01:00
|
|
|
const newBranch = branch.createClone(parentNoteId);
|
|
|
|
newBranch.save();
|
|
|
|
|
|
|
|
branch.markAsDeleted();
|
2018-08-13 13:53:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (parentNoteId) {
|
2021-05-02 11:23:58 +02:00
|
|
|
const note = becca.getNote(noteId);
|
2019-01-21 21:55:40 +01:00
|
|
|
|
|
|
|
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 {
|
2023-01-03 13:52:37 +01:00
|
|
|
new BBranch({
|
2019-11-10 19:29:51 +01:00
|
|
|
noteId: noteId,
|
|
|
|
parentNoteId: parentNoteId,
|
|
|
|
prefix: prefix
|
|
|
|
}).save();
|
|
|
|
}
|
2018-08-13 13:53:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-13 18:02:41 -05:00
|
|
|
module.exports = {
|
2019-11-16 11:23:28 +01:00
|
|
|
getNotes,
|
2018-01-13 18:02:41 -05:00
|
|
|
validateParentChild,
|
2021-02-28 23:40:15 +01:00
|
|
|
sortNotes,
|
2021-09-03 22:33:40 +02:00
|
|
|
sortNotesIfNeeded,
|
2020-01-25 11:52:45 +01:00
|
|
|
setNoteToParent
|
2020-05-16 23:12:29 +02:00
|
|
|
};
|