diff options
author | Lobo Metalúrgico <43734867+LoboMetalurgico@users.noreply.github.com> | 2021-10-08 23:45:12 -0300 |
---|---|---|
committer | Lobo Metalúrgico <43734867+LoboMetalurgico@users.noreply.github.com> | 2021-10-08 23:45:12 -0300 |
commit | 90af904661ac5af8db83dc2e34fb4f6f2d0c9275 (patch) | |
tree | 89a3a27a84f7a3e0516daf3ba2f00809180778de /api/src/routes | |
parent | :bug: fix import (diff) | |
download | server-90af904661ac5af8db83dc2e34fb4f6f2d0c9275.tar.xz |
(api): inital emojis route implementation
Diffstat (limited to 'api/src/routes')
-rw-r--r-- | api/src/routes/guilds/#guild_id/emojis.ts | 78 |
1 files changed, 78 insertions, 0 deletions
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..f1e427e8 --- /dev/null +++ b/api/src/routes/guilds/#guild_id/emojis.ts @@ -0,0 +1,78 @@ +import { Router, Request, Response } from "express"; +import { + Config, + emitEvent, + Emoji, + GuildEmojiUpdateEvent, + Member, + User +} from "@fosscord/util"; +import { HTTPError } from "lambert-server"; +import { route } from "@fosscord/api"; + +const router = Router(); + +export interface EmojiCreateSchema { + name?: string; + image?: 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); +}); + +/** 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; + + 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); + } +}); +*/ + +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; |