trilium/apps/server/src/routes/api/edited-notes.ts
2025-11-26 11:13:22 +02:00

87 lines
2.4 KiB
TypeScript

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,
};