summary refs log tree commit diff
path: root/api/src/routes/guilds/#guild_id/emoji.ts
blob: 000c9949faa2df2aebc6a1e4186fb76e0a24bdde (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
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;