Notes/src/routes/api/branches.js

206 lines
6.6 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');
const entityChangesService = require('../../services/entity_changes.js');
2020-02-17 22:38:46 +01:00
const treeService = require('../../services/tree');
const noteService = require('../../services/notes');
const noteCache = require('../../services/note_cache/note_cache');
2018-03-31 23:08:22 -04:00
const repository = require('../../services/repository');
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
2020-06-20 12:31:38 +02:00
const parentBranch = repository.getBranch(parentBranchId);
const branchToMove = repository.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.isDeleted = true;
2020-06-20 12:31:38 +02:00
branchToMove.save();
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
2020-06-20 12:31:38 +02:00
const branchToMove = repository.getBranch(branchId);
const beforeNote = repository.getBranch(beforeBranchId);
2018-01-13 18:02:41 -05:00
2020-06-20 12:31:38 +02:00
const validationResult = treeService.validateParentChild(beforeNote.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
[beforeNote.parentNoteId, beforeNote.notePosition]);
2018-01-13 18:02:41 -05:00
entityChangesService.addNoteReorderingEntityChange(beforeNote.parentNoteId);
if (branchToMove.parentNoteId === beforeNote.parentNoteId) {
branchToMove.notePosition = beforeNote.notePosition;
2020-06-20 12:31:38 +02:00
branchToMove.save();
}
else {
const newBranch = branchToMove.createClone(beforeNote.parentNoteId, beforeNote.notePosition);
2020-06-20 12:31:38 +02:00
newBranch.save();
2018-01-13 18:02:41 -05:00
branchToMove.isDeleted = true;
2020-06-20 12:31:38 +02:00
branchToMove.save();
}
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
2020-06-20 12:31:38 +02:00
const branchToMove = repository.getBranch(branchId);
const afterNote = repository.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.isDeleted = true;
2020-06-20 12:31:38 +02:00
branchToMove.save();
}
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) {
2019-10-19 12:36:16 +02:00
const {branchId, expanded} = req.params;
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
const branch = noteCache.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) {
const {branchId, expanded} = req.params;
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) {
const branch = noteCache.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';
2020-06-20 12:31:38 +02:00
const branch = repository.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;
2020-06-20 12:31:38 +02:00
const branch = repository.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
};