diff --git a/server/routes/api/documents.js b/server/routes/api/documents.js index 4580044dd..1c1f26c5a 100644 --- a/server/routes/api/documents.js +++ b/server/routes/api/documents.js @@ -1067,32 +1067,30 @@ router.post("documents.update", auth(), async (ctx) => { throw err; } - if (changed) { - if (publish) { - await Event.create({ - name: "documents.publish", - documentId: document.id, - collectionId: document.collectionId, - teamId: document.teamId, - actorId: user.id, - data: { title: document.title }, - ip: ctx.request.ip, - }); - } else { - await Event.create({ - name: "documents.update", - documentId: document.id, - collectionId: document.collectionId, - teamId: document.teamId, - actorId: user.id, - data: { - autosave, - done, - title: document.title, - }, - ip: ctx.request.ip, - }); - } + if (publish) { + await Event.create({ + name: "documents.publish", + documentId: document.id, + collectionId: document.collectionId, + teamId: document.teamId, + actorId: user.id, + data: { title: document.title }, + ip: ctx.request.ip, + }); + } else if (changed) { + await Event.create({ + name: "documents.update", + documentId: document.id, + collectionId: document.collectionId, + teamId: document.teamId, + actorId: user.id, + data: { + autosave, + done, + title: document.title, + }, + ip: ctx.request.ip, + }); } if (document.title !== previousTitle) { diff --git a/server/routes/api/documents.test.js b/server/routes/api/documents.test.js index 11ec9f6a5..561d62983 100644 --- a/server/routes/api/documents.test.js +++ b/server/routes/api/documents.test.js @@ -8,6 +8,7 @@ import { Backlink, CollectionUser, SearchQuery, + Event, } from "../../models"; import webService from "../../services/web"; import { @@ -2005,6 +2006,9 @@ describe("#documents.update", () => { expect(res.status).toEqual(200); expect(body.data.title).toBe("Updated title"); expect(body.data.text).toBe("Updated text"); + + const events = await Event.findAll(); + expect(events.length).toEqual(1); }); it("should not add template to collection structure when publishing", async () => { @@ -2068,6 +2072,9 @@ describe("#documents.update", () => { expect(res.status).toEqual(200); expect(body.data.publishedAt).toBeTruthy(); expect(body.policies[0].abilities.update).toEqual(true); + + const events = await Event.findAll(); + expect(events.length).toEqual(1); }); it("should not edit archived document", async () => { @@ -2260,6 +2267,24 @@ describe("#documents.update", () => { expect(body.data.text).toBe(""); }); + it("should not produce event if nothing changes", async () => { + const { user, document } = await seed(); + + const res = await server.post("/api/documents.update", { + body: { + token: user.getJwtToken(), + id: document.id, + lastRevision: document.revision, + title: document.title, + text: document.text, + }, + }); + expect(res.status).toEqual(200); + + const events = await Event.findAll(); + expect(events.length).toEqual(0); + }); + it("should require authentication", async () => { const { document } = await seed(); const res = await server.post("/api/documents.update", {