Notes/src/routes/api/branches.js

235 lines
7.8 KiB
JavaScript
Raw Normal View History

2018-01-13 18:02:41 -05:00
"use strict";
const sql = require('../../services/sql.js');
const utils = require('../../services/utils.js');
const entityChangesService = require('../../services/entity_changes.js');
const treeService = require('../../services/tree.js');
const eraseService = require('../../services/erase.js');
const becca = require('../../becca/becca.js');
const TaskContext = require('../../services/task_context.js');
const branchService = require('../../services/branches.js');
const log = require('../../services/log.js');
const ValidationError = require('../../errors/validation_error.js');
2023-11-02 23:26:32 +01:00
const eventService = require("../../services/events.js");
2018-01-13 18:02:41 -05:00
/**
2023-05-05 23:41:11 +02:00
* Code in this file deals with moving and cloning branches. The relationship between note and parent note is unique
* for not deleted branches. There may be multiple deleted note-parent note relationships.
2018-01-13 18:02:41 -05:00
*/
2020-06-20 12:31:38 +02:00
function moveBranchToParent(req) {
2020-05-30 10:30:21 +02:00
const {branchId, parentBranchId} = req.params;
2018-01-13 18:02:41 -05:00
2021-05-02 11:23:58 +02:00
const branchToMove = becca.getBranch(branchId);
const targetParentBranch = becca.getBranch(parentBranchId);
2018-01-13 18:02:41 -05:00
if (!branchToMove || !targetParentBranch) {
2023-05-04 22:16:18 +02:00
throw new ValidationError(`One or both branches '${branchId}', '${parentBranchId}' have not been found`);
2020-05-30 10:30:21 +02:00
}
return branchService.moveBranchToBranch(branchToMove, targetParentBranch, branchId);
2018-03-30 12:57:22 -04:00
}
2018-01-13 18:02:41 -05:00
2020-06-20 12:31:38 +02:00
function moveBranchBeforeNote(req) {
2019-10-19 12:36:16 +02:00
const {branchId, beforeBranchId} = req.params;
2018-01-13 18:02:41 -05:00
const branchToMove = becca.getBranchOrThrow(branchId);
const beforeBranch = becca.getBranchOrThrow(beforeBranchId);
const validationResult = treeService.validateParentChild(beforeBranch.parentNoteId, branchToMove.noteId, branchId);
2018-03-30 12:57:22 -04:00
if (!validationResult.success) {
return [200, validationResult];
2018-01-13 18:02:41 -05:00
}
const originalBeforeNotePosition = beforeBranch.notePosition;
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",
[beforeBranch.parentNoteId, originalBeforeNotePosition]);
// also need to update becca positions
const parentNote = becca.getNote(beforeBranch.parentNoteId);
for (const childBranch of parentNote.getChildBranches()) {
if (childBranch.notePosition >= originalBeforeNotePosition) {
childBranch.notePosition += 10;
}
}
2018-01-13 18:02:41 -05:00
if (branchToMove.parentNoteId === beforeBranch.parentNoteId) {
branchToMove.notePosition = originalBeforeNotePosition;
2020-06-20 12:31:38 +02:00
branchToMove.save();
}
else {
const newBranch = branchToMove.createClone(beforeBranch.parentNoteId, originalBeforeNotePosition);
2020-06-20 12:31:38 +02:00
newBranch.save();
2018-01-13 18:02:41 -05:00
branchToMove.markAsDeleted();
}
2018-01-13 18:02:41 -05:00
treeService.sortNotesIfNeeded(parentNote.noteId);
2023-05-05 23:41:11 +02:00
// if sorting is not needed, then still the ordering might have changed above manually
2023-07-29 23:25:02 +02:00
entityChangesService.putNoteReorderingEntityChange(parentNote.noteId);
2022-02-05 12:06:23 +01:00
log.info(`Moved note ${branchToMove.noteId}, branch ${branchId} before note ${beforeBranch.noteId}, branch ${beforeBranchId}`);
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 moveBranchAfterNote(req) {
2019-10-19 12:36:16 +02:00
const {branchId, afterBranchId} = req.params;
2018-01-13 18:02:41 -05:00
2021-05-02 11:23:58 +02:00
const branchToMove = becca.getBranch(branchId);
const afterNote = becca.getBranch(afterBranchId);
2018-01-13 18:02:41 -05:00
2020-06-20 12:31:38 +02:00
const validationResult = treeService.validateParentChild(afterNote.parentNoteId, branchToMove.noteId, branchId);
2018-03-30 12:57:22 -04:00
if (!validationResult.success) {
return [200, validationResult];
2018-01-13 18:02:41 -05:00
}
const originalAfterNotePosition = afterNote.notePosition;
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, originalAfterNotePosition]);
// also need to update becca positions
const parentNote = becca.getNote(afterNote.parentNoteId);
for (const childBranch of parentNote.getChildBranches()) {
if (childBranch.notePosition > originalAfterNotePosition) {
childBranch.notePosition += 10;
}
}
2018-01-13 18:02:41 -05:00
const movedNotePosition = originalAfterNotePosition + 10;
2018-01-13 18:02:41 -05:00
if (branchToMove.parentNoteId === afterNote.parentNoteId) {
branchToMove.notePosition = movedNotePosition;
2020-06-20 12:31:38 +02:00
branchToMove.save();
}
else {
const newBranch = branchToMove.createClone(afterNote.parentNoteId, movedNotePosition);
2020-06-20 12:31:38 +02:00
newBranch.save();
branchToMove.markAsDeleted();
}
2018-01-13 18:02:41 -05:00
treeService.sortNotesIfNeeded(parentNote.noteId);
2023-05-05 23:41:11 +02:00
// if sorting is not needed, then still the ordering might have changed above manually
2023-07-29 23:25:02 +02:00
entityChangesService.putNoteReorderingEntityChange(parentNote.noteId);
2022-02-05 12:06:23 +01:00
log.info(`Moved note ${branchToMove.noteId}, branch ${branchId} after note ${afterNote.noteId}, branch ${afterBranchId}`);
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 setExpanded(req) {
2021-02-04 23:08:16 +01:00
const {branchId} = req.params;
const expanded = parseInt(req.params.expanded);
2018-01-13 18:02:41 -05:00
2022-12-28 13:09:49 +01:00
if (branchId !== 'none_root') {
2020-06-20 12:31:38 +02:00
sql.execute("UPDATE branches SET isExpanded = ? WHERE branchId = ?", [expanded, branchId]);
2020-05-03 13:52:12 +02:00
// we don't sync expanded label
2020-06-03 11:06:45 +02:00
// also this does not trigger updates to the frontend, this would trigger too many reloads
2021-04-16 23:00:08 +02:00
const branch = becca.branches[branchId];
if (branch) {
branch.isExpanded = !!expanded;
}
2023-11-02 23:26:32 +01:00
eventService.emit(eventService.ENTITY_CHANGED, {
entityName: 'branches',
entity: branch
});
2020-05-03 13:52:12 +02:00
}
2018-03-30 12:57:22 -04:00
}
2020-06-20 12:31:38 +02:00
function setExpandedForSubtree(req) {
2021-02-04 23:08:16 +01:00
const {branchId} = req.params;
const expanded = parseInt(req.params.expanded);
2020-06-20 12:31:38 +02:00
let branchIds = sql.getColumn(`
WITH RECURSIVE
tree(branchId, noteId) AS (
SELECT branchId, noteId FROM branches WHERE branchId = ?
UNION
SELECT branches.branchId, branches.noteId FROM branches
JOIN tree ON branches.parentNoteId = tree.noteId
WHERE branches.isDeleted = 0
)
SELECT branchId FROM tree`, [branchId]);
2020-05-03 13:52:12 +02:00
// root is always expanded
2022-12-28 13:09:49 +01:00
branchIds = branchIds.filter(branchId => branchId !== 'none_root');
2020-05-03 13:52:12 +02:00
2020-06-20 12:31:38 +02:00
sql.executeMany(`UPDATE branches SET isExpanded = ${expanded} WHERE branchId IN (???)`, branchIds);
for (const branchId of branchIds) {
2021-04-16 23:00:08 +02:00
const branch = becca.branches[branchId];
if (branch) {
branch.isExpanded = !!expanded;
}
}
return {
branchIds
};
}
2020-06-20 12:31:38 +02:00
function deleteBranch(req) {
2019-10-19 00:11:07 +02:00
const last = req.query.last === 'true';
2021-09-16 14:38:09 +02:00
const eraseNotes = req.query.eraseNotes === 'true';
const branch = becca.getBranchOrThrow(req.params.branchId);
2023-05-09 00:05:27 +02:00
const taskContext = TaskContext.getInstance(req.query.taskId, 'deleteNotes');
2018-03-31 23:08:22 -04:00
2020-01-03 10:48:36 +01:00
const deleteId = utils.randomString(10);
2022-09-21 23:58:54 +02:00
let noteDeleted;
2019-10-19 00:11:07 +02:00
2021-09-16 14:38:09 +02:00
if (eraseNotes) {
2022-09-21 23:58:54 +02:00
// erase automatically means deleting all clones + note itself
branch.getNote().deleteNote(deleteId, taskContext);
eraseService.eraseNotesWithDeleteId(deleteId);
2022-09-21 23:58:54 +02:00
noteDeleted = true;
} else {
noteDeleted = branch.deleteBranch(deleteId, taskContext);
2021-09-16 14:38:09 +02:00
}
2019-10-19 00:11:07 +02:00
if (last) {
taskContext.taskSucceeded();
}
return {
2019-10-19 00:11:07 +02:00
noteDeleted: noteDeleted
};
2018-03-30 12:57:22 -04:00
}
2020-06-20 12:31:38 +02:00
function setPrefix(req) {
2018-04-01 20:33:10 -04:00
const branchId = req.params.branchId;
const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix;
2021-05-02 11:23:58 +02:00
const branch = becca.getBranch(branchId);
2018-04-01 20:33:10 -04:00
branch.prefix = prefix;
2020-06-20 12:31:38 +02:00
branch.save();
2018-04-01 20:33:10 -04:00
}
2018-03-30 12:57:22 -04:00
module.exports = {
moveBranchToParent,
moveBranchBeforeNote,
moveBranchAfterNote,
setExpanded,
setExpandedForSubtree,
2018-04-01 20:33:10 -04:00
deleteBranch,
setPrefix
2020-05-30 10:30:21 +02:00
};