Skip to content

A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.

TypeScript fluxerfluxerapp
22.9 KB
Raw Blame
// SPDX-License-Identifier: AGPL-3.0-or-later

import {
	createChannelID,
	createGuildID,
	createMessageID,
	createUserID,
	createWebhookID,
	createWebhookToken,
} from '@app/api/BrandedTypes';
import type {MessageRequest} from '@app/api/channel/MessageTypes';
import {normalizeMessageRequestPayload} from '@app/api/channel/services/message/MessageRequestCompatibility';
import {parseMultipartMessageData} from '@app/api/channel/services/message/MessageRequestParser';
import {viewerFromCtx} from '@app/api/experiment/ChannelThreadsGate';
import {LoginRequired} from '@app/api/middleware/AuthMiddleware';
import {BlockAppOriginMiddleware} from '@app/api/middleware/BlockAppOriginMiddleware';
import {RateLimitMiddleware} from '@app/api/middleware/RateLimitMiddleware';
import {
	isMultipartFormData,
	MultipartUploadRoute,
	RequestBodyLimit,
} from '@app/api/middleware/RequestBodyLimitMiddleware';
import {OpenAPI} from '@app/api/middleware/ResponseTypeMiddleware';
import {RateLimitConfigs} from '@app/api/RateLimitConfig';
import type {HonoApp, HonoEnv} from '@app/api/types/HonoEnv';
import {assertAccountNotLimited} from '@app/api/user/AccountLimit';
import {parseJsonPreservingLargeIntegers} from '@app/api/utils/LosslessJsonParser';
import {inputValidationErrorFromZodIssues, Validator} from '@app/api/Validator';
import type {WebhookExecuteMessageData} from '@app/api/webhook/WebhookService';
import {DELETED_USER_ID} from '@fluxer/constants/src/UserConstants';
import {ValidationErrorCodes} from '@fluxer/constants/src/ValidationErrorCodes';
import {InputValidationError} from '@fluxer/errors/src/domains/core/InputValidationError';
import {UnknownUserError} from '@fluxer/errors/src/domains/user/UnknownUserError';
import {
	ChannelIdParam,
	GuildIdParam,
	WebhookIdParam,
	WebhookIdTokenMessageIdParam,
	WebhookIdTokenParam,
} from '@fluxer/schema/src/domains/common/CommonParamSchemas';
import {MessageRequestSchema} from '@fluxer/schema/src/domains/message/MessageRequestSchemas';
import {MessageResponseSchema} from '@fluxer/schema/src/domains/message/MessageResponseSchemas';
import {GitHubWebhook} from '@fluxer/schema/src/domains/webhook/GitHubWebhookSchemas';
import {InstatusWebhook} from '@fluxer/schema/src/domains/webhook/InstatusWebhookSchemas';
import {LibredeskWebhook} from '@fluxer/schema/src/domains/webhook/LibredeskWebhookSchemas';
import {
	SlackWebhookRequest,
	WebhookCreateRequest,
	WebhookExecuteQueryRequest,
	WebhookMessageEditRequest,
	WebhookMessageQueryRequest,
	WebhookMessageRequest,
	WebhookMultipartMessageRequest,
	WebhookTokenUpdateRequest,
	WebhookUpdateRequest,
} from '@fluxer/schema/src/domains/webhook/WebhookRequestSchemas';
import {
	SlackWebhookResponse,
	WebhookCreateResponse,
	WebhookListResponse,
	WebhookResponse,
	WebhookTokenResponse,
} from '@fluxer/schema/src/domains/webhook/WebhookSchemas';
import type {Context} from 'hono';
import type {z} from 'zod';

const GITHUB_WEBHOOK_MAX_BODY_BYTES = 25 * 1024 * 1024;
const FORUM_POST_FIELDS = {thread_name: true, applied_tags: true} as const;
const WebhookMessagePayload = WebhookMessageRequest.omit(FORUM_POST_FIELDS);
const WebhookMultipartMessagePayload = WebhookMultipartMessageRequest.omit(FORUM_POST_FIELDS);

function validateWebhookMessagePayload(data: unknown): z.infer<typeof WebhookMessagePayload> {
	const validationResult = WebhookMessagePayload.safeParse(normalizeMessageRequestPayload(data));
	if (!validationResult.success) {
		throw inputValidationErrorFromZodIssues(validationResult.error.issues);
	}
	return validationResult.data;
}

function validateWebhookMultipartMessagePayload(data: unknown): z.infer<typeof WebhookMultipartMessagePayload> {
	const validationResult = WebhookMultipartMessagePayload.safeParse(normalizeMessageRequestPayload(data));
	if (!validationResult.success) {
		throw inputValidationErrorFromZodIssues(validationResult.error.issues);
	}
	return validationResult.data;
}

function pickForumPostFields(payload: unknown): Pick<WebhookExecuteMessageData, 'thread_name' | 'applied_tags'> {
	if (typeof payload !== 'object' || payload === null || Array.isArray(payload)) return {};
	const {thread_name, applied_tags} = payload as Record<string, unknown>;
	return {
		...(thread_name !== undefined ? {thread_name} : {}),
		...(applied_tags !== undefined ? {applied_tags} : {}),
	};
}

async function parseWebhookJsonMessageData(ctx: Context<HonoEnv>): Promise<WebhookExecuteMessageData> {
	let data: unknown;
	try {
		const raw = await ctx.req.text();
		data = raw.trim().length === 0 ? {} : parseJsonPreservingLargeIntegers(raw);
	} catch {
		data = {};
	}
	return {...validateWebhookMessagePayload(data), ...pickForumPostFields(data)};
}

async function parseWebhookMultipartMessageData(
	ctx: Context<HonoEnv>,
	webhookId: bigint,
	token: string,
): Promise<WebhookExecuteMessageData> {
	const webhook = await ctx.get('webhookService').getWebhookByToken({
		webhookId: createWebhookID(webhookId),
		token: createWebhookToken(token),
	});
	const userRepository = ctx.get('userRepository');
	const creator =
		(webhook.creatorId ? await userRepository.findUnique(webhook.creatorId) : null) ??
		(await userRepository.findUnique(createUserID(DELETED_USER_ID)));
	if (!creator) {
		throw new UnknownUserError();
	}
	let parsedPayload: unknown = null;
	const messageData: MessageRequest = await parseMultipartMessageData(
		ctx,
		creator,
		webhook.channelId!,
		MessageRequestSchema,
		{
			onPayloadParsed(payload) {
				parsedPayload = payload;
			},
			actor: 'webhook',
		},
	);
	if (!parsedPayload) {
		throw InputValidationError.fromCode('message_data', ValidationErrorCodes.INVALID_MESSAGE_DATA);
	}
	const webhookData = validateWebhookMultipartMessagePayload(parsedPayload);
	return {
		...webhookData,
		...messageData,
		...pickForumPostFields(parsedPayload),
		attachments: messageData.attachments,
		username: webhookData.username,
		avatar_url: webhookData.avatar_url,
	};
}

export function WebhookController(app: HonoApp) {
	app.get(
		'/guilds/:guild_id/webhooks',
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_LIST_GUILD),
		LoginRequired,
		OpenAPI({
			operationId: 'list_guild_webhooks',
			summary: 'List guild webhooks',
			description:
				'Returns a list of all webhooks configured in the specified guild. Requires the user to have appropriate permissions to view webhooks in the guild.',
			responseSchema: WebhookListResponse,
			statusCode: 200,
			security: ['botToken', 'bearerToken', 'sessionToken'],
			tags: ['Webhooks'],
		}),
		Validator('param', GuildIdParam),
		async (ctx) => {
			const response = await ctx.get('webhookRequestService').listGuildWebhooks({
				userId: ctx.get('user').id,
				viewer: viewerFromCtx(ctx),
				guildId: createGuildID(ctx.req.valid('param').guild_id),
				requestCache: ctx.get('requestCache'),
			});
			return ctx.json(response);
		},
	);
	app.get(
		'/channels/:channel_id/webhooks',
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_LIST_CHANNEL),
		LoginRequired,
		OpenAPI({
			operationId: 'list_channel_webhooks',
			summary: 'List channel webhooks',
			description:
				'Returns a list of all webhooks configured in the specified channel. Requires the user to have appropriate permissions to view webhooks in the channel.',
			responseSchema: WebhookListResponse,
			statusCode: 200,
			security: ['botToken', 'bearerToken', 'sessionToken'],
			tags: ['Webhooks'],
		}),
		Validator('param', ChannelIdParam),
		async (ctx) => {
			const response = await ctx.get('webhookRequestService').listChannelWebhooks({
				viewer: viewerFromCtx(ctx),
				userId: ctx.get('user').id,
				channelId: createChannelID(ctx.req.valid('param').channel_id),
				requestCache: ctx.get('requestCache'),
			});
			return ctx.json(response);
		},
	);
	app.post(
		'/channels/:channel_id/webhooks',
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_CREATE),
		LoginRequired,
		OpenAPI({
			operationId: 'create_webhook',
			summary: 'Create webhook',
			description:
				'Creates a new webhook in the specified channel with the provided name and optional avatar. Returns the newly created webhook object including its ID and token.',
			responseSchema: WebhookCreateResponse,
			statusCode: 200,
			security: ['botToken', 'bearerToken', 'sessionToken'],
			tags: ['Webhooks'],
		}),
		Validator('param', ChannelIdParam),
		Validator('json', WebhookCreateRequest),
		async (ctx) => {
			assertAccountNotLimited(ctx.get('user'));
			const auditLogReason = ctx.get('auditLogReason') ?? null;
			const response = await ctx.get('webhookRequestService').createWebhook({
				viewer: viewerFromCtx(ctx),
				userId: ctx.get('user').id,
				channelId: createChannelID(ctx.req.valid('param').channel_id),
				data: ctx.req.valid('json'),
				requestCache: ctx.get('requestCache'),
				auditLogReason,
			});
			return ctx.json(response);
		},
	);
	app.get(
		'/webhooks/:webhook_id',
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_GET),
		LoginRequired,
		OpenAPI({
			operationId: 'get_webhook',
			summary: 'Get webhook',
			description:
				'Retrieves detailed information about a specific webhook by its ID. Requires authentication and appropriate permissions to access the webhook.',
			responseSchema: WebhookResponse,
			statusCode: 200,
			security: ['botToken', 'bearerToken', 'sessionToken'],
			tags: ['Webhooks'],
		}),
		Validator('param', WebhookIdParam),
		async (ctx) => {
			const response = await ctx.get('webhookRequestService').getWebhook({
				userId: ctx.get('user').id,
				viewer: viewerFromCtx(ctx),
				webhookId: createWebhookID(ctx.req.valid('param').webhook_id),
				requestCache: ctx.get('requestCache'),
			});
			return ctx.json(response);
		},
	);
	app.patch(
		'/webhooks/:webhook_id',
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_UPDATE),
		LoginRequired,
		OpenAPI({
			operationId: 'update_webhook',
			summary: 'Update webhook',
			description:
				'Updates the specified webhook with new settings such as name, avatar, or target channel. All fields are optional. Returns the updated webhook object.',
			responseSchema: WebhookResponse,
			statusCode: 200,
			security: ['botToken', 'bearerToken', 'sessionToken'],
			tags: ['Webhooks'],
		}),
		Validator('param', WebhookIdParam),
		Validator('json', WebhookUpdateRequest),
		async (ctx) => {
			const auditLogReason = ctx.get('auditLogReason') ?? null;
			const response = await ctx.get('webhookRequestService').updateWebhook({
				userId: ctx.get('user').id,
				viewer: viewerFromCtx(ctx),
				webhookId: createWebhookID(ctx.req.valid('param').webhook_id),
				data: ctx.req.valid('json'),
				requestCache: ctx.get('requestCache'),
				auditLogReason,
			});
			return ctx.json(response);
		},
	);
	app.delete(
		'/webhooks/:webhook_id',
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_DELETE),
		LoginRequired,
		OpenAPI({
			operationId: 'delete_webhook',
			summary: 'Delete webhook',
			description:
				'Permanently deletes the specified webhook. This action cannot be undone. Returns a 204 status code on successful deletion.',
			responseSchema: null,
			statusCode: 204,
			security: ['botToken', 'bearerToken', 'sessionToken'],
			tags: ['Webhooks'],
		}),
		Validator('param', WebhookIdParam),
		async (ctx) => {
			const auditLogReason = ctx.get('auditLogReason') ?? null;
			await ctx.get('webhookRequestService').deleteWebhook({
				userId: ctx.get('user').id,
				viewer: viewerFromCtx(ctx),
				webhookId: createWebhookID(ctx.req.valid('param').webhook_id),
				auditLogReason,
			});
			return ctx.body(null, 204);
		},
	);
	app.get(
		'/webhooks/:webhook_id/:token',
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_GET),
		OpenAPI({
			operationId: 'get_webhook_with_token',
			summary: 'Get webhook with token',
			description:
				'Retrieves detailed information about a specific webhook using its ID and token. No authentication required as the token serves as the credential. Returns the webhook object without creator user data.',
			responseSchema: WebhookTokenResponse,
			statusCode: 200,
			tags: ['Webhooks'],
		}),
		Validator('param', WebhookIdTokenParam),
		async (ctx) => {
			const {webhook_id: webhookId, token} = ctx.req.valid('param');
			const response = await ctx.get('webhookRequestService').getWebhook({
				webhookId: createWebhookID(webhookId),
				token: createWebhookToken(token),
				requestCache: ctx.get('requestCache'),
			});
			return ctx.json(response);
		},
	);
	app.patch(
		'/webhooks/:webhook_id/:token',
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_UPDATE),
		OpenAPI({
			operationId: 'update_webhook_with_token',
			summary: 'Update webhook with token',
			description:
				'Updates the specified webhook using its ID and token for authentication. Allows modification of name or avatar. Returns the updated webhook object without creator user data.',
			responseSchema: WebhookTokenResponse,
			statusCode: 200,
			tags: ['Webhooks'],
		}),
		Validator('param', WebhookIdTokenParam),
		Validator('json', WebhookTokenUpdateRequest),
		async (ctx) => {
			const {webhook_id: webhookId, token} = ctx.req.valid('param');
			const response = await ctx.get('webhookRequestService').updateWebhook({
				webhookId: createWebhookID(webhookId),
				token: createWebhookToken(token),
				data: ctx.req.valid('json'),
				requestCache: ctx.get('requestCache'),
			});
			return ctx.json(response);
		},
	);
	app.delete(
		'/webhooks/:webhook_id/:token',
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_DELETE),
		OpenAPI({
			operationId: 'delete_webhook_with_token',
			summary: 'Delete webhook with token',
			description:
				'Permanently deletes the specified webhook using its ID and token for authentication. This action cannot be undone. Returns a 204 status code on successful deletion.',
			responseSchema: null,
			statusCode: 204,
			tags: ['Webhooks'],
		}),
		Validator('param', WebhookIdTokenParam),
		async (ctx) => {
			const {webhook_id: webhookId, token} = ctx.req.valid('param');
			await ctx.get('webhookRequestService').deleteWebhook({
				webhookId: createWebhookID(webhookId),
				token: createWebhookToken(token),
			});
			return ctx.body(null, 204);
		},
	);
	app.post(
		'/webhooks/:webhook_id/:token',
		MultipartUploadRoute(),
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_EXECUTE),
		BlockAppOriginMiddleware,
		OpenAPI({
			operationId: 'execute_webhook',
			summary: 'Execute webhook',
			description:
				'Executes the webhook by sending a message to its configured channel. If the wait query parameter is true, returns the created message object; otherwise returns a 204 status with no content.',
			responseSchema: MessageResponseSchema,
			requestSchema: WebhookMessageRequest,
			requestFormSchema: WebhookMultipartMessageRequest,
			statusCode: 200,
			tags: ['Webhooks'],
		}),
		Validator('param', WebhookIdTokenParam),
		Validator('query', WebhookExecuteQueryRequest),
		async (ctx) => {
			const {webhook_id: webhookId, token} = ctx.req.valid('param');
			const {wait} = ctx.req.valid('query');
			const data = isMultipartFormData(ctx.req.header('content-type'))
				? await parseWebhookMultipartMessageData(ctx, webhookId, token)
				: await parseWebhookJsonMessageData(ctx);
			const response = await ctx.get('webhookRequestService').executeWebhook({
				webhookId: createWebhookID(webhookId),
				token: createWebhookToken(token),
				threadId: ctx.req.query('thread_id'),
				data,
				wait,
				requestCache: ctx.get('requestCache'),
			});
			if (!response) {
				return ctx.body(null, 204);
			}
			return ctx.json(response);
		},
	);
	app.get(
		'/webhooks/:webhook_id/:token/messages/:message_id',
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_MESSAGE_GET),
		BlockAppOriginMiddleware,
		OpenAPI({
			operationId: 'get_webhook_message',
			summary: 'Get webhook message',
			description:
				'Retrieves a message previously sent by the webhook. Only messages authored by the webhook can be retrieved.',
			responseSchema: MessageResponseSchema,
			statusCode: 200,
			tags: ['Webhooks'],
		}),
		Validator('param', WebhookIdTokenMessageIdParam),
		Validator('query', WebhookMessageQueryRequest),
		async (ctx) => {
			const {webhook_id: webhookId, token, message_id: messageId} = ctx.req.valid('param');
			const response = await ctx.get('webhookRequestService').getWebhookMessage({
				webhookId: createWebhookID(webhookId),
				token: createWebhookToken(token),
				threadId: ctx.req.query('thread_id'),
				messageId: createMessageID(messageId),
				requestCache: ctx.get('requestCache'),
			});
			return ctx.json(response);
		},
	);
	app.patch(
		'/webhooks/:webhook_id/:token/messages/:message_id',
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_MESSAGE_EDIT),
		BlockAppOriginMiddleware,
		OpenAPI({
			operationId: 'edit_webhook_message',
			summary: 'Edit webhook message',
			description:
				'Edits a message previously sent by the webhook. Only messages authored by the webhook can be edited. Returns the updated message object.',
			responseSchema: MessageResponseSchema,
			statusCode: 200,
			tags: ['Webhooks'],
		}),
		Validator('param', WebhookIdTokenMessageIdParam),
		Validator('query', WebhookMessageQueryRequest),
		Validator('json', WebhookMessageEditRequest),
		async (ctx) => {
			const {webhook_id: webhookId, token, message_id: messageId} = ctx.req.valid('param');
			const data = ctx.req.valid('json');
			const response = await ctx.get('webhookRequestService').editWebhookMessage({
				webhookId: createWebhookID(webhookId),
				token: createWebhookToken(token),
				threadId: ctx.req.query('thread_id'),
				messageId: createMessageID(messageId),
				data,
				requestCache: ctx.get('requestCache'),
			});
			return ctx.json(response);
		},
	);
	app.delete(
		'/webhooks/:webhook_id/:token/messages/:message_id',
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_MESSAGE_DELETE),
		BlockAppOriginMiddleware,
		OpenAPI({
			operationId: 'delete_webhook_message',
			summary: 'Delete webhook message',
			description:
				'Deletes a message previously sent by the webhook. Only messages authored by the webhook can be deleted. Returns a 204 status code on successful deletion.',
			responseSchema: null,
			statusCode: 204,
			tags: ['Webhooks'],
		}),
		Validator('param', WebhookIdTokenMessageIdParam),
		Validator('query', WebhookMessageQueryRequest),
		async (ctx) => {
			const {webhook_id: webhookId, token, message_id: messageId} = ctx.req.valid('param');
			await ctx.get('webhookService').deleteWebhookMessage({
				webhookId: createWebhookID(webhookId),
				token: createWebhookToken(token),
				threadId: ctx.req.query('thread_id'),
				messageId: createMessageID(messageId),
				requestCache: ctx.get('requestCache'),
			});
			return ctx.body(null, 204);
		},
	);
	app.post(
		'/webhooks/:webhook_id/:token/github',
		RequestBodyLimit(GITHUB_WEBHOOK_MAX_BODY_BYTES),
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_GITHUB),
		BlockAppOriginMiddleware,
		OpenAPI({
			operationId: 'execute_github_webhook',
			summary: 'Execute GitHub webhook',
			description:
				'Receives and processes GitHub webhook events, formatting them as messages in the configured channel. Reads event type from X-GitHub-Event header and delivery ID from X-GitHub-Delivery header.',
			responseSchema: null,
			statusCode: 204,
			tags: ['Webhooks'],
		}),
		Validator('param', WebhookIdTokenParam),
		Validator('query', WebhookMessageQueryRequest),
		Validator('json', GitHubWebhook),
		async (ctx) => {
			const {webhook_id: webhookId, token} = ctx.req.valid('param');
			await ctx.get('webhookService').executeGitHubWebhook({
				webhookId: createWebhookID(webhookId),
				token: createWebhookToken(token),
				threadId: ctx.req.query('thread_id'),
				event: ctx.req.header('X-GitHub-Event') ?? '',
				delivery: ctx.req.header('X-GitHub-Delivery') ?? '',
				data: ctx.req.valid('json'),
				requestCache: ctx.get('requestCache'),
			});
			return ctx.body(null, 204);
		},
	);
	app.post(
		'/webhooks/:webhook_id/:token/slack',
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_EXECUTE),
		BlockAppOriginMiddleware,
		OpenAPI({
			operationId: 'execute_slack_webhook',
			summary: 'Execute Slack webhook',
			description:
				'Receives and processes Slack-formatted webhook payloads, converting them to messages in the configured channel. Returns "ok" as plain text with a 200 status code.',
			responseSchema: SlackWebhookResponse,
			statusCode: 200,
			tags: ['Webhooks'],
		}),
		Validator('param', WebhookIdTokenParam),
		Validator('query', WebhookMessageQueryRequest),
		Validator('json', SlackWebhookRequest),
		async (ctx) => {
			const {webhook_id: webhookId, token} = ctx.req.valid('param');
			await ctx.get('webhookRequestService').executeSlackWebhook({
				webhookId: createWebhookID(webhookId),
				token: createWebhookToken(token),
				threadId: ctx.req.query('thread_id'),
				data: ctx.req.valid('json'),
				requestCache: ctx.get('requestCache'),
			});
			ctx.header('Content-Type', 'text/html; charset=utf-8');
			return ctx.body('ok', 200);
		},
	);
	app.post(
		'/webhooks/:webhook_id/:token/instatus',
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_INSTATUS),
		BlockAppOriginMiddleware,
		OpenAPI({
			operationId: 'execute_instatus_webhook',
			summary: 'Execute Instatus webhook',
			description:
				'Receives and processes Instatus status page webhook events, formatting them as messages in the configured channel.',
			responseSchema: null,
			statusCode: 204,
			tags: ['Webhooks'],
		}),
		Validator('param', WebhookIdTokenParam),
		Validator('json', InstatusWebhook),
		async (ctx) => {
			const {webhook_id: webhookId, token} = ctx.req.valid('param');
			await ctx.get('webhookService').executeInstatusWebhook({
				webhookId: createWebhookID(webhookId),
				token: createWebhookToken(token),
				data: ctx.req.valid('json'),
				requestCache: ctx.get('requestCache'),
			});
			return ctx.body(null, 204);
		},
	);
	app.post(
		'/webhooks/:webhook_id/:token/libredesk',
		RateLimitMiddleware(RateLimitConfigs.WEBHOOK_LIBREDESK),
		BlockAppOriginMiddleware,
		OpenAPI({
			operationId: 'execute_libredesk_webhook',
			summary: 'Execute Libredesk webhook',
			description:
				'Receives and processes Libredesk help desk webhook events, formatting them as messages in the configured channel.',
			responseSchema: null,
			statusCode: 204,
			tags: ['Webhooks'],
		}),
		Validator('param', WebhookIdTokenParam),
		Validator('query', WebhookMessageQueryRequest),
		Validator('json', LibredeskWebhook),
		async (ctx) => {
			const {webhook_id: webhookId, token} = ctx.req.valid('param');
			await ctx.get('webhookService').executeLibredeskWebhook({
				webhookId: createWebhookID(webhookId),
				token: createWebhookToken(token),
				threadId: ctx.req.query('thread_id'),
				data: ctx.req.valid('json'),
				requestCache: ctx.get('requestCache'),
			});
			return ctx.body(null, 204);
		},
	);
	app.post('/webhooks/livekit', async (ctx) => {
		const response = await ctx.get('webhookRequestService').handleLiveKitWebhook({
			body: await ctx.req.text(),
			authHeader: ctx.req.header('Authorization') ?? undefined,
		});
		return response;
	});
	app.post('/webhooks/sweego', async (ctx) => {
		const response = await ctx.get('webhookRequestService').handleSweegoWebhook({
			body: await ctx.req.text(),
			webhookId: ctx.req.header('webhook-id') ?? undefined,
			timestamp: ctx.req.header('webhook-timestamp') ?? undefined,
			signature: ctx.req.header('webhook-signature') ?? undefined,
		});
		return response;
	});
}