Files
outline/plugins/webhooks/server/api/webhookSubscriptions.ts
T
Tom MoorandClaude Opus 5.5 5647621224 chore: Prune redundant comments and type suppressions (#14124)
* chore: Prune redundant comments and normalize lint directives

Removes comments that restate the following line and stale TODOs, drops
the passive event listener feature check now that all supported browsers
support it, and renames eslint directives to oxlint.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH

* fix: Evaluate restricted team domains at validation time

The restricted email provider list was captured when the model loaded, so
it could not follow the hosting environment and the tests that covered it
never reached the model.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH

* chore: Replace type suppressions with accurate types

Adds a Concrete helper type for registries of abstract classes, declares
missing Sequelize and refractor typings, uses public ProseMirror APIs, a
typed mock context helper in middleware tests, and a StaticLink component
for sidebar rows being edited.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BehTjuZwKbN5csMCp3H4QH

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-10-17 17:07:39 -04:00

174 lines
4.6 KiB
TypeScript

import Router from "@koa/router";
import { compact, isEmpty } from "es-toolkit/compat";
import { Op, Sequelize, type WhereOptions } from "sequelize";
import { UserRole } from "@shared/types";
import auth from "@server/middlewares/authentication";
import { transaction } from "@server/middlewares/transaction";
import validate from "@server/middlewares/validate";
import { WebhookSubscription } from "@server/models";
import { authorize } from "@server/policies";
import pagination, {
paginateQuery,
} from "@server/routes/api/middlewares/pagination";
import type { APIContext } from "@server/types";
import { AuthenticationType } from "@server/types";
import { QueryHelper } from "@server/storage/QueryHelper";
import presentWebhookSubscription from "../presenters/webhookSubscription";
import * as T from "./schema";
const router = new Router();
router.post(
"webhookSubscriptions.list",
auth({ role: UserRole.Admin }),
pagination(),
validate(T.WebhookSubscriptionsListSchema),
async (ctx: APIContext<T.WebhookSubscriptionsListReq>) => {
const { sort, direction, query } = ctx.input.body;
const { user } = ctx.state.auth;
authorize(user, "listWebhookSubscription", user.team);
let where: WhereOptions<WebhookSubscription> = {
teamId: user.teamId,
};
if (query) {
where = {
...where,
[Op.and]: [
Sequelize.literal(
`unaccent(LOWER("webhook_subscription"."name")) like unaccent(LOWER(:query))`
),
],
};
}
const replacements = { query: QueryHelper.likeContains(query ?? "") };
const { results, pagination } = await paginateQuery(
ctx,
(opts) =>
WebhookSubscription.findAll({
where,
replacements,
include: [
{
association: "createdBy",
required: false,
},
],
order: [[sort, direction]],
offset: opts.offset,
limit: opts.limit,
}),
() =>
WebhookSubscription.count({
where,
replacements,
}) as unknown as Promise<number>
);
ctx.body = {
pagination,
data: results.map(presentWebhookSubscription),
};
}
);
router.post(
"webhookSubscriptions.create",
auth({
role: UserRole.Admin,
type: [AuthenticationType.API, AuthenticationType.APP],
}),
validate(T.WebhookSubscriptionsCreateSchema),
transaction(),
async (ctx: APIContext<T.WebhookSubscriptionsCreateReq>) => {
const { name, url, secret, events } = ctx.input.body;
const { user } = ctx.state.auth;
authorize(user, "createWebhookSubscription", user.team);
const webhookSubscription = await WebhookSubscription.createWithCtx(ctx, {
name,
url,
events: compact(events),
enabled: true,
secret: isEmpty(secret) ? undefined : secret,
createdById: user.id,
teamId: user.teamId,
});
ctx.body = {
data: presentWebhookSubscription(webhookSubscription),
};
}
);
router.post(
"webhookSubscriptions.delete",
auth({
role: UserRole.Admin,
type: [AuthenticationType.API, AuthenticationType.APP],
}),
validate(T.WebhookSubscriptionsDeleteSchema),
transaction(),
async (ctx: APIContext<T.WebhookSubscriptionsDeleteReq>) => {
const { id } = ctx.input.body;
const { user } = ctx.state.auth;
const { transaction } = ctx.state;
const webhookSubscription = await WebhookSubscription.findByPk(id, {
rejectOnEmpty: true,
lock: transaction.LOCK.UPDATE,
transaction,
});
authorize(user, "delete", webhookSubscription);
await webhookSubscription.destroyWithCtx(ctx);
ctx.body = {
success: true,
};
}
);
router.post(
"webhookSubscriptions.update",
auth({
role: UserRole.Admin,
type: [AuthenticationType.API, AuthenticationType.APP],
}),
validate(T.WebhookSubscriptionsUpdateSchema),
transaction(),
async (ctx: APIContext<T.WebhookSubscriptionsUpdateReq>) => {
const { id, name, url, secret, events } = ctx.input.body;
const { user } = ctx.state.auth;
const { transaction } = ctx.state;
const webhookSubscription = await WebhookSubscription.findByPk(id, {
rejectOnEmpty: true,
lock: transaction.LOCK.UPDATE,
transaction,
});
authorize(user, "update", webhookSubscription);
await webhookSubscription.updateWithCtx(ctx, {
name,
url,
events: compact(events),
enabled: true,
secret: isEmpty(secret) ? undefined : secret,
});
ctx.body = {
data: presentWebhookSubscription(webhookSubscription),
};
}
);
export default router;