edited notes: move edited-notes related code to own module

moved as-is
This commit is contained in:
contributor 2025-11-10 20:42:56 +02:00
parent 445dfaaeb4
commit 6134722b70
3 changed files with 89 additions and 80 deletions

View File

@ -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<string>(/*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,
};

View File

@ -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<string>(/*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,

View File

@ -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);