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;
}
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) {

View File

@ -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", {