mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
180 lines
4.8 KiB
TypeScript
180 lines
4.8 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: { level: transaction.LOCK.NO_KEY_UPDATE, of: WebhookSubscription },
|
|
transaction,
|
|
});
|
|
|
|
authorize(user, "delete", webhookSubscription);
|
|
|
|
await webhookSubscription.destroyWithCtx(ctx, {
|
|
data: { name: webhookSubscription.name },
|
|
});
|
|
|
|
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: { level: transaction.LOCK.NO_KEY_UPDATE, of: WebhookSubscription },
|
|
transaction,
|
|
});
|
|
|
|
authorize(user, "update", webhookSubscription);
|
|
|
|
await webhookSubscription.updateWithCtx(
|
|
ctx,
|
|
{
|
|
name,
|
|
url,
|
|
events: compact(events),
|
|
enabled: true,
|
|
secret: secret === "" ? null : secret,
|
|
},
|
|
{ data: { name } }
|
|
);
|
|
|
|
ctx.body = {
|
|
data: presentWebhookSubscription(webhookSubscription),
|
|
};
|
|
}
|
|
);
|
|
|
|
export default router;
|