summary refs log tree commit diff
path: root/api/src
diff options
context:
space:
mode:
authorLobo Metalúrgico <43734867+LoboMetalurgico@users.noreply.github.com>2021-10-09 11:36:58 -0300
committerLobo Metalúrgico <43734867+LoboMetalurgico@users.noreply.github.com>2021-10-09 11:36:58 -0300
commit4e684e2cd45b12184422ec9933476e553d037016 (patch)
tree0924c3cf4e9210cb16b7ee130dd6bbd0c2a67b32 /api/src
parent(api): add patch to emoji route (diff)
downloadserver-4e684e2cd45b12184422ec9933476e553d037016.tar.xz
(api): implement post on emoji route
Diffstat (limited to 'api/src')
-rw-r--r--api/src/routes/guilds/#guild_id/emoji.ts42
1 files changed, 38 insertions, 4 deletions
diff --git a/api/src/routes/guilds/#guild_id/emoji.ts b/api/src/routes/guilds/#guild_id/emoji.ts
index f14790ed..6b194f7b 100644
--- a/api/src/routes/guilds/#guild_id/emoji.ts
+++ b/api/src/routes/guilds/#guild_id/emoji.ts
@@ -1,10 +1,13 @@
 import { Router, Request, Response } from "express";
 import {
     Config,
+	DiscordApiErrors,
 	emitEvent,
 	Emoji,
 	GuildEmojiUpdateEvent,
+	handleFile,
 	Member,
+	Snowflake,
 	User
 } from "@fosscord/util";
 import { HTTPError } from "lambert-server";
@@ -15,6 +18,7 @@ const router = Router();
 export interface EmojiCreateSchema {
     name?: string;
     image?: string;
+    require_colons?: boolean | null;
     roles?: string[];
 }
 
@@ -44,7 +48,6 @@ router.get("/:emoji_id", route({}), async (req: Request, res: Response) => {
     return res.json(emoji);
 });
 
-/** WIP
 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;
@@ -52,11 +55,42 @@ router.post("/", route({ body: "EmojiCreateSchema", permission: "MANAGE_EMOJIS_A
     const emoji_count = await Emoji.count({ guild_id: guild_id });
     const { maxEmojis } = Config.get().limits.guild;
 
-    if (emoji_count >= maxEmojis) {
-        throw new HTTPError("Max emojis reached", 400);
+    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;