summary refs log tree commit diff
path: root/api/src/routes
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-14 22:15:55 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-14 22:15:55 +0200
commitf691aa4c5aa47c8a8085c7b01912a1c403ce8732 (patch)
tree532673ee0955f5eec2c8a33c7abad8ed64dc9a48 /api/src/routes
parentfix #129 (diff)
downloadserver-f691aa4c5aa47c8a8085c7b01912a1c403ce8732.tar.xz
:construction: webhook
Diffstat (limited to 'api/src/routes')
-rw-r--r--api/src/routes/discoverable-guilds.ts2
-rw-r--r--api/src/routes/guilds/#guild_id/integrations.ts10
-rw-r--r--api/src/routes/template.ts.disabled2
-rw-r--r--api/src/routes/webhooks/#webhook_id/index.ts89
4 files changed, 101 insertions, 2 deletions
diff --git a/api/src/routes/discoverable-guilds.ts b/api/src/routes/discoverable-guilds.ts
index f667eb2a..71789123 100644
--- a/api/src/routes/discoverable-guilds.ts
+++ b/api/src/routes/discoverable-guilds.ts
@@ -10,7 +10,7 @@ router.get("/", route({}), async (req: Request, res: Response) => {
 	// ! this only works using SQL querys
 	// TODO: implement this with default typeorm query
 	// const guilds = await Guild.find({ where: { features: "DISCOVERABLE" } }); //, take: Math.abs(Number(limit)) });
-	const guilds = await Guild.find({ where: `"features" LIKE 'COMMUNITY'`, take: Math.abs(Number(limit)) });
+	const guilds = await Guild.find({ where: `"features" LIKE 'COMMUNITY'`, take: Math.abs(Number(limit) || 50) });
 	res.send({ guilds: guilds });
 });
 
diff --git a/api/src/routes/guilds/#guild_id/integrations.ts b/api/src/routes/guilds/#guild_id/integrations.ts
new file mode 100644
index 00000000..f6b8e99d
--- /dev/null
+++ b/api/src/routes/guilds/#guild_id/integrations.ts
@@ -0,0 +1,10 @@
+import { route } from "@fosscord/api";
+import { Router, Request, Response } from "express";
+const router = Router();
+
+router.get("/", route({ permission: "MANAGE_GUILD" }), async (req: Request, res: Response) => {
+	// TODO: integrations (followed channels, youtube, twitch)
+	res.send([]);
+});
+
+export default router;
diff --git a/api/src/routes/template.ts.disabled b/api/src/routes/template.ts.disabled
index ad785f10..524e981b 100644
--- a/api/src/routes/template.ts.disabled
+++ b/api/src/routes/template.ts.disabled
@@ -4,7 +4,7 @@ import { Router, Request, Response } from "express";
 const router = Router();
 
 router.get("/", async (req: Request, res: Response) => {
-	res.send({});
+	res.json({});
 });
 
 export default router;
diff --git a/api/src/routes/webhooks/#webhook_id/index.ts b/api/src/routes/webhooks/#webhook_id/index.ts
new file mode 100644
index 00000000..e9b40ebf
--- /dev/null
+++ b/api/src/routes/webhooks/#webhook_id/index.ts
@@ -0,0 +1,89 @@
+import { Channel, Config, emitEvent, JWTOptions, Webhook, WebhooksUpdateEvent } from "@fosscord/util";
+import { route, Authentication, handleFile } from "@fosscord/api";
+import { Router, Request, Response, NextFunction } from "express";
+import jwt from "jsonwebtoken";
+import { HTTPError } from "lambert-server";
+const router = Router();
+
+export interface WebhookModifySchema {
+	name?: string;
+	avatar?: string;
+	// channel_id?: string; // TODO
+}
+
+function validateWebhookToken(req: Request, res: Response, next: NextFunction) {
+	const { jwtSecret } = Config.get().security;
+
+	jwt.verify(req.params.token, jwtSecret, JWTOptions, async (err, decoded: any) => {
+		if (err) return next(new HTTPError("Invalid Token", 401));
+		next();
+	});
+}
+
+router.get("/", route({}), async (req: Request, res: Response) => {
+	res.json(await Webhook.findOneOrFail({ id: req.params.webhook_id }));
+});
+
+router.get("/:token", route({}), validateWebhookToken, async (req: Request, res: Response) => {
+	res.json(await Webhook.findOneOrFail({ id: req.params.webhook_id }));
+});
+
+router.patch("/", route({ body: "WebhookModifySchema", permission: "MANAGE_WEBHOOKS" }), (req: Request, res: Response) => {
+	return updateWebhook(req, res);
+});
+
+router.patch("/:token", route({ body: "WebhookModifySchema" }), validateWebhookToken, (req: Request, res: Response) => {
+	return updateWebhook(req, res);
+});
+
+async function updateWebhook(req: Request, res: Response) {
+	const webhook = await Webhook.findOneOrFail({ id: req.params.webhook_id });
+	if (req.body.channel_id) await Channel.findOneOrFail({ id: req.body.channel_id, guild_id: webhook.guild_id });
+
+	webhook.assign({
+		...req.body,
+		avatar: await handleFile(`/icons/${req.params.webhook_id}`, req.body.avatar)
+	});
+
+	await Promise.all([
+		emitEvent({
+			event: "WEBHOOKS_UPDATE",
+			channel_id: webhook.channel_id,
+			data: {
+				channel_id: webhook.channel_id,
+				guild_id: webhook.guild_id
+			}
+		} as WebhooksUpdateEvent),
+		webhook.save()
+	]);
+
+	res.json(webhook);
+}
+
+router.delete("/", route({ permission: "MANAGE_WEBHOOKS" }), async (req: Request, res: Response) => {
+	return deleteWebhook(req, res);
+});
+
+router.delete("/:token", route({}), validateWebhookToken, (req: Request, res: Response) => {
+	return deleteWebhook(req, res);
+});
+
+async function deleteWebhook(req: Request, res: Response) {
+	const webhook = await Webhook.findOneOrFail({ id: req.params.webhook_id });
+
+	await Promise.all([
+		emitEvent({
+			event: "WEBHOOKS_UPDATE",
+			channel_id: webhook.channel_id,
+			data: {
+				channel_id: webhook.channel_id,
+				guild_id: webhook.guild_id
+			}
+		} as WebhooksUpdateEvent),
+		webhook.remove()
+	]);
+
+	res.sendStatus(204);
+}
+
+export default router;