Notes/src/routes/api/branches.ts

281 lines
9.3 KiB
TypeScript
Raw Normal View History

2018-01-13 18:02:41 -05:00
"use strict";
import sql from "../../services/sql.js";
import utils from "../../services/utils.js";
import entityChangesService from "../../services/entity_changes.js";
import treeService from "../../services/tree.js";
import eraseService from "../../services/erase.js";
import becca from "../../becca/becca.js";
import TaskContext from "../../services/task_context.js";
import branchService from "../../services/branches.js";
import log from "../../services/log.js";
import ValidationError from "../../errors/validation_error.js";
import eventService from "../../services/events.js";
import type { Request } from "express";
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
*/
2024-04-05 20:33:04 +03:00
function moveBranchToParent(req: Request) {
2025-01-09 18:07:02 +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
2024-04-05 20:33:04 +03:00
function moveBranchBeforeNote(req: Request) {
2025-01-09 18:07:02 +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
2025-01-09 18:07:02 +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
2024-04-05 20:33:04 +03:00
const parentNote = becca.getNoteOrThrow(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();
2025-01-09 18:07:02 +02:00
} 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
2024-04-05 20:33:04 +03:00
function moveBranchAfterNote(req: Request) {
2025-01-09 18:07:02 +02:00
const { branchId, afterBranchId } = req.params;
2018-01-13 18:02:41 -05:00
2024-04-05 20:33:04 +03:00
const branchToMove = becca.getBranchOrThrow(branchId);
const afterNote = becca.getBranchOrThrow(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
2025-01-09 18:07:02 +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
2024-04-05 20:33:04 +03:00
const parentNote = becca.getNoteOrThrow(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();
2025-01-09 18:07:02 +02:00
} 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
2024-04-05 20:33:04 +03:00
function setExpanded(req: Request) {
2025-01-09 18:07:02 +02:00
const { branchId } = req.params;
2021-02-04 23:08:16 +01:00
const expanded = parseInt(req.params.expanded);
2018-01-13 18:02:41 -05:00
2025-01-09 18:07:02 +02: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, {
2025-01-09 18:07:02 +02:00
entityName: "branches",
2023-11-02 23:26:32 +01:00
entity: branch
});
2020-05-03 13:52:12 +02:00
}
2018-03-30 12:57:22 -04:00
}
2024-04-05 20:33:04 +03:00
function setExpandedForSubtree(req: Request) {
2025-01-09 18:07:02 +02:00
const { branchId } = req.params;
2021-02-04 23:08:16 +01:00
const expanded = parseInt(req.params.expanded);
2025-01-09 18:07:02 +02:00
let branchIds = sql.getColumn<string>(
`
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
)
2025-01-09 18:07:02 +02:00
SELECT branchId FROM tree`,
[branchId]
);
2020-05-03 13:52:12 +02:00
// root is always expanded
2025-01-09 18:07:02 +02: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
};
}
/**
* @swagger
* /api/branches/{branchId}:
* delete:
* summary: Delete branch (note clone)
* operationId: branches-delete
* parameters:
* - name: branchId
* in: path
* required: true
* schema:
* $ref: "#/components/schemas/BranchId"
* - name: taskId
* in: query
* required: true
* schema:
* type: string
* description: Task group identifier
* - name: eraseNotes
* in: query
* schema:
* type: boolean
* required: false
* description: Whether to erase the note immediately
* - name: last
* in: query
* schema:
* type: boolean
* required: true
* description: Whether this is the last request of this task group
* responses:
* '200':
* description: Branch successfully deleted
* content:
* application/json:
* schema:
* type: object
* properties:
* noteDeleted:
* type: boolean
* description: Whether the last note clone was deleted
* security:
* - session: []
* tags: ["data"]
*/
2024-04-05 20:33:04 +03:00
function deleteBranch(req: Request) {
2025-01-09 18:07:02 +02:00
const last = req.query.last === "true";
const eraseNotes = req.query.eraseNotes === "true";
const branch = becca.getBranchOrThrow(req.params.branchId);
2025-01-09 18:07:02 +02:00
const taskContext = TaskContext.getInstance(req.query.taskId as string, "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
}
2024-04-05 20:33:04 +03:00
function setPrefix(req: Request) {
2018-04-01 20:33:10 -04:00
const branchId = req.params.branchId;
//TriliumNextTODO: req.body arrives as string, so req.body.prefix will be undefined did the code below ever even work?
2018-04-01 20:33:10 -04:00
const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix;
2024-04-05 20:33:04 +03:00
const branch = becca.getBranchOrThrow(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
}
export default {
2018-03-30 12:57:22 -04:00
moveBranchToParent,
moveBranchBeforeNote,
moveBranchAfterNote,
setExpanded,
setExpandedForSubtree,
2018-04-01 20:33:10 -04:00
deleteBranch,
setPrefix
2020-05-30 10:30:21 +02:00
};