summary refs log tree commit diff
path: root/api/src/routes
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-10 14:10:27 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-10 14:10:27 +0200
commit29497bd4d34e7642248b573f384a51854333b297 (patch)
tree169d65e08f7b45644493bf90254bdc610ec04c46 /api/src/routes
parent:art: emoji db migration (diff)
downloadserver-29497bd4d34e7642248b573f384a51854333b297.tar.xz
:bug: fix emojis route
Diffstat (limited to 'api/src/routes')
-rw-r--r--api/src/routes/guilds/#guild_id/emoji.ts137
-rw-r--r--api/src/routes/guilds/#guild_id/emojis.ts121
2 files changed, 121 insertions, 137 deletions
diff --git a/api/src/routes/guilds/#guild_id/emoji.ts b/api/src/routes/guilds/#guild_id/emoji.ts
deleted file mode 100644
index 000c9949..00000000
--- a/api/src/routes/guilds/#guild_id/emoji.ts
+++ /dev/null
@@ -1,137 +0,0 @@
-import { Router, Request, Response } from "express";
-import {
-    Config,
-	DiscordApiErrors,
-	emitEvent,
-	Emoji,
-	GuildEmojiUpdateEvent,
-	handleFile,
-	Member,
-	Snowflake,
-	User
-} from "@fosscord/util";
-import { route } from "@fosscord/api";
-
-const router = Router();
-
-export interface EmojiCreateSchema {
-    name?: string;
-    image?: string;
-    require_colons?: boolean | null;
-    roles?: string[];
-}
-
-export interface EmojiModifySchema {
-    name?: string;
-    roles?: string[];
-}
-
-router.get("/", route({}), async (req: Request, res: Response) => {
-    const guild_id = req.params.guild_id;
-
-    await Member.IsInGuildOrFail(req.user_id, guild_id);
-
-    const emojis = await Emoji.find({ guild_id: guild_id });
-
-    return res.json(emojis);
-});
-
-router.get("/:emoji_id", route({}), async (req: Request, res: Response) => {
-    const guild_id = req.params.guild_id;
-    const emoji_id = req.params.emoji_id;
-
-    await Member.IsInGuildOrFail(req.user_id, guild_id);
-
-    const emoji = await Emoji.findOneOrFail({ guild_id: guild_id, id: emoji_id });
-
-    return res.json(emoji);
-});
-
-router.post("/", route({ body: "EmojiCreateSchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => {
-    const guild_id = req.params.guild_id;
-	const body = req.body as EmojiCreateSchema;
-
-    const emoji_count = await Emoji.count({ guild_id: guild_id });
-    const { maxEmojis } = Config.get().limits.guild;
-
-    if (emoji_count >= maxEmojis) throw DiscordApiErrors.MAXIMUM_NUMBER_OF_EMOJIS_REACHED.withParams(maxEmojis);
-
-    const id = Snowflake.generate();
-
-    if (!body.image) {
-        throw DiscordApiErrors.GENERAL_ERROR.withParams("No image provided");
-    }
-
-    if (body.require_colons === null) body.require_colons = true;
-
-    const user = await User.findOneOrFail({ id: req.user_id });
-
-    body.image = await handleFile(`/emojis/${id}`, body.image);
-
-    const emoji = new Emoji({
-        id: id,
-        guild_id: guild_id,
-        ...body,
-        user: user,
-        managed: false,
-        animated: false, // TODO: Add support animated emojis
-        available: true
-    });
-
-    await Promise.all([
-        emoji.save(),
-        emitEvent({
-            event: "GUILD_EMOJI_UPDATE",
-            guild_id: guild_id,
-            data: {
-                guild_id: guild_id,
-                emojis: await Emoji.find({ guild_id: guild_id })
-            }
-        } as GuildEmojiUpdateEvent)
-    ]);
-});
-
-router.patch("/:emoji_id", route({ body: "EmojiModifySchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => {
-    const { emoji_id, guild_id } = req.params;
-    const body = req.body as EmojiModifySchema;
-
-    const emoji = new Emoji({ ...body, id: emoji_id, guild_id: guild_id });
-
-    await Promise.all([
-        emoji.save(),
-        emitEvent({
-            event: "GUILD_EMOJI_UPDATE",
-            guild_id: guild_id,
-            data: {
-                guild_id: guild_id,
-                emojis: await Emoji.find({ guild_id: guild_id })
-            }
-        } as GuildEmojiUpdateEvent)
-    ]);
-
-    return res.json(emoji);
-});
-
-router.delete("/:emoji_id", route({ permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => {
-    const guild_id = req.params.guild_id;
-    const { emoji_id } = req.params;
-
-    await Promise.all([
-        Emoji.delete({
-            id: emoji_id,
-            guild_id: guild_id
-        }),
-        emitEvent({
-            event: "GUILD_EMOJI_UPDATE",
-            guild_id: guild_id,
-            data: {
-                guild_id: guild_id,
-                emojis: await Emoji.find({ guild_id: guild_id })
-            }
-        } as GuildEmojiUpdateEvent)
-    ])
-
-    res.sendStatus(204);
-});
-
-export default router;
diff --git a/api/src/routes/guilds/#guild_id/emojis.ts b/api/src/routes/guilds/#guild_id/emojis.ts
new file mode 100644
index 00000000..ff565cd4
--- /dev/null
+++ b/api/src/routes/guilds/#guild_id/emojis.ts
@@ -0,0 +1,121 @@
+import { Router, Request, Response } from "express";
+import { Config, DiscordApiErrors, emitEvent, Emoji, GuildEmojisUpdateEvent, handleFile, Member, Snowflake, User } from "@fosscord/util";
+import { route } from "@fosscord/api";
+
+const router = Router();
+
+export interface EmojiCreateSchema {
+	name?: string;
+	image: string;
+	require_colons?: boolean | null;
+	roles?: string[];
+}
+
+export interface EmojiModifySchema {
+	name?: string;
+	roles?: string[];
+}
+
+router.get("/", route({}), async (req: Request, res: Response) => {
+	const { guild_id } = req.params;
+
+	await Member.IsInGuildOrFail(req.user_id, guild_id);
+
+	const emojis = await Emoji.find({ where: { guild_id: guild_id }, relations: ["user"] });
+
+	return res.json(emojis);
+});
+
+router.get("/:emoji_id", route({}), async (req: Request, res: Response) => {
+	const { guild_id, emoji_id } = req.params;
+
+	await Member.IsInGuildOrFail(req.user_id, guild_id);
+
+	const emoji = await Emoji.findOneOrFail({ where: { guild_id: guild_id, id: emoji_id }, relations: ["user"] });
+
+	return res.json(emoji);
+});
+
+router.post("/", route({ body: "EmojiCreateSchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => {
+	const { guild_id } = req.params;
+	const body = req.body as EmojiCreateSchema;
+
+	const emoji_count = await Emoji.count({ guild_id: guild_id });
+	const { maxEmojis } = Config.get().limits.guild;
+
+	if (emoji_count >= maxEmojis) throw DiscordApiErrors.MAXIMUM_NUMBER_OF_EMOJIS_REACHED.withParams(maxEmojis);
+
+	const id = Snowflake.generate();
+
+	if (body.require_colons == null) body.require_colons = true;
+
+	const user = await User.findOneOrFail({ id: req.user_id });
+
+	body.image = (await handleFile(`/emojis/${id}`, body.image)) as string;
+
+	const emoji = await new Emoji({
+		id: id,
+		guild_id: guild_id,
+		...body,
+		user: user,
+		managed: false,
+		animated: false, // TODO: Add support animated emojis
+		available: true,
+		roles: []
+	}).save();
+
+	await emitEvent({
+		event: "GUILD_EMOJIS_UPDATE",
+		guild_id: guild_id,
+		data: {
+			guild_id: guild_id,
+			emojis: await Emoji.find({ guild_id: guild_id })
+		}
+	} as GuildEmojisUpdateEvent);
+
+	return res.status(201).json(emoji);
+});
+
+router.patch(
+	"/:emoji_id",
+	route({ body: "EmojiModifySchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }),
+	async (req: Request, res: Response) => {
+		const { emoji_id, guild_id } = req.params;
+		const body = req.body as EmojiModifySchema;
+
+		const emoji = await new Emoji({ ...body, id: emoji_id, guild_id: guild_id }).save();
+
+		await emitEvent({
+			event: "GUILD_EMOJIS_UPDATE",
+			guild_id: guild_id,
+			data: {
+				guild_id: guild_id,
+				emojis: await Emoji.find({ guild_id: guild_id })
+			}
+		} as GuildEmojisUpdateEvent);
+
+		return res.json(emoji);
+	}
+);
+
+router.delete("/:emoji_id", route({ permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => {
+	const { emoji_id, guild_id } = req.params;
+
+	await Emoji.delete({
+		id: emoji_id,
+		guild_id: guild_id
+	});
+
+	await emitEvent({
+		event: "GUILD_EMOJIS_UPDATE",
+		guild_id: guild_id,
+		data: {
+			guild_id: guild_id,
+			emojis: await Emoji.find({ guild_id: guild_id })
+		}
+	} as GuildEmojisUpdateEvent);
+
+	res.sendStatus(204);
+});
+
+export default router;