Skip to content

Build your personal knowledge base with Trilium Notes

11.9 KB
Raw Blame
import branchService from "../../services/branches.js";
import eraseService from "../../services/erase.js";
import eventService from "../../services/events.js";
import type { Request } from "../../http_interface";

import becca from "../../becca/becca.js";
import type BBranch from "../../becca/entities/bbranch.js";
import entityChangesService from "../../services/entity_changes.js";
import { getLog } from "../../services/log.js";
import TaskContext from "../../services/task_context.js";
import treeService from "../../services/tree.js";
import { isEmptyOrWhitespace, randomString } from "../../services/utils/index.js";
import { getSql } from "../../services/sql/index.js";
import { ValidationError } from "../../errors.js";

/**
 * 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.
 */

/**
 * Moves the branches in `branchIds`, in order, under the note of `parentBranchId`. All moves run in one
 * transaction, so the client receives them as a single frontend update. The first move that fails
 * validation stops the batch; the moves before it stay.
 */
function moveBranchesToParent(req: Request<{ parentBranchId: string }>) {
    const { parentBranchId } = req.params;
    const { branchIds } = req.body;

    if (!Array.isArray(branchIds)) {
        throw new ValidationError("branchIds must be an array");
    }

    const targetParentBranch = becca.getBranch(parentBranchId);
    if (!targetParentBranch) {
        throw new ValidationError(`Target branch '${parentBranchId}' has not been found`);
    }

    const branchesToMove = branchIds.map((branchId) => {
        const branch = typeof branchId === "string" ? becca.getBranch(branchId) : null;
        if (!branch) {
            throw new ValidationError(`Branch '${branchId}' has not been found`);
        }
        return { branchId: branchId as string, branch };
    });

    for (const { branchId, branch } of branchesToMove) {
        const res = branchService.moveBranchToBranch(branch, targetParentBranch, branchId);

        if (!("success" in res) || !res.success) {
            return res;
        }
    }

    return { success: true };
}

function moveBranchBeforeNote(req: Request<{ branchId: string, beforeBranchId: string }>) {
    const { branchId, beforeBranchId } = req.params;

    const branchToMove = becca.getBranchOrThrow(branchId);
    const beforeBranch = becca.getBranchOrThrow(beforeBranchId);

    const validationResult = treeService.validateParentChild(beforeBranch.parentNoteId, branchToMove.noteId, branchId);

    if (!validationResult.success) {
        return [200, validationResult];
    }

    const originalBeforeNotePosition = beforeBranch.notePosition;

    // 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

    getSql().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.getNoteOrThrow(beforeBranch.parentNoteId);

    for (const childBranch of parentNote.getChildBranches()) {
        if (childBranch.notePosition >= originalBeforeNotePosition) {
            childBranch.notePosition += 10;
        }
    }

    if (branchToMove.parentNoteId === beforeBranch.parentNoteId) {
        branchToMove.notePosition = originalBeforeNotePosition;
        branchToMove.save();
    } else {
        const newBranch = branchToMove.createClone(beforeBranch.parentNoteId, originalBeforeNotePosition);
        newBranch.save();

        branchToMove.markAsDeleted();
    }

    treeService.sortNotesIfNeeded(parentNote.noteId);

    // if sorting is not needed, then still the ordering might have changed above manually
    entityChangesService.putNoteReorderingEntityChange(parentNote.noteId);

    getLog().info(`Moved note ${branchToMove.noteId}, branch ${branchId} before note ${beforeBranch.noteId}, branch ${beforeBranchId}`);

    return { success: true };
}

function moveBranchAfterNote(req: Request<{ branchId: string, afterBranchId: string }>) {
    const { branchId, afterBranchId } = req.params;

    const branchToMove = becca.getBranchOrThrow(branchId);
    const afterNote = becca.getBranchOrThrow(afterBranchId);

    const validationResult = treeService.validateParentChild(afterNote.parentNoteId, branchToMove.noteId, branchId);

    if (!validationResult.success) {
        return [200, validationResult];
    }

    const originalAfterNotePosition = afterNote.notePosition;

    // 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
    getSql().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.getNoteOrThrow(afterNote.parentNoteId);

    for (const childBranch of parentNote.getChildBranches()) {
        if (childBranch.notePosition > originalAfterNotePosition) {
            childBranch.notePosition += 10;
        }
    }

    const movedNotePosition = originalAfterNotePosition + 10;

    if (branchToMove.parentNoteId === afterNote.parentNoteId) {
        branchToMove.notePosition = movedNotePosition;
        branchToMove.save();
    } else {
        const newBranch = branchToMove.createClone(afterNote.parentNoteId, movedNotePosition);
        newBranch.save();

        branchToMove.markAsDeleted();
    }

    treeService.sortNotesIfNeeded(parentNote.noteId);

    // if sorting is not needed, then still the ordering might have changed above manually
    entityChangesService.putNoteReorderingEntityChange(parentNote.noteId);

    getLog().info(`Moved note ${branchToMove.noteId}, branch ${branchId} after note ${afterNote.noteId}, branch ${afterBranchId}`);

    return { success: true };
}

function setExpanded(req: Request<{ branchId: string, expanded: string }>) {
    const { branchId } = req.params;
    const expanded = parseInt(req.params.expanded);

    if (branchId !== "none_root") {
        getSql().execute("UPDATE branches SET isExpanded = ? WHERE branchId = ?", [expanded, branchId]);
        // we don't sync expanded label
        // also this does not trigger updates to the frontend, this would trigger too many reloads

        const branch = becca.branches[branchId];

        if (branch) {
            branch.isExpanded = !!expanded;
        }

        eventService.emit(eventService.ENTITY_CHANGED, {
            entityName: "branches",
            entity: branch
        });
    }
}

function setExpandedForSubtree(req: Request<{ branchId: string, expanded: string }>) {
    const { branchId } = req.params;
    const isExpanded = req.params.expanded === "1";

    // Walks becca rather than the `branches` table, so that the virtual branches a provider
    // injects are reached too; `none_root` is skipped because the root is always expanded.
    const changedBranches: BBranch[] = [];
    const visitedNoteIds = new Set<string>();
    const startBranch = becca.branches[branchId];
    const pending = startBranch ? [ startBranch ] : [];

    for (let branch = pending.pop(); branch; branch = pending.pop()) {
        if (branch.branchId !== "none_root" && branch.isExpanded !== isExpanded) {
            changedBranches.push(branch);
        }

        if (!visitedNoteIds.has(branch.noteId)) {
            visitedNoteIds.add(branch.noteId);
            for (const childBranch of branch.childNote.getChildBranches()) {
                if (childBranch) {
                    pending.push(childBranch);
                }
            }
        }
    }

    getSql().executeMany(
        /*sql*/`UPDATE branches SET isExpanded = ${isExpanded ? 1 : 0} WHERE branchId IN (???)`,
        changedBranches.filter((branch) => !branch.isVirtual).map((branch) => branch.branchId)
    );

    for (const branch of changedBranches) {
        branch.isExpanded = isExpanded;
    }

    return {
        branchIds: changedBranches.map((branch) => branch.branchId)
    };
}

/**
 * @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"]
 */
function deleteBranch(req: Request<{ branchId: string }>) {
    const last = req.query.last === "true";
    const eraseNotes = req.query.eraseNotes === "true";
    const branch = becca.getBranchOrThrow(req.params.branchId);

    const taskContext = TaskContext.getInstance(req.query.taskId as string, "deleteNotes", null);

    const deleteId = randomString(10);
    let noteDeleted;

    if (eraseNotes) {
        // erase automatically means deleting all clones + note itself
        branch.getNote().deleteNote(deleteId, taskContext);
        taskContext.scheduleErase(deleteId);
        noteDeleted = true;
    } else {
        noteDeleted = branch.deleteBranch(deleteId, taskContext);
    }

    if (last) {
        eraseService.eraseNotesWithDeleteIds(taskContext.takeScheduledErases());
        taskContext.taskSucceeded(null);
    }

    return {
        noteDeleted
    };
}

function setPrefix(req: Request<{ branchId: string }>) {
    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?
    const prefix = isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix;

    const branch = becca.getBranchOrThrow(branchId);
    branch.prefix = prefix;
    branch.save();
}

function setPrefixBatch(req: Request) {
    const { branchIds, prefix } = req.body;

    if (!Array.isArray(branchIds)) {
        throw new ValidationError("branchIds must be an array");
    }

    // Validate that prefix is a string or null/undefined to prevent prototype pollution
    if (prefix !== null && prefix !== undefined && typeof prefix !== 'string') {
        throw new ValidationError("prefix must be a string or null");
    }

    const normalizedPrefix = isEmptyOrWhitespace(prefix) ? null : prefix;
    let updatedCount = 0;

    for (const branchId of branchIds) {
        const branch = becca.getBranch(branchId);
        if (branch) {
            branch.prefix = normalizedPrefix;
            branch.save();
            updatedCount++;
        } else {
            getLog().info(`Branch ${branchId} not found, skipping prefix update`);
        }
    }

    return {
        success: true,
        count: updatedCount
    };
}

export default {
    moveBranchesToParent,
    moveBranchBeforeNote,
    moveBranchAfterNote,
    setExpanded,
    setExpandedForSubtree,
    deleteBranch,
    setPrefix,
    setPrefixBatch
};