Notes/src/routes/api/branches.js

212 lines
6.7 KiB
JavaScript
Raw Normal View History

2018-01-13 18:02:41 -05:00
"use strict";
const sql = require('../../services/sql');
const utils = require('../../services/utils');
2021-06-29 22:15:57 +02:00
const entityChangesService = require('../../services/entity_changes');
2020-02-17 22:38:46 +01:00
const treeService = require('../../services/tree');
const noteService = require('../../services/notes');
2021-06-29 22:15:57 +02:00
const becca = require('../../becca/becca');
2019-10-18 23:19:16 +02:00
const TaskContext = require('../../services/task_context');
2018-01-13 18:02:41 -05:00
/**
* Code in this file deals with moving and cloning branches. 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 parentBranch = becca.getBranch(parentBranchId);
const branchToMove = becca.getBranch(branchId);
2018-01-13 18:02:41 -05:00
2020-05-30 10:30:21 +02:00
if (!parentBranch || !branchToMove) {
return [400, `One or both branches ${branchId}, ${parentBranchId} have not been found`];
}
if (branchToMove.parentNoteId === parentBranch.noteId) {
return { success: true }; // no-op
}
2020-06-20 12:31:38 +02:00
const validationResult = treeService.validateParentChild(parentBranch.noteId, 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
}
2020-06-20 12:31:38 +02:00
const maxNotePos = sql.getValue('SELECT MAX(notePosition) FROM branches WHERE parentNoteId = ? AND isDeleted = 0', [parentBranch.noteId]);
2019-10-19 12:36:16 +02:00
const newNotePos = maxNotePos === null ? 0 : maxNotePos + 10;
2018-01-13 18:02:41 -05:00
2020-05-30 10:30:21 +02:00
// expanding so that the new placement of the branch is immediately visible
parentBranch.isExpanded = true;
2020-06-20 12:31:38 +02:00
parentBranch.save();
2020-05-30 10:30:21 +02:00
const newBranch = branchToMove.createClone(parentBranch.noteId, newNotePos);
2020-06-20 12:31:38 +02:00
newBranch.save();
branchToMove.markAsDeleted();
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 moveBranchBeforeNote(req) {
2019-10-19 12:36:16 +02:00
const {branchId, beforeBranchId} = req.params;
2018-01-13 18:02:41 -05:00
2021-05-02 11:23:58 +02:00
const branchToMove = becca.getBranch(branchId);
const beforeBranch = becca.getBranch(beforeBranchId);
2018-01-13 18:02:41 -05:00
if (!branchToMove) {
return [404, `Can't find branch ${branchId}`];
}
if (!beforeBranch) {
return [404, `Can't find branch ${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
}
2019-03-12 20:58:31 +01: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, beforeBranch.notePosition]);
2018-01-13 18:02:41 -05:00
entityChangesService.addNoteReorderingEntityChange(beforeBranch.parentNoteId);
if (branchToMove.parentNoteId === beforeBranch.parentNoteId) {
branchToMove.notePosition = beforeBranch.notePosition;
2020-06-20 12:31:38 +02:00
branchToMove.save();
}
else {
const newBranch = branchToMove.createClone(beforeBranch.parentNoteId, beforeBranch.notePosition);
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
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
}
2019-03-12 20:58:31 +01: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",
2018-03-30 12:57:22 -04:00
[afterNote.parentNoteId, afterNote.notePosition]);
2018-01-13 18:02:41 -05:00
entityChangesService.addNoteReorderingEntityChange(afterNote.parentNoteId);
const movedNotePosition = afterNote.notePosition + 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
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
2020-05-03 13:52:12 +02:00
if (branchId !== '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;
}
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
branchIds = branchIds.filter(branchId => branchId !== 'root');
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-05-02 11:23:58 +02:00
const branch = becca.getBranch(req.params.branchId);
2019-10-18 23:19:16 +02:00
const taskContext = TaskContext.getInstance(req.query.taskId, 'delete-notes');
2018-03-31 23:08:22 -04:00
2020-01-03 10:48:36 +01:00
const deleteId = utils.randomString(10);
2020-06-20 12:31:38 +02:00
const noteDeleted = noteService.deleteBranch(branch, deleteId, taskContext);
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
};