Skip to content

Build your personal knowledge base with Trilium Notes

feat(etapi): accept utcDateModified on create-note and note patch

Elian Doran8e876786e6f2
diff --git a/apps/server/spec/etapi/create-entities.spec.ts b/apps/server/spec/etapi/create-entities.spec.ts
index ddf98c522a..52684219e7 100644
--- a/apps/server/spec/etapi/create-entities.spec.ts
+++ b/apps/server/spec/etapi/create-entities.spec.ts
@@ -1,4 +1,4 @@
-import { BBranch, note_service as noteService } from "@triliumnext/core";
+import { BBranch, becca, note_service as noteService } from "@triliumnext/core";
import { Application } from "express";
import { afterEach, beforeAll, describe, expect, it, vi } from "vitest";
import supertest from "supertest";
@@ -76,6 +76,40 @@ describe("etapi/create-entities", () => {
});
});
+ it("keeps a given utcDateModified on create and on patch", async () => {
+ const utcDateModified = "2024-05-06 08:11:12.456Z";
+ const created = await supertest(app)
+ .post("/etapi/create-note")
+ .auth(USER, token, { "type": "basic"})
+ .send({
+ parentNoteId: "root",
+ title: "Imported",
+ type: "text",
+ content: "x",
+ utcDateModified
+ })
+ .expect(201);
+ const { noteId } = created.body.note;
+ expect(created.body.note.utcDateModified).toBe(utcDateModified);
+ expect(becca.getNoteOrThrow(noteId).utcDateModified).toBe(utcDateModified);
+
+ await supertest(app)
+ .put(`/etapi/notes/${noteId}/content`)
+ .auth(USER, token, { "type": "basic"})
+ .set("Content-Type", "text/plain")
+ .send("rewritten")
+ .expect(204);
+ expect(becca.getNoteOrThrow(noteId).utcDateModified).not.toBe(utcDateModified);
+
+ const patched = await supertest(app)
+ .patch(`/etapi/notes/${noteId}`)
+ .auth(USER, token, { "type": "basic"})
+ .send({ utcDateModified })
+ .expect(200);
+ expect(patched.body.utcDateModified).toBe(utcDateModified);
+ expect(becca.getNoteOrThrow(noteId).utcDateModified).toBe(utcDateModified);
+ });
+
it("obtains attribute information", async () => {
const response = await supertest(app)
.get(`/etapi/attributes/${createdAttributeId}`)
diff --git a/apps/server/src/assets/etapi.openapi.yaml b/apps/server/src/assets/etapi.openapi.yaml
index 35d77d9163..45d2fff99c 100644
--- a/apps/server/src/assets/etapi.openapi.yaml
+++ b/apps/server/src/assets/etapi.openapi.yaml
@@ -1166,6 +1166,12 @@ components:
utcDateCreated:
$ref: "#/components/schemas/UtcDateTime"
description: UTC timestap of the note creation. Specify only if you want to override the default (current datetime).
+ utcDateModified:
+ $ref: "#/components/schemas/UtcDateTime"
+ description: >
+ UTC timestamp of the note's last modification, e.g. to keep the date of a note being migrated
+ from another application. The local dateModified is derived from it. Specify only if you want
+ to override the default (current datetime).
Note:
type: object
properties:
@@ -1229,7 +1235,9 @@ components:
$ref: "#/components/schemas/UtcDateTime"
utcDateModified:
$ref: "#/components/schemas/UtcDateTime"
- readOnly: true
+ description: >
+ Writable through PATCH, e.g. to restore a migrated note's date after its content was rewritten.
+ The local dateModified is derived from it.
Branch:
type: object
description: Branch places the note into the tree, it represents the relationship between a parent note and child note
diff --git a/apps/server/src/etapi/notes.ts b/apps/server/src/etapi/notes.ts
index 1dec2c0895..01c39fbe3e 100644
--- a/apps/server/src/etapi/notes.ts
+++ b/apps/server/src/etapi/notes.ts
@@ -1,5 +1,5 @@
import { type ExportFormat, note_service as noteService, NoteParams, search as searchService, SearchContext, SearchParams, TaskContext, zipExportService, zipImportService } from "@triliumnext/core";
-import { becca } from "@triliumnext/core";
+import { type BNote, becca } from "@triliumnext/core";
import type { Request, Router } from "express";
import type { ParsedQs } from "qs";
@@ -51,7 +51,8 @@ function register(router: Router) {
isExpanded: [v.notNull, v.isBoolean],
noteId: [v.notNull, v.isValidEntityId],
dateCreated: [v.notNull, v.isString, v.isLocalDateTime],
- utcDateCreated: [v.notNull, v.isString, v.isUtcDateTime]
+ utcDateCreated: [v.notNull, v.isString, v.isUtcDateTime],
+ utcDateModified: [v.notNull, v.isString, v.isUtcDateTime]
};
eu.route(router, "post", "/etapi/create-note", (req, res, next) => {
@@ -66,6 +67,7 @@ function register(router: Router) {
try {
const resp = noteService.createNewNote(params);
+ restoreDateModified(resp.note, req.body.utcDateModified);
res.status(201).json({
note: mappers.mapNoteToPojo(resp.note),
@@ -81,7 +83,8 @@ function register(router: Router) {
type: [v.notNull, v.isString],
mime: [v.notNull, v.isString],
dateCreated: [v.notNull, v.isString, v.isLocalDateTime],
- utcDateCreated: [v.notNull, v.isString, v.isUtcDateTime]
+ utcDateCreated: [v.notNull, v.isString, v.isUtcDateTime],
+ utcDateModified: [v.notNull, v.isString, v.isUtcDateTime]
};
eu.route<{ noteId: string }>(router, "patch", "/etapi/notes/:noteId", (req, res, next) => {
@@ -102,6 +105,7 @@ function register(router: Router) {
noteService.saveRevisionIfNeeded(note);
eu.validateAndPatch(note, req.body, ALLOWED_PROPERTIES_FOR_PATCH);
note.save();
+ restoreDateModified(note, req.body.utcDateModified);
res.json(mappers.mapNoteToPojo(note));
});
@@ -191,6 +195,16 @@ function register(router: Router) {
});
}
+/**
+ * Applies a caller-supplied modification date. `save()` stamps the current time, so this runs after
+ * it, the same way the importers keep a source note's dates.
+ */
+function restoreDateModified(note: BNote, utcDateModified: string | undefined) {
+ if (utcDateModified) {
+ note.setDateCreatedAndModified(undefined, utcDateModified);
+ }
+}
+
function parseSearchParams(req: Request) {
const rawSearchParams: SearchParams = {
fastSearch: parseBoolean(req.query, "fastSearch"),