fix: documents.publish event not triggered if nothing else changed in doc (#2728)

This commit is contained in:
Tom Moor 2021-11-03 18:43:01 -07:00 committed by GitHub
parent ad3bb98087
commit 89115a53ca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 49 additions and 26 deletions

View File

@ -1067,32 +1067,30 @@ router.post("documents.update", auth(), async (ctx) => {
throw err; throw err;
} }
if (changed) { if (publish) {
if (publish) { await Event.create({
await Event.create({ name: "documents.publish",
name: "documents.publish", documentId: document.id,
documentId: document.id, collectionId: document.collectionId,
collectionId: document.collectionId, teamId: document.teamId,
teamId: document.teamId, actorId: user.id,
actorId: user.id, data: { title: document.title },
data: { title: document.title }, ip: ctx.request.ip,
ip: ctx.request.ip, });
}); } else if (changed) {
} else { await Event.create({
await Event.create({ name: "documents.update",
name: "documents.update", documentId: document.id,
documentId: document.id, collectionId: document.collectionId,
collectionId: document.collectionId, teamId: document.teamId,
teamId: document.teamId, actorId: user.id,
actorId: user.id, data: {
data: { autosave,
autosave, done,
done, title: document.title,
title: document.title, },
}, ip: ctx.request.ip,
ip: ctx.request.ip, });
});
}
} }
if (document.title !== previousTitle) { if (document.title !== previousTitle) {

View File

@ -8,6 +8,7 @@ import {
Backlink, Backlink,
CollectionUser, CollectionUser,
SearchQuery, SearchQuery,
Event,
} from "../../models"; } from "../../models";
import webService from "../../services/web"; import webService from "../../services/web";
import { import {
@ -2005,6 +2006,9 @@ describe("#documents.update", () => {
expect(res.status).toEqual(200); expect(res.status).toEqual(200);
expect(body.data.title).toBe("Updated title"); expect(body.data.title).toBe("Updated title");
expect(body.data.text).toBe("Updated text"); 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 () => { it("should not add template to collection structure when publishing", async () => {
@ -2068,6 +2072,9 @@ describe("#documents.update", () => {
expect(res.status).toEqual(200); expect(res.status).toEqual(200);
expect(body.data.publishedAt).toBeTruthy(); expect(body.data.publishedAt).toBeTruthy();
expect(body.policies[0].abilities.update).toEqual(true); 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 () => { it("should not edit archived document", async () => {
@ -2260,6 +2267,24 @@ describe("#documents.update", () => {
expect(body.data.text).toBe(""); 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 () => { it("should require authentication", async () => {
const { document } = await seed(); const { document } = await seed();
const res = await server.post("/api/documents.update", { const res = await server.post("/api/documents.update", {