diff --git a/packages/endpoint-micropub/lib/post-data.js b/packages/endpoint-micropub/lib/post-data.js index 96b93dcce..346ad085b 100644 --- a/packages/endpoint-micropub/lib/post-data.js +++ b/packages/endpoint-micropub/lib/post-data.js @@ -7,7 +7,7 @@ import makeDebug from "debug"; import { getSyndicateToProperty, normaliseProperties } from "./jf2.js"; import { getPostType } from "./post-type-discovery.js"; import * as updateMf2 from "./update.js"; -import { renderPath } from "./utils.js"; +import { getPostTemplateProperties, renderPath } from "./utils.js"; const debug = makeDebug("indiekit:endpoint-micropub:post-data"); @@ -119,7 +119,7 @@ export const postData = { let { path: _originalPath, properties } = await this.read(application, url); // Save incoming properties for later comparison - const _originalProperties = structuredClone(properties); + let oldProperties = structuredClone(properties); // Add properties if (operation.add) { @@ -143,6 +143,7 @@ export const postData = { // Normalise properties properties = normaliseProperties(publication, properties, timeZone); + oldProperties = normaliseProperties(publication, oldProperties, timeZone); // Post type const type = getPostType(postTypes, properties); @@ -164,8 +165,10 @@ export const postData = { ); properties.url = getCanonicalUrl(updatedUrl, me); - // Return if no changes to properties detected - if (isDeepStrictEqual(properties, _originalProperties)) { + // Return if no changes to template properties detected + const newProperties = getPostTemplateProperties(properties); + oldProperties = getPostTemplateProperties(oldProperties); + if (isDeepStrictEqual(newProperties, oldProperties)) { return; } diff --git a/packages/endpoint-micropub/test/unit/post-data.js b/packages/endpoint-micropub/test/unit/post-data.js index ac35b1d51..2f8897580 100644 --- a/packages/endpoint-micropub/test/unit/post-data.js +++ b/packages/endpoint-micropub/test/unit/post-data.js @@ -13,10 +13,11 @@ describe("endpoint-micropub/lib/post-data", async () => { const properties = { type: "entry", published: "2020-07-26T20:10:57.062Z", - name: "Foo", + content: "Foo", + category: ["foo", "bar"], "mp-slug": "foo", }; - const url = "https://website.example/foo"; + const url = "https://website.example/notes/foo/"; before(() => { mock.method(console, "info", () => {}); @@ -25,20 +26,6 @@ describe("endpoint-micropub/lib/post-data", async () => { beforeEach(async () => { const postsCollection = database.collection("posts"); - await postsCollection.insertOne({ - path: "foo", - properties: { - type: "entry", - content: "hello world", - published: "2019-08-17T23:56:38.977+01:00", - category: ["foo", "bar"], - "mp-slug": "baz", - "mp-syndicate-to": "https://archive.org/", - "post-type": "note", - url, - }, - }); - const config = await testConfig(); const collections = new Map([["posts", postsCollection]]); @@ -57,7 +44,7 @@ describe("endpoint-micropub/lib/post-data", async () => { assert.equal(result.properties["post-type"], "note"); assert.equal(result.properties.slug, "foo"); assert.equal(result.properties.type, "entry"); - assert.equal(result.properties.url, "https://website.example/notes/foo/"); + assert.equal(result.properties.url, url); }); it("Throws error creating post data for non-configured post type", async () => { @@ -75,7 +62,7 @@ describe("endpoint-micropub/lib/post-data", async () => { const result = await postData.read(application, url); assert.equal(result.properties["post-type"], "note"); - assert.equal(result.properties.url, "https://website.example/foo"); + assert.equal(result.properties.url, url); }); it("Updates post by adding properties", async () => { @@ -90,6 +77,18 @@ describe("endpoint-micropub/lib/post-data", async () => { assert.ok(result.properties.syndication); }); + it("Doesn’t update post if no changes", async () => { + const operation = { replace: { content: ["Foo"] } }; + const result = await postData.update( + application, + publication, + url, + operation, + ); + + assert.equal(result, undefined); + }); + it("Updates post by replacing properties", async () => { const operation = { replace: { content: ["hello moon"] } }; const result = await postData.update(