summary refs log tree commit diff
path: root/api/src/routes/guilds/#guild_id/vanity-url.ts
blob: 335cea27b32e741340c00019395f59b4b323949b (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
import { Channel, ChannelType, getPermission, Guild, InviteModel, trimSpecial } from "@fosscord/util";
import { Router, Request, Response } from "express";
import { HTTPError } from "lambert-server";
import { check, Length } from "../../../util/instanceOf";
import { isMember } from "../../../util/Member";

const router = Router();

const InviteRegex = /\W/g;

router.get("/", async (req: Request, res: Response) => {
	const { guild_id } = req.params;

	const permission = await getPermission(req.user_id, guild_id);
	permission.hasThrow("MANAGE_GUILD");

	const guild = await Guild.findOneOrFail({ id: guild_id });
	if (!guild.vanity_url_code) return res.json({ code: null });
	const { uses } = await Invite.findOneOrFail({ code: guild.vanity_url_code });

	return res.json({ code: guild.vanity_url_code, uses });
});

// TODO: check if guild is elgible for vanity url
router.patch("/", check({ code: new Length(String, 0, 20) }), async (req: Request, res: Response) => {
	const { guild_id } = req.params;
	var code = req.body.code.replace(InviteRegex);
	if (!code) code = null;

	const guild = await Guild.findOneOrFail({ id: guild_id });
	const permission = await getPermission(req.user_id, guild_id, undefined, { guild });
	permission.hasThrow("MANAGE_GUILD");

	const alreadyExists = await Promise.all([
		Guild.findOneOrFail({ vanity_url_code: code }).catch(() => null),
		Invite.findOneOrFail({ code: code }).catch(() => null)
	]);
	if (alreadyExists.some((x) => x)) throw new HTTPError("Vanity url already exists", 400);

	await Guild.update({ id: guild_id }, { vanity_url_code: code });
	const { id } = await Channel.findOneOrFail({ guild_id, type: ChannelType.GUILD_TEXT });
	await Invite.update(
		{ code: guild.vanity_url_code },
		{
			code: code,
			uses: 0,
			created_at: new Date(),
			guild_id,
			channel_id: id
		},
		{ upsert: true }
	);

	return res.json({ code: code });
});

export default router;