Webhook support for comments (#4932)
* fix: Restore newlines in code line numbers as safety measure * Add comments to webhook payloads
This commit is contained in:
@@ -5,10 +5,12 @@ import { useEffect } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { useTranslation, Trans } from "react-i18next";
|
||||
import styled from "styled-components";
|
||||
import { TeamPreference } from "@shared/types";
|
||||
import WebhookSubscription from "~/models/WebhookSubscription";
|
||||
import Button from "~/components/Button";
|
||||
import Input from "~/components/Input";
|
||||
import Text from "~/components/Text";
|
||||
import useCurrentTeam from "~/hooks/useCurrentTeam";
|
||||
import useMobile from "~/hooks/useMobile";
|
||||
|
||||
const WEBHOOK_EVENTS = {
|
||||
@@ -36,12 +38,6 @@ const WEBHOOK_EVENTS = {
|
||||
"documents.update",
|
||||
"documents.title_change",
|
||||
],
|
||||
revision: ["revisions.create"],
|
||||
fileOperation: [
|
||||
"fileOperations.create",
|
||||
"fileOperations.update",
|
||||
"fileOperations.delete",
|
||||
],
|
||||
collection: [
|
||||
"collections.create",
|
||||
"collections.update",
|
||||
@@ -53,6 +49,13 @@ const WEBHOOK_EVENTS = {
|
||||
"collections.move",
|
||||
"collections.permission_changed",
|
||||
],
|
||||
comment: ["comments.create", "comments.update", "comments.delete"],
|
||||
revision: ["revisions.create"],
|
||||
fileOperation: [
|
||||
"fileOperations.create",
|
||||
"fileOperations.update",
|
||||
"fileOperations.delete",
|
||||
],
|
||||
group: [
|
||||
"groups.create",
|
||||
"groups.update",
|
||||
@@ -155,6 +158,7 @@ function generateSigningSecret() {
|
||||
|
||||
function WebhookSubscriptionForm({ handleSubmit, webhookSubscription }: Props) {
|
||||
const { t } = useTranslation();
|
||||
const team = useCurrentTeam();
|
||||
const {
|
||||
register,
|
||||
handleSubmit: formHandleSubmit,
|
||||
@@ -270,19 +274,25 @@ function WebhookSubscriptionForm({ handleSubmit, webhookSubscription }: Props) {
|
||||
|
||||
<FieldSet disabled={isAllEventSelected}>
|
||||
<GroupGrid isMobile={isMobile}>
|
||||
{Object.entries(WEBHOOK_EVENTS).map(([group, events], i) => (
|
||||
<GroupWrapper key={i} isMobile={isMobile}>
|
||||
<EventCheckbox
|
||||
label={t(`All {{ groupName }} events`, { groupName: group })}
|
||||
value={group}
|
||||
/>
|
||||
<FieldSet disabled={selectedGroups.includes(group)}>
|
||||
{events.map((event) => (
|
||||
<EventCheckbox label={event} value={event} key={event} />
|
||||
))}
|
||||
</FieldSet>
|
||||
</GroupWrapper>
|
||||
))}
|
||||
{Object.entries(WEBHOOK_EVENTS)
|
||||
.filter(
|
||||
([group]) =>
|
||||
group !== "comment" ||
|
||||
team.getPreference(TeamPreference.Commenting)
|
||||
)
|
||||
.map(([group, events], i) => (
|
||||
<GroupWrapper key={i} isMobile={isMobile}>
|
||||
<EventCheckbox
|
||||
label={t(`All {{ groupName }} events`, { groupName: group })}
|
||||
value={group}
|
||||
/>
|
||||
<FieldSet disabled={selectedGroups.includes(group)}>
|
||||
{events.map((event) => (
|
||||
<EventCheckbox label={event} value={event} key={event} />
|
||||
))}
|
||||
</FieldSet>
|
||||
</GroupWrapper>
|
||||
))}
|
||||
</GroupGrid>
|
||||
</FieldSet>
|
||||
<Button
|
||||
|
||||
@@ -22,6 +22,7 @@ import {
|
||||
CollectionUser,
|
||||
CollectionGroup,
|
||||
GroupUser,
|
||||
Comment,
|
||||
} from "@server/models";
|
||||
import {
|
||||
presentCollection,
|
||||
@@ -39,12 +40,14 @@ import {
|
||||
presentMembership,
|
||||
presentGroupMembership,
|
||||
presentCollectionGroupMembership,
|
||||
presentComment,
|
||||
} from "@server/presenters";
|
||||
import BaseTask from "@server/queues/tasks/BaseTask";
|
||||
import {
|
||||
CollectionEvent,
|
||||
CollectionGroupEvent,
|
||||
CollectionUserEvent,
|
||||
CommentEvent,
|
||||
DocumentEvent,
|
||||
Event,
|
||||
FileOperationEvent,
|
||||
@@ -156,7 +159,7 @@ export default class DeliverWebhookTask extends BaseTask<Props> {
|
||||
case "comments.create":
|
||||
case "comments.update":
|
||||
case "comments.delete":
|
||||
// TODO
|
||||
await this.handleCommentEvent(subscription, event);
|
||||
return;
|
||||
case "groups.create":
|
||||
case "groups.update":
|
||||
@@ -285,6 +288,23 @@ export default class DeliverWebhookTask extends BaseTask<Props> {
|
||||
});
|
||||
}
|
||||
|
||||
private async handleCommentEvent(
|
||||
subscription: WebhookSubscription,
|
||||
event: CommentEvent
|
||||
): Promise<void> {
|
||||
const model = await Comment.findByPk(event.modelId, {
|
||||
paranoid: false,
|
||||
});
|
||||
await this.sendWebhook({
|
||||
event,
|
||||
subscription,
|
||||
payload: {
|
||||
id: event.modelId,
|
||||
model: model && presentComment(model),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
private async handlePinEvent(
|
||||
subscription: WebhookSubscription,
|
||||
event: PinEvent
|
||||
|
||||
Reference in New Issue
Block a user