mirror of
https://github.com/outline/outline.git
synced 2026-11-27 16:08:58 +03:00
feat: Include collectionId in comment webhook payloads (#14176)
This commit is contained in:
1 parent
9094489273
commit
4e52850527
2 files changed
+109
-7
No files matched your search
@@ -6,16 +6,22 @@ import {
|
||||
type StrictRequest,
|
||||
} from "msw";
|
||||
import { server } from "@server/test/msw";
|
||||
import { WebhookDelivery } from "@server/models";
|
||||
import { Revision, WebhookDelivery } from "@server/models";
|
||||
import {
|
||||
buildCollection,
|
||||
buildComment,
|
||||
buildDocument,
|
||||
buildPersonalCollection,
|
||||
buildUser,
|
||||
buildWebhookDelivery,
|
||||
buildWebhookSubscription,
|
||||
} from "@server/test/factories";
|
||||
import type { DocumentEvent, UserEvent } from "@server/types";
|
||||
import type {
|
||||
CommentEvent,
|
||||
DocumentEvent,
|
||||
RevisionEvent,
|
||||
UserEvent,
|
||||
} from "@server/types";
|
||||
import DeliverWebhookTask, {
|
||||
isExpectedNetworkError,
|
||||
} from "./DeliverWebhookTask";
|
||||
@@ -160,6 +166,89 @@ describe("DeliverWebhookTask", () => {
|
||||
);
|
||||
});
|
||||
|
||||
test("should include the document collectionId in comment events", async () => {
|
||||
const user = await buildUser();
|
||||
const subscription = await buildWebhookSubscription({
|
||||
teamId: user.teamId,
|
||||
createdById: user.id,
|
||||
url: "http://example.com",
|
||||
events: ["*"],
|
||||
});
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
const comment = await buildComment({
|
||||
userId: user.id,
|
||||
documentId: document.id,
|
||||
});
|
||||
const captured = captureWebhook("http://example.com");
|
||||
|
||||
const event: CommentEvent = {
|
||||
name: "comments.create",
|
||||
modelId: comment.id,
|
||||
documentId: document.id,
|
||||
teamId: user.teamId,
|
||||
actorId: user.id,
|
||||
ip,
|
||||
};
|
||||
await new DeliverWebhookTask().perform({
|
||||
subscriptionId: subscription.id,
|
||||
event,
|
||||
});
|
||||
|
||||
expect(captured.length).toBe(1);
|
||||
const parsedBody = JSON.parse(captured[0].body);
|
||||
expect(parsedBody.payload.id).toBe(comment.id);
|
||||
expect(parsedBody.payload.model.collectionId).toBe(collection.id);
|
||||
expect(parsedBody.payload.model.documentId).toBe(document.id);
|
||||
});
|
||||
|
||||
test("should include the document collectionId in revision events", async () => {
|
||||
const user = await buildUser();
|
||||
const subscription = await buildWebhookSubscription({
|
||||
teamId: user.teamId,
|
||||
createdById: user.id,
|
||||
url: "http://example.com",
|
||||
events: ["*"],
|
||||
});
|
||||
const collection = await buildCollection({
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
});
|
||||
const document = await buildDocument({
|
||||
teamId: user.teamId,
|
||||
userId: user.id,
|
||||
collectionId: collection.id,
|
||||
});
|
||||
const revision = await Revision.buildFromDocument(document).save();
|
||||
const captured = captureWebhook("http://example.com");
|
||||
|
||||
const event: RevisionEvent = {
|
||||
name: "revisions.create",
|
||||
modelId: revision.id,
|
||||
documentId: document.id,
|
||||
teamId: user.teamId,
|
||||
actorId: user.id,
|
||||
ip,
|
||||
};
|
||||
await new DeliverWebhookTask().perform({
|
||||
subscriptionId: subscription.id,
|
||||
event,
|
||||
});
|
||||
|
||||
expect(captured.length).toBe(1);
|
||||
const parsedBody = JSON.parse(captured[0].body);
|
||||
expect(parsedBody.payload.id).toBe(revision.id);
|
||||
expect(parsedBody.payload.model.collectionId).toBe(collection.id);
|
||||
expect(parsedBody.payload.model.documentId).toBe(document.id);
|
||||
});
|
||||
|
||||
test("should hit the subscription url when the eventing model doesn't exist", async () => {
|
||||
const subscription = await buildWebhookSubscription({
|
||||
url: "http://example.com",
|
||||
|
||||
@@ -432,15 +432,26 @@ export default class DeliverWebhookTask extends BaseTask<Props> {
|
||||
subscription: WebhookSubscription,
|
||||
event: CommentEvent
|
||||
): Promise<void> {
|
||||
const model = await Comment.findByPk(event.modelId, {
|
||||
paranoid: false,
|
||||
});
|
||||
const [model, document] = await Promise.all([
|
||||
Comment.findByPk(event.modelId, {
|
||||
paranoid: false,
|
||||
}),
|
||||
Document.unscoped().findOne({
|
||||
attributes: ["collectionId"],
|
||||
where: { id: event.documentId },
|
||||
paranoid: false,
|
||||
}),
|
||||
]);
|
||||
|
||||
await this.sendWebhook({
|
||||
event,
|
||||
subscription,
|
||||
payload: {
|
||||
id: event.modelId,
|
||||
model: model && presentComment(model),
|
||||
model: model && {
|
||||
...presentComment(model),
|
||||
collectionId: document?.collectionId ?? null,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -744,7 +755,9 @@ export default class DeliverWebhookTask extends BaseTask<Props> {
|
||||
Revision.findByPk(event.modelId, {
|
||||
paranoid: false,
|
||||
}),
|
||||
Document.findByPk(event.documentId, {
|
||||
Document.unscoped().findOne({
|
||||
attributes: ["collectionId"],
|
||||
where: { id: event.documentId },
|
||||
paranoid: false,
|
||||
}),
|
||||
]);
|
||||
|
||||
Reference in new issue
Block a user