summary refs log tree commit diff
path: root/src/api/routes/guilds/#guild_id/emojis.ts
blob: 6e8570eb9467a33a32085ab650480d91ae21f444 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import { Router, Request, Response } from "express";
import {
	Config,
	DiscordApiErrors,
	emitEvent,
	Emoji,
	GuildEmojisUpdateEvent,
	handleFile,
	Member,
	Snowflake,
	User,
	EmojiCreateSchema,
	EmojiModifySchema,
} from "@fosscord/util";
import { route } from "@fosscord/api";

const router = Router();

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 id = Snowflake.generate();
		const emoji_count = await Emoji.count({
			where: { guild_id: guild_id },
		});
		const { maxEmojis } = Config.get().limits.guild;

		if (emoji_count >= maxEmojis)
			throw DiscordApiErrors.MAXIMUM_NUMBER_OF_EMOJIS_REACHED.withParams(
				maxEmojis,
			);
		if (body.require_colons == null) body.require_colons = true;

		const user = await User.findOneOrFail({ where: { id: req.user_id } });
		body.image = (await handleFile(`/emojis/${id}`, body.image)) as string;

		const emoji = await Emoji.create({
			id: id,
			guild_id: guild_id,
			...body,
			require_colons: body.require_colons ?? undefined, // schema allows nulls, db does not
			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({ where: { 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 Emoji.create({
			...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({ where: { 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({ where: { guild_id: guild_id } }),
			},
		} as GuildEmojisUpdateEvent);

		res.sendStatus(204);
	},
);

export default router;