server-ts: Port services/tree

This commit is contained in:
Elian Doran 2024-02-18 11:47:32 +02:00
parent d8d729342d
commit 0d4fb42731
No known key found for this signature in database
10 changed files with 43 additions and 35 deletions

View File

@ -267,17 +267,19 @@ class BBranch extends AbstractBeccaEntity<BBranch> {
}; };
} }
createClone(parentNoteId: string, notePosition: number) { createClone(parentNoteId: string, notePosition?: number) {
const existingBranch = this.becca.getBranchFromChildAndParent(this.noteId, parentNoteId); const existingBranch = this.becca.getBranchFromChildAndParent(this.noteId, parentNoteId);
if (existingBranch) { if (existingBranch) {
if (notePosition) {
existingBranch.notePosition = notePosition; existingBranch.notePosition = notePosition;
}
return existingBranch; return existingBranch;
} else { } else {
return new BBranch({ return new BBranch({
noteId: this.noteId, noteId: this.noteId,
parentNoteId: parentNoteId, parentNoteId: parentNoteId,
notePosition: notePosition, notePosition: notePosition || null,
prefix: this.prefix, prefix: this.prefix,
isExpanded: this.isExpanded isExpanded: this.isExpanded
}); });

View File

@ -80,7 +80,7 @@ export interface BranchRow {
noteId: string; noteId: string;
parentNoteId: string; parentNoteId: string;
prefix?: string | null; prefix?: string | null;
notePosition: number | null; notePosition?: number | null;
isExpanded?: boolean; isExpanded?: boolean;
isDeleted?: boolean; isDeleted?: boolean;
utcDateModified?: string; utcDateModified?: string;

View File

@ -3,7 +3,7 @@
const sql = require('../../services/sql'); const sql = require('../../services/sql');
const utils = require('../../services/utils'); const utils = require('../../services/utils');
const entityChangesService = require('../../services/entity_changes'); const entityChangesService = require('../../services/entity_changes');
const treeService = require('../../services/tree.js'); const treeService = require('../../services/tree');
const eraseService = require('../../services/erase'); const eraseService = require('../../services/erase');
const becca = require('../../becca/becca'); const becca = require('../../becca/becca');
const TaskContext = require('../../services/task_context'); const TaskContext = require('../../services/task_context');

View File

@ -2,7 +2,7 @@
const noteService = require('../../services/notes'); const noteService = require('../../services/notes');
const eraseService = require('../../services/erase'); const eraseService = require('../../services/erase');
const treeService = require('../../services/tree.js'); const treeService = require('../../services/tree');
const sql = require('../../services/sql'); const sql = require('../../services/sql');
const utils = require('../../services/utils'); const utils = require('../../services/utils');
const log = require('../../services/log'); const log = require('../../services/log');

View File

@ -4,7 +4,7 @@ const sql = require('./sql');
const utils = require('./utils'); const utils = require('./utils');
const attributeService = require('./attributes'); const attributeService = require('./attributes');
const dateNoteService = require('./date_notes.js'); const dateNoteService = require('./date_notes.js');
const treeService = require('./tree.js'); const treeService = require('./tree');
const config = require('./config'); const config = require('./config');
const axios = require('axios'); const axios = require('axios');
const dayjs = require('dayjs'); const dayjs = require('dayjs');

View File

@ -1,4 +1,4 @@
const treeService = require('./tree.js'); const treeService = require('./tree');
const sql = require('./sql'); const sql = require('./sql');
function moveBranchToNote(branchToMove, targetParentNoteId) { function moveBranchToNote(branchToMove, targetParentNoteId) {

View File

@ -2,7 +2,7 @@
const sql = require('./sql'); const sql = require('./sql');
const eventChangesService = require('./entity_changes'); const eventChangesService = require('./entity_changes');
const treeService = require('./tree.js'); const treeService = require('./tree');
const BBranch = require('../becca/entities/bbranch'); const BBranch = require('../becca/entities/bbranch');
const becca = require('../becca/becca'); const becca = require('../becca/becca');
const log = require('./log'); const log = require('./log');

View File

@ -1,6 +1,6 @@
const eventService = require('./events'); const eventService = require('./events');
const scriptService = require('./script.js'); const scriptService = require('./script.js');
const treeService = require('./tree.js'); const treeService = require('./tree');
const noteService = require('./notes'); const noteService = require('./notes');
const becca = require('../becca/becca'); const becca = require('../becca/becca');
const BAttribute = require('../becca/entities/battribute'); const BAttribute = require('../becca/entities/battribute');

View File

@ -9,7 +9,7 @@ const BBranch = require('../../becca/entities/bbranch');
const path = require('path'); const path = require('path');
const protectedSessionService = require('../protected_session'); const protectedSessionService = require('../protected_session');
const mimeService = require('./mime.js'); const mimeService = require('./mime.js');
const treeService = require('../tree.js'); const treeService = require('../tree');
const yauzl = require("yauzl"); const yauzl = require("yauzl");
const htmlSanitizer = require('../html_sanitizer'); const htmlSanitizer = require('../html_sanitizer');
const becca = require('../../becca/becca'); const becca = require('../../becca/becca');

View File

@ -1,12 +1,13 @@
"use strict"; "use strict";
const sql = require('./sql'); import sql = require('./sql');
const log = require('./log'); import log = require('./log');
const BBranch = require('../becca/entities/bbranch'); import BBranch = require('../becca/entities/bbranch');
const entityChangesService = require('./entity_changes'); import entityChangesService = require('./entity_changes');
const becca = require('../becca/becca'); import becca = require('../becca/becca');
import BNote = require('../becca/entities/bnote');
function validateParentChild(parentNoteId, childNoteId, branchId = null) { function validateParentChild(parentNoteId: string, childNoteId: string, branchId: string | null = null) {
if (['root', '_hidden', '_share', '_lbRoot', '_lbAvailableLaunchers', '_lbVisibleLaunchers'].includes(childNoteId)) { if (['root', '_hidden', '_share', '_lbRoot', '_lbAvailableLaunchers', '_lbVisibleLaunchers'].includes(childNoteId)) {
return { branch: null, success: false, message: `Cannot change this note's location.` }; return { branch: null, success: false, message: `Cannot change this note's location.` };
} }
@ -25,7 +26,7 @@ function validateParentChild(parentNoteId, childNoteId, branchId = null) {
return { return {
branch: existingBranch, branch: existingBranch,
success: false, success: false,
message: `Note "${childNote.title}" note already exists in the "${parentNote.title}".` message: `Note "${childNote?.title}" note already exists in the "${parentNote?.title}".`
}; };
} }
@ -37,7 +38,7 @@ function validateParentChild(parentNoteId, childNoteId, branchId = null) {
}; };
} }
if (parentNoteId !== '_lbBookmarks' && becca.getNote(parentNoteId).type === 'launcher') { if (parentNoteId !== '_lbBookmarks' && becca.getNote(parentNoteId)?.type === 'launcher') {
return { return {
branch: null, branch: null,
success: false, success: false,
@ -51,7 +52,7 @@ function validateParentChild(parentNoteId, childNoteId, branchId = null) {
/** /**
* Tree cycle can be created when cloning or when moving existing clone. This method should detect both cases. * Tree cycle can be created when cloning or when moving existing clone. This method should detect both cases.
*/ */
function wouldAddingBranchCreateCycle(parentNoteId, childNoteId) { function wouldAddingBranchCreateCycle(parentNoteId: string, childNoteId: string) {
if (parentNoteId === childNoteId) { if (parentNoteId === childNoteId) {
return true; return true;
} }
@ -70,20 +71,22 @@ function wouldAddingBranchCreateCycle(parentNoteId, childNoteId) {
return parentAncestorNoteIds.some(parentAncestorNoteId => childSubtreeNoteIds.has(parentAncestorNoteId)); return parentAncestorNoteIds.some(parentAncestorNoteId => childSubtreeNoteIds.has(parentAncestorNoteId));
} }
function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, foldersFirst = false, sortNatural = false, sortLocale) { function sortNotes(parentNoteId: string, customSortBy: string = 'title', reverse = false, foldersFirst = false, sortNatural = false, _sortLocale?: string | null) {
if (!customSortBy) { if (!customSortBy) {
customSortBy = 'title'; customSortBy = 'title';
} }
if (!sortLocale) {
// sortLocale can not be empty string or null value, default value must be set to undefined. // sortLocale can not be empty string or null value, default value must be set to undefined.
sortLocale = undefined; const sortLocale = (_sortLocale || undefined);
}
sql.transactional(() => { sql.transactional(() => {
const notes = becca.getNote(parentNoteId).getChildNotes(); const note = becca.getNote(parentNoteId);
if (!note) {
throw new Error("Unable to find note");
}
const normalize = obj => (obj && typeof obj === 'string') ? obj.toLowerCase() : obj; const notes = note.getChildNotes();
const normalize = (obj: any) => (obj && typeof obj === 'string') ? obj.toLowerCase() : obj;
notes.sort((a, b) => { notes.sort((a, b) => {
if (foldersFirst) { if (foldersFirst) {
@ -96,7 +99,7 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder
} }
} }
function fetchValue(note, key) { function fetchValue(note: BNote, key: string) {
let rawValue; let rawValue;
if (key === 'title') { if (key === 'title') {
@ -105,14 +108,14 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder
rawValue = prefix ? `${prefix} - ${note.title}` : note.title; rawValue = prefix ? `${prefix} - ${note.title}` : note.title;
} else { } else {
rawValue = ['dateCreated', 'dateModified'].includes(key) rawValue = ['dateCreated', 'dateModified'].includes(key)
? note[key] ? (note as any)[key]
: note.getLabelValue(key); : note.getLabelValue(key);
} }
return normalize(rawValue); return normalize(rawValue);
} }
function compare(a, b) { function compare(a: string, b: string) {
if (!sortNatural) { if (!sortNatural) {
// alphabetical sort // alphabetical sort
return b === null || b === undefined || a < b ? -1 : 1; return b === null || b === undefined || a < b ? -1 : 1;
@ -160,6 +163,7 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder
for (const note of notes) { for (const note of notes) {
const branch = note.getParentBranches().find(b => b.parentNoteId === parentNoteId); const branch = note.getParentBranches().find(b => b.parentNoteId === parentNoteId);
if (!branch) { continue; }
if (branch.noteId === '_hidden') { if (branch.noteId === '_hidden') {
position = 999_999_999; position = 999_999_999;
@ -182,9 +186,8 @@ function sortNotes(parentNoteId, customSortBy = 'title', reverse = false, folder
}); });
} }
function sortNotesIfNeeded(parentNoteId) { function sortNotesIfNeeded(parentNoteId: string) {
const parentNote = becca.getNote(parentNoteId); const parentNote = becca.getNote(parentNoteId);
if (!parentNote) { if (!parentNote) {
return; return;
} }
@ -206,7 +209,7 @@ function sortNotesIfNeeded(parentNoteId) {
/** /**
* @deprecated this will be removed in the future * @deprecated this will be removed in the future
*/ */
function setNoteToParent(noteId, prefix, parentNoteId) { function setNoteToParent(noteId: string, prefix: string, parentNoteId: string) {
const parentNote = becca.getNote(parentNoteId); const parentNote = becca.getNote(parentNoteId);
if (parentNoteId && !parentNote) { if (parentNoteId && !parentNote) {
@ -215,7 +218,7 @@ function setNoteToParent(noteId, prefix, parentNoteId) {
} }
// case where there might be more such branches is ignored. It's expected there should be just one // case where there might be more such branches is ignored. It's expected there should be just one
const branchId = sql.getValue("SELECT branchId FROM branches WHERE isDeleted = 0 AND noteId = ? AND prefix = ?", [noteId, prefix]); const branchId = sql.getValue<string>("SELECT branchId FROM branches WHERE isDeleted = 0 AND noteId = ? AND prefix = ?", [noteId, prefix]);
const branch = becca.getBranch(branchId); const branch = becca.getBranch(branchId);
if (branch) { if (branch) {
@ -233,12 +236,15 @@ function setNoteToParent(noteId, prefix, parentNoteId) {
} }
else if (parentNoteId) { else if (parentNoteId) {
const note = becca.getNote(noteId); const note = becca.getNote(noteId);
if (!note) {
throw new Error(`Cannot find note '${noteId}.`);
}
if (note.isDeleted) { if (note.isDeleted) {
throw new Error(`Cannot create a branch for '${noteId}' which is deleted.`); throw new Error(`Cannot create a branch for '${noteId}' which is deleted.`);
} }
const branchId = sql.getValue('SELECT branchId FROM branches WHERE isDeleted = 0 AND noteId = ? AND parentNoteId = ?', [noteId, parentNoteId]); const branchId = sql.getValue<string>('SELECT branchId FROM branches WHERE isDeleted = 0 AND noteId = ? AND parentNoteId = ?', [noteId, parentNoteId]);
const branch = becca.getBranch(branchId); const branch = becca.getBranch(branchId);
if (branch) { if (branch) {