2018-01-13 18:02:41 -05:00
"use strict" ;
2024-07-18 21:35:17 +03:00
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" ;
2025-01-09 18:07:02 +02:00
import { 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
2018-04-03 22:15:28 -04:00
* 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 ) ;
2023-11-16 23:31:33 +01:00
const targetParentBranch = becca . getBranch ( parentBranchId ) ;
2018-01-13 18:02:41 -05:00
2023-11-16 23:31:33 +01: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
}
2023-11-16 23:31:33 +01: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
2023-05-08 00:02:08 +02:00
const branchToMove = becca . getBranchOrThrow ( branchId ) ;
const beforeBranch = becca . getBranchOrThrow ( beforeBranchId ) ;
2021-02-18 22:30:55 +01:00
const validationResult = treeService . validateParentChild ( beforeBranch . parentNoteId , branchToMove . noteId , branchId ) ;
2018-03-30 12:57:22 -04:00
if ( ! validationResult . success ) {
2018-10-21 22:42:20 +02:00
return [ 200 , validationResult ] ;
2018-01-13 18:02:41 -05:00
}
2021-07-25 20:55:41 +02: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
2018-04-03 22:15:28 -04:00
// also we would have to sync all those modified branches otherwise hash checks would fail
2021-07-25 20:55:41 +02:00
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 ] ) ;
2021-07-25 20:55:41 +02:00
// also need to update becca positions
2024-04-05 20:33:04 +03:00
const parentNote = becca . getNoteOrThrow ( beforeBranch . parentNoteId ) ;
2021-07-25 20:55:41 +02:00
for ( const childBranch of parentNote . getChildBranches ( ) ) {
if ( childBranch . notePosition >= originalBeforeNotePosition ) {
childBranch . notePosition += 10 ;
}
}
2018-01-13 18:02:41 -05:00
2021-02-18 22:30:55 +01:00
if ( branchToMove . parentNoteId === beforeBranch . parentNoteId ) {
2021-07-25 20:55:41 +02:00
branchToMove . notePosition = originalBeforeNotePosition ;
2020-06-20 12:31:38 +02:00
branchToMove . save ( ) ;
2025-01-09 18:07:02 +02:00
} else {
2021-07-25 20:55:41 +02:00
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
2021-05-02 20:32:50 +02:00
branchToMove . markAsDeleted ( ) ;
2020-01-28 22:15:33 +01:00
}
2018-01-13 18:02:41 -05:00
2021-09-03 22:33:40 +02: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 ) ;
2021-09-02 23:21:02 +02:00
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 ) {
2018-10-21 22:42:20 +02:00
return [ 200 , validationResult ] ;
2018-01-13 18:02:41 -05:00
}
2021-07-25 20:55:41 +02: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
2018-04-03 22:15:28 -04:00
// 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 ] ) ;
2021-07-25 20:55:41 +02:00
// also need to update becca positions
2024-04-05 20:33:04 +03:00
const parentNote = becca . getNoteOrThrow ( afterNote . parentNoteId ) ;
2021-07-25 20:55:41 +02:00
for ( const childBranch of parentNote . getChildBranches ( ) ) {
if ( childBranch . notePosition > originalAfterNotePosition ) {
childBranch . notePosition += 10 ;
}
}
2018-01-13 18:02:41 -05:00
2021-07-25 20:55:41 +02:00
const movedNotePosition = originalAfterNotePosition + 10 ;
2018-01-13 18:02:41 -05:00
2020-02-01 22:29:32 +01: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 {
2020-02-01 22:29:32 +01:00
const newBranch = branchToMove . createClone ( afterNote . parentNoteId , movedNotePosition ) ;
2020-06-20 12:31:38 +02:00
newBranch . save ( ) ;
2020-01-28 22:15:33 +01:00
2021-05-02 20:32:50 +02:00
branchToMove . markAsDeleted ( ) ;
2020-01-28 22:15:33 +01:00
}
2018-01-13 18:02:41 -05:00
2021-09-03 22:33:40 +02: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 ) ;
2021-09-02 23:21:02 +02:00
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-02-04 22:05:32 +01:00
2021-04-16 23:00:08 +02:00
const branch = becca . branches [ branchId ] ;
2021-02-04 22:05:32 +01:00
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 ) ;
2020-05-03 13:15:08 +02:00
2025-01-09 18:07:02 +02:00
let branchIds = sql . getColumn < string > (
`
2020-05-03 13:15:08 +02:00
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:15:08 +02:00
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 ) ;
2020-05-03 13:15:08 +02:00
2021-02-04 22:05:32 +01:00
for ( const branchId of branchIds ) {
2021-04-16 23:00:08 +02:00
const branch = becca . branches [ branchId ] ;
2021-02-04 22:05:32 +01:00
if ( branch ) {
branch . isExpanded = ! ! expanded ;
}
}
2020-05-03 13:15:08 +02:00
return {
branchIds
} ;
}
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" ;
2023-05-08 00:02:08 +02:00
const branch = becca . getBranchOrThrow ( req . params . branchId ) ;
2022-10-27 20:34:53 +02:00
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 ) ;
2023-07-27 23:57:12 +02:00
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 ( ) ;
}
2019-05-20 21:50:01 +02:00
return {
2019-10-19 00:11:07 +02:00
noteDeleted : noteDeleted
2019-05-20 21:50:01 +02:00
} ;
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 ;
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
}
2024-07-18 21:47:30 +03:00
export default {
2018-03-30 12:57:22 -04:00
moveBranchToParent ,
moveBranchBeforeNote ,
moveBranchAfterNote ,
setExpanded ,
2020-05-03 13:15:08 +02:00
setExpandedForSubtree ,
2018-04-01 20:33:10 -04:00
deleteBranch ,
setPrefix
2020-05-30 10:30:21 +02:00
} ;