From 6134722b70832d6d73985e8c5b2456ea34a61dca Mon Sep 17 00:00:00 2001 From: contributor Date: Mon, 10 Nov 2025 20:42:56 +0200 Subject: [PATCH] edited notes: move edited-notes related code to own module moved as-is --- apps/server/src/routes/api/edited-notes.ts | 86 ++++++++++++++++++++++ apps/server/src/routes/api/revisions.ts | 80 +------------------- apps/server/src/routes/routes.ts | 3 +- 3 files changed, 89 insertions(+), 80 deletions(-) create mode 100644 apps/server/src/routes/api/edited-notes.ts diff --git a/apps/server/src/routes/api/edited-notes.ts b/apps/server/src/routes/api/edited-notes.ts new file mode 100644 index 000000000..953e5b4d0 --- /dev/null +++ b/apps/server/src/routes/api/edited-notes.ts @@ -0,0 +1,86 @@ +import beccaService from "../../becca/becca_service.js"; +import sql from "../../services/sql.js"; +import cls from "../../services/cls.js"; +import becca from "../../becca/becca.js"; +import type { Request } from "express"; +import { NotePojo } from "../../becca/becca-interface.js"; +import type BNote from "../../becca/entities/bnote.js"; +import { EditedNotesResponse } from "@triliumnext/commons"; + +interface NotePath { + noteId: string; + branchId?: string; + title: string; + notePath: string[]; + path: string; +} + +interface NotePojoWithNotePath extends NotePojo { + notePath?: string[] | null; +} + +function getEditedNotesOnDate(req: Request) { + + const noteIds = sql.getColumn(/*sql*/`\ + SELECT notes.* + FROM notes + WHERE noteId IN ( + SELECT noteId FROM notes + WHERE + (notes.dateCreated LIKE :date OR notes.dateModified LIKE :date) + AND (notes.noteId NOT LIKE '\\_%' ESCAPE '\\') + UNION ALL + SELECT noteId FROM revisions + WHERE revisions.dateCreated LIKE :date + ) + ORDER BY isDeleted + LIMIT 50`, + { date: `${req.params.date}%` } + ); + + let notes = becca.getNotes(noteIds, true); + + // Narrow down the results if a note is hoisted, similar to "Jump to note". + const hoistedNoteId = cls.getHoistedNoteId(); + if (hoistedNoteId !== "root") { + notes = notes.filter((note) => note.hasAncestor(hoistedNoteId)); + } + + return notes.map((note) => { + const notePath = getNotePathData(note); + + const notePojo: NotePojoWithNotePath = note.getPojo(); + notePojo.notePath = notePath ? notePath.notePath : null; + + return notePojo; + }) satisfies EditedNotesResponse; +} + +function getNotePathData(note: BNote): NotePath | undefined { + const retPath = note.getBestNotePath(); + + if (retPath) { + const noteTitle = beccaService.getNoteTitleForPath(retPath); + + let branchId; + + if (note.isRoot()) { + branchId = "none_root"; + } else { + const parentNote = note.parents[0]; + branchId = becca.getBranchFromChildAndParent(note.noteId, parentNote.noteId)?.branchId; + } + + return { + noteId: note.noteId, + branchId: branchId, + title: noteTitle, + notePath: retPath, + path: retPath.join("/") + }; + } +} + +export default { + getEditedNotesOnDate, +}; diff --git a/apps/server/src/routes/api/revisions.ts b/apps/server/src/routes/api/revisions.ts index 9700e7f78..7780605da 100644 --- a/apps/server/src/routes/api/revisions.ts +++ b/apps/server/src/routes/api/revisions.ts @@ -1,30 +1,14 @@ "use strict"; -import beccaService from "../../becca/becca_service.js"; import utils from "../../services/utils.js"; import sql from "../../services/sql.js"; -import cls from "../../services/cls.js"; import path from "path"; import becca from "../../becca/becca.js"; import blobService from "../../services/blob.js"; import eraseService from "../../services/erase.js"; import type { Request, Response } from "express"; import type BRevision from "../../becca/entities/brevision.js"; -import type BNote from "../../becca/entities/bnote.js"; -import type { NotePojo } from "../../becca/becca-interface.js"; -import { EditedNotesResponse, RevisionItem, RevisionPojo, RevisionRow } from "@triliumnext/commons"; - -interface NotePath { - noteId: string; - branchId?: string; - title: string; - notePath: string[]; - path: string; -} - -interface NotePojoWithNotePath extends NotePojo { - notePath?: string[] | null; -} +import { RevisionItem, RevisionPojo } from "@triliumnext/commons"; function getRevisionBlob(req: Request) { const preview = req.query.preview === "true"; @@ -151,73 +135,11 @@ function restoreRevision(req: Request) { } } -function getEditedNotesOnDate(req: Request) { - const noteIds = sql.getColumn(/*sql*/`\ - SELECT notes.* - FROM notes - WHERE noteId IN ( - SELECT noteId FROM notes - WHERE - (notes.dateCreated LIKE :date OR notes.dateModified LIKE :date) - AND (notes.noteId NOT LIKE '\\_%' ESCAPE '\\') - UNION ALL - SELECT noteId FROM revisions - WHERE revisions.dateCreated LIKE :date - ) - ORDER BY isDeleted - LIMIT 50`, - { date: `${req.params.date}%` } - ); - - let notes = becca.getNotes(noteIds, true); - - // Narrow down the results if a note is hoisted, similar to "Jump to note". - const hoistedNoteId = cls.getHoistedNoteId(); - if (hoistedNoteId !== "root") { - notes = notes.filter((note) => note.hasAncestor(hoistedNoteId)); - } - - return notes.map((note) => { - const notePath = getNotePathData(note); - - const notePojo: NotePojoWithNotePath = note.getPojo(); - notePojo.notePath = notePath ? notePath.notePath : null; - - return notePojo; - }) satisfies EditedNotesResponse; -} - -function getNotePathData(note: BNote): NotePath | undefined { - const retPath = note.getBestNotePath(); - - if (retPath) { - const noteTitle = beccaService.getNoteTitleForPath(retPath); - - let branchId; - - if (note.isRoot()) { - branchId = "none_root"; - } else { - const parentNote = note.parents[0]; - branchId = becca.getBranchFromChildAndParent(note.noteId, parentNote.noteId)?.branchId; - } - - return { - noteId: note.noteId, - branchId: branchId, - title: noteTitle, - notePath: retPath, - path: retPath.join("/") - }; - } -} - export default { getRevisionBlob, getRevisions, getRevision, downloadRevision, - getEditedNotesOnDate, eraseAllRevisions, eraseAllExcessRevisions, eraseRevision, diff --git a/apps/server/src/routes/routes.ts b/apps/server/src/routes/routes.ts index 78a1380b7..5b4dae29a 100644 --- a/apps/server/src/routes/routes.ts +++ b/apps/server/src/routes/routes.ts @@ -22,6 +22,7 @@ import attachmentsApiRoute from "./api/attachments.js"; import autocompleteApiRoute from "./api/autocomplete.js"; import cloningApiRoute from "./api/cloning.js"; import revisionsApiRoute from "./api/revisions.js"; +import editedNotesApiRoute from "./api/edited-notes.js"; import recentChangesApiRoute from "./api/recent_changes.js"; import optionsApiRoute from "./api/options.js"; import passwordApiRoute from "./api/password.js"; @@ -349,7 +350,7 @@ function register(app: express.Application) { apiRoute(GET, "/api/other/icon-usage", otherRoute.getIconUsage); apiRoute(PST, "/api/other/render-markdown", otherRoute.renderMarkdown); apiRoute(GET, "/api/recent-changes/:ancestorNoteId", recentChangesApiRoute.getRecentChanges); - apiRoute(GET, "/api/edited-notes/:date", revisionsApiRoute.getEditedNotesOnDate); + apiRoute(GET, "/api/edited-notes/:date", editedNotesApiRoute.getEditedNotesOnDate); apiRoute(PST, "/api/note-map/:noteId/tree", noteMapRoute.getTreeMap); apiRoute(PST, "/api/note-map/:noteId/link", noteMapRoute.getLinkMap);