Move bulk of webhook logic to plugin (#4866)
* Move bulk of webhook logic to plugin * Re-enable cleanup task * cron tasks
This commit is contained in:
96
plugins/webhooks/server/processors/WebhookProcessor.test.ts
Normal file
96
plugins/webhooks/server/processors/WebhookProcessor.test.ts
Normal file
@@ -0,0 +1,96 @@
|
||||
import { buildUser, buildWebhookSubscription } from "@server/test/factories";
|
||||
import { setupTestDatabase } from "@server/test/support";
|
||||
import { UserEvent } from "@server/types";
|
||||
import DeliverWebhookTask from "../tasks/DeliverWebhookTask";
|
||||
import WebhookProcessor from "./WebhookProcessor";
|
||||
|
||||
jest.mock("@server/queues/tasks/DeliverWebhookTask");
|
||||
const ip = "127.0.0.1";
|
||||
|
||||
setupTestDatabase();
|
||||
|
||||
beforeEach(async () => {
|
||||
jest.resetAllMocks();
|
||||
});
|
||||
|
||||
describe("WebhookProcessor", () => {
|
||||
test("it schedules a delivery for the event", async () => {
|
||||
const subscription = await buildWebhookSubscription({
|
||||
url: "http://example.com",
|
||||
events: ["*"],
|
||||
});
|
||||
const signedInUser = await buildUser({ teamId: subscription.teamId });
|
||||
const processor = new WebhookProcessor();
|
||||
|
||||
const event: UserEvent = {
|
||||
name: "users.signin",
|
||||
userId: signedInUser.id,
|
||||
teamId: subscription.teamId,
|
||||
actorId: signedInUser.id,
|
||||
ip,
|
||||
};
|
||||
|
||||
await processor.perform(event);
|
||||
|
||||
expect(DeliverWebhookTask.schedule).toHaveBeenCalled();
|
||||
expect(DeliverWebhookTask.schedule).toHaveBeenCalledWith({
|
||||
event,
|
||||
subscriptionId: subscription.id,
|
||||
});
|
||||
});
|
||||
|
||||
test("not schedule a delivery when not subscribed to event", async () => {
|
||||
const subscription = await buildWebhookSubscription({
|
||||
url: "http://example.com",
|
||||
events: ["users.create"],
|
||||
});
|
||||
const signedInUser = await buildUser({ teamId: subscription.teamId });
|
||||
const processor = new WebhookProcessor();
|
||||
const event: UserEvent = {
|
||||
name: "users.signin",
|
||||
userId: signedInUser.id,
|
||||
teamId: subscription.teamId,
|
||||
actorId: signedInUser.id,
|
||||
ip,
|
||||
};
|
||||
|
||||
await processor.perform(event);
|
||||
|
||||
expect(DeliverWebhookTask.schedule).toHaveBeenCalledTimes(0);
|
||||
});
|
||||
|
||||
test("it schedules a delivery for the event for each subscription", async () => {
|
||||
const subscription = await buildWebhookSubscription({
|
||||
url: "http://example.com",
|
||||
events: ["*"],
|
||||
});
|
||||
const subscriptionTwo = await buildWebhookSubscription({
|
||||
url: "http://example.com",
|
||||
teamId: subscription.teamId,
|
||||
events: ["*"],
|
||||
});
|
||||
const signedInUser = await buildUser({ teamId: subscription.teamId });
|
||||
const processor = new WebhookProcessor();
|
||||
|
||||
const event: UserEvent = {
|
||||
name: "users.signin",
|
||||
userId: signedInUser.id,
|
||||
teamId: subscription.teamId,
|
||||
actorId: signedInUser.id,
|
||||
ip,
|
||||
};
|
||||
|
||||
await processor.perform(event);
|
||||
|
||||
expect(DeliverWebhookTask.schedule).toHaveBeenCalled();
|
||||
expect(DeliverWebhookTask.schedule).toHaveBeenCalledTimes(2);
|
||||
expect(DeliverWebhookTask.schedule).toHaveBeenCalledWith({
|
||||
event,
|
||||
subscriptionId: subscription.id,
|
||||
});
|
||||
expect(DeliverWebhookTask.schedule).toHaveBeenCalledWith({
|
||||
event,
|
||||
subscriptionId: subscriptionTwo.id,
|
||||
});
|
||||
});
|
||||
});
|
||||
31
plugins/webhooks/server/processors/WebhookProcessor.ts
Normal file
31
plugins/webhooks/server/processors/WebhookProcessor.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { WebhookSubscription } from "@server/models";
|
||||
import BaseProcessor from "@server/queues/processors/BaseProcessor";
|
||||
import { Event } from "@server/types";
|
||||
import DeliverWebhookTask from "../tasks/DeliverWebhookTask";
|
||||
|
||||
export default class WebhookProcessor extends BaseProcessor {
|
||||
static applicableEvents: ["*"] = ["*"];
|
||||
|
||||
async perform(event: Event) {
|
||||
if (!event.teamId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const webhookSubscriptions = await WebhookSubscription.findAll({
|
||||
where: {
|
||||
enabled: true,
|
||||
teamId: event.teamId,
|
||||
},
|
||||
});
|
||||
|
||||
const applicableSubscriptions = webhookSubscriptions.filter((webhook) =>
|
||||
webhook.validForEvent(event)
|
||||
);
|
||||
|
||||
await Promise.all(
|
||||
applicableSubscriptions.map((subscription) =>
|
||||
DeliverWebhookTask.schedule({ event, subscriptionId: subscription.id })
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user