Notes/src/services/cloning.js

186 lines
6.0 KiB
JavaScript
Raw Normal View History

"use strict";
const sql = require('./sql');
2021-06-29 22:15:57 +02:00
const eventChangesService = require('./entity_changes');
2018-08-11 19:45:55 +02:00
const treeService = require('./tree');
const BBranch = require('../becca/entities/bbranch');
2021-06-29 22:15:57 +02:00
const becca = require("../becca/becca");
2022-02-05 12:06:23 +01:00
const log = require("./log");
2023-04-15 00:06:13 +02:00
function cloneNoteToParentNote(noteId, parentNoteId, prefix) {
2023-06-05 09:23:42 +02:00
if (!(noteId in becca.notes) || !(parentNoteId in becca.notes)) {
return { success: false, message: 'Note cannot be cloned because either the cloned note or the intended parent is deleted.' };
}
const parentNote = becca.getNote(parentNoteId);
if (parentNote.type === 'search') {
return {
success: false,
message: "Can't clone into a search note"
};
}
const validationResult = treeService.validateParentChild(parentNoteId, noteId);
if (!validationResult.success) {
return validationResult;
}
const branch = new BBranch({
noteId: noteId,
parentNoteId: parentNoteId,
prefix: prefix,
isExpanded: 0
}).save();
2023-04-15 00:06:13 +02:00
log.info(`Cloned note '${noteId}' to a new parent note '${parentNoteId}' with prefix '${prefix}'`);
2022-02-05 12:06:23 +01:00
2021-06-06 11:01:10 +02:00
return {
success: true,
branchId: branch.branchId,
2023-04-16 11:28:24 +02:00
notePath: `${parentNote.getBestNotePathString()}/${noteId}`
2021-06-06 11:01:10 +02:00
};
}
function cloneNoteToBranch(noteId, parentBranchId, prefix) {
const parentBranch = becca.getBranch(parentBranchId);
if (!parentBranch) {
2023-05-04 22:16:18 +02:00
return { success: false, message: `Parent branch '${parentBranchId}' does not exist.` };
}
2023-04-15 00:06:13 +02:00
const ret = cloneNoteToParentNote(noteId, parentBranch.noteId, prefix);
parentBranch.isExpanded = true; // the new target should be expanded, so it immediately shows up to the user
parentBranch.save();
return ret;
}
2020-06-20 12:31:38 +02:00
function ensureNoteIsPresentInParent(noteId, parentNoteId, prefix) {
2023-06-29 23:32:19 +02:00
if (!(noteId in becca.notes)) {
return { branch: null, success: false, message: `Note '${noteId}' is deleted.` };
2023-06-29 23:32:19 +02:00
} else if (!(parentNoteId in becca.notes)) {
return { branch: null, success: false, message: `Note '${parentNoteId}' is deleted.` };
}
const parentNote = becca.getNote(parentNoteId);
if (parentNote.type === 'search') {
return { branch: null, success: false, message: "Can't clone into a search note" };
}
2020-06-20 12:31:38 +02:00
const validationResult = treeService.validateParentChild(parentNoteId, noteId);
2018-08-13 10:59:31 +02:00
if (!validationResult.success) {
return validationResult;
}
const branch = new BBranch({
2018-08-13 10:59:31 +02:00
noteId: noteId,
parentNoteId: parentNoteId,
prefix: prefix,
isExpanded: 0
}).save();
2022-02-05 12:06:23 +01:00
2022-12-27 21:17:40 +01:00
log.info(`Ensured note '${noteId}' is in parent note '${parentNoteId}' with prefix '${branch.prefix}'`);
return { branch: branch, success: true };
2018-08-11 19:45:55 +02:00
}
2020-06-20 12:31:38 +02:00
function ensureNoteIsAbsentFromParent(noteId, parentNoteId) {
2021-05-11 22:00:16 +02:00
const branchId = sql.getValue(`SELECT branchId FROM branches WHERE noteId = ? AND parentNoteId = ? AND isDeleted = 0`, [noteId, parentNoteId]);
const branch = becca.getBranch(branchId);
2018-08-11 19:45:55 +02:00
if (branch) {
2022-12-04 13:16:05 +01:00
if (!branch.isWeak && branch.getNote().getStrongParentBranches().length <= 1) {
return {
success: false,
message: `Cannot remove branch '${branch.branchId}' between child '${noteId}' and parent '${parentNoteId}' because this would delete the note as well.`
};
2022-01-31 22:09:39 +01:00
}
branch.deleteBranch();
2022-02-05 12:06:23 +01:00
2022-12-04 13:16:05 +01:00
log.info(`Ensured note '${noteId}' is NOT in parent note '${parentNoteId}'`);
return { success: true };
2018-08-11 19:45:55 +02:00
}
}
2020-06-20 12:31:38 +02:00
function toggleNoteInParent(present, noteId, parentNoteId, prefix) {
if (present) {
2022-12-04 13:16:05 +01:00
return ensureNoteIsPresentInParent(noteId, parentNoteId, prefix);
}
else {
2022-12-04 13:16:05 +01:00
return ensureNoteIsAbsentFromParent(noteId, parentNoteId);
}
}
2020-06-20 12:31:38 +02:00
function cloneNoteAfter(noteId, afterBranchId) {
2022-12-21 16:11:00 +01:00
if (['_hidden', 'root'].includes(noteId)) {
return { success: false, message: `Cloning the note '${noteId}' is forbidden.` };
}
const afterBranch = becca.getBranch(afterBranchId);
if (!afterBranch) {
return { success: false, message: `Branch '${afterBranchId}' does not exist.` };
}
if (afterBranch.noteId === '_hidden') {
2022-11-26 14:57:39 +01:00
return { success: false, message: 'Cannot clone after the hidden branch.' };
}
2021-05-02 11:23:58 +02:00
const afterNote = becca.getBranch(afterBranchId);
2023-06-29 22:10:13 +02:00
if (!(noteId in becca.notes)) {
return { success: false, message: `Note to be cloned '${noteId}' is deleted or does not exist.` };
} else if (!(afterNote.parentNoteId in becca.notes)) {
return { success: false, message: `After note '${afterNote.parentNoteId}' is deleted or does not exist.` };
}
const parentNote = becca.getNote(afterNote.parentNoteId);
if (parentNote.type === 'search') {
return {
success: false,
message: "Can't clone into a search note"
};
}
2020-06-20 12:31:38 +02:00
const validationResult = treeService.validateParentChild(afterNote.parentNoteId, noteId);
2019-10-26 21:14:06 +02:00
if (!validationResult.success) {
return validationResult;
}
2023-06-30 11:18:34 +02:00
// we don't change utcDateModified, so other changes are prioritized in case of conflict
// also we would have to sync all those modified branches otherwise hash checks would fail
2020-06-20 12:31:38 +02:00
sql.execute("UPDATE branches SET notePosition = notePosition + 10 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",
[afterNote.parentNoteId, afterNote.notePosition]);
2023-07-29 23:25:02 +02:00
eventChangesService.putNoteReorderingEntityChange(afterNote.parentNoteId);
const branch = new BBranch({
noteId: noteId,
parentNoteId: afterNote.parentNoteId,
2019-10-19 12:36:16 +02:00
notePosition: afterNote.notePosition + 10,
isExpanded: 0
}).save();
2022-12-27 10:22:50 +01:00
log.info(`Cloned note '${noteId}' into parent note '${afterNote.parentNoteId}' after note '${afterNote.noteId}', branch '${afterBranchId}'`);
2022-02-05 12:06:23 +01:00
return { success: true, branchId: branch.branchId };
}
module.exports = {
cloneNoteToBranch,
2023-04-15 00:06:13 +02:00
cloneNoteToParentNote,
2018-08-11 19:45:55 +02:00
ensureNoteIsPresentInParent,
ensureNoteIsAbsentFromParent,
toggleNoteInParent,
cloneNoteAfter
};